添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
安静的松树  ·  ImportError: DLL load ...·  12 小时前    · 
魁梧的排球  ·  Issue ...·  12 小时前    · 
机灵的手电筒  ·  Re:openvino.runtime - ...·  12 小时前    · 
粗眉毛的青蛙  ·  spark ...·  4 小时前    · 
大鼻子的伤疤  ·  2024 年曼岛 TT ...·  1 月前    · 
傲视众生的伤疤  ·  MaxCompute ...·  9 月前    · 

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hello,I have Python via anaconda and have already installed all the necessary things via “pip install” at the beginning. Only when I run my Python file, I encounter the following error: “ line 12, in import cv2 ModuleNotFoundError: No module named 'cv2 ” I have already uninstalled and reinstalled opencv, but the problem persists. Even commands like “conda install opencv” or “conda install cv2” do not help. So my question is if you have any idea how I can solve the problem. Thanks in advance!

Version: Carla 0.9.15 - Windows 10

It does not seem a Carla problem, but more a opencv related one. If opencv is installed correctly, you may find opencv-python in the list of pip packages when doing pip list in the shell.
Also, you can just try typing in the command line python3 -c "import opencv as cv2" to check whether the package is properly installed.
Could you please share the part of code where the importation fails? Which version of OpenCV have you installed?

Thanks for the answer! I can find opencv in the list of pip packages while using anaconda prompt (With normal cmd not) .But when I try python3 -c "import opencv as cv2" I get following error: "Python could not be found. Run the shortcut without arguments to install it via the Microsoft Store or disable this shortcut under". My Python version is 3.7.12 packaged by conda-forge and my OpenCV version is 4.9.0.80.

Code:

import numpy as np
import glob
import os
import sys
import random
import carla
import random
import time
import cv2

sys.path.append(glob.glob('../carla/dist/carla-*%d.%d-%s.egg' %
sys.version_info.major,
sys.version_info.minor,
'win-amd64' if os.name == 'nt' else 'linux-x86_64'))[0])
except IndexError:

Thanks for the answer! I can find opencv in the list of pip packages while using anaconda prompt (With normal cmd not) .But when I try python3 -c "import opencv as cv2" I get following error: "Python could not be found. Run the shortcut without arguments to install it via the Microsoft Store or disable this shortcut under".

That error points to some issue with python itself. Do you still get the same with running python -c "import cv2" (so using python instead of python3 ?

How have you installed opencv with pip? The pip command should be pip install opencv-python

When you run the script, make sure that you are using the python 3 version (and not python 2, in some cases typing just python would use python2, where I guess you have not installed opencv).

Yes i installed opencv with pip. I use pycharm for writing my script so for running I navigate with anaconda prompt to the folder "examples" , in which my script is, and then type just the name with .py .
Edit: Now I have solved the problem. Thanks for your help- i assume that this will not be my last problem with opencv.😅

using vs code
used conda to install opencv
can see opencv 4.6.0 in conda list, but importing cv2 in code returns no module called cv2 found

python3 -c "import opencv as cv2" returns python not found
python -c "import opencv as cv2" returns cv2 not found

Create a new Conda environment, use
#conda install -c conda-forge opencv
check for its version once you installed,
#python -c "mport cv2; print(cv2. version )"
it will show the version, thus it says the library is downloaded.

Wow. First of all, "import opencv" is wrong. It will never work. Because it's wrong. It didn't work because that's wrong. Please never ever repeat that string of characters.

You are supposed to use import cv2 as cv , and then use cv as in im = cv.imread(some_path) , or as a code block:

import cv2 as cv
im = cv.imread(some_path)

I believe you are incorrect. "import cv2 as cv" will not fix the issue. The program will still attempt to import cv2 under the alias of cv. This is equivalent to just writing "import cv2" in terms of the library being accessed. The core issue is with the cv2 library not being able to be found outside of the virtual environment. I don't know how to solve this problem however.

I believe you are incorrect.

I addressed people suggesting the wrong import statement ("import opencv", must be "import cv2" or the like), because the wrong import would never work, not even if the package is installed.

I hope you understand now.