I installed opencv library by using "pip install opencv-python", but ModuleNotFoundError: No module named ‘cv2’ error message came out. So I tried to install in anaconda prompt, and I installed opencv-python again, but it stilll didn’t work. vscode, python version 3.8.
terminal capture
I went to opencv github site, and I knew that I had to install only one option of opencv package. so I removed all of opencv packages and installed opencv-python again, but it didnt work…..ModuleNotFoundError: No module named ‘cv2’… how can I import cv2?
3
Answers
As you can see in the terminal output, the basic pip you use is the one from Python3.8 installed on your computer
pip 23.1.2 from C:users...python38libsite-packagespip (python 3.8)
.But you try to run your program from Python3.9
& C:users...python39python.exe
. The installed modules are not available for the other versions of python installed, so if you want the program to work you will have to either launch your program with python 3.8, or install opencv with your python 3.9 pip.It seems you are trying to run your code by
python3.9
but you are installing opencv via onpython3.8
. To be sure which python version is used for pip you can run the command:Also you can still run the code via on
Python3.9
. All you need to do is that installing opencv by python3.9:This command is helpfull when you need to install a package for a specific python version.
You install the
opencv
package in the python 38 environment, but use pyth 39 to execute the code, of course you will get errors.You can use Ctrl+Shift+P –>
Python: Select Interpreter
to select the correct interpreter (Or click on the current python version shown in the lower right corner.)and then run the script through the triangle button in the upper right corner.
Follow this doc to start using python in vscode.
More information about the python environment.