Home > Enterprise >  Why am I not able to import OpenCV in VS Code?
Why am I not able to import OpenCV in VS Code?

Time:05-30

In command prompt, I've used the command pip install opencv-python to install OpenCV, and it seems to have installed successfully.

When I type python in command prompt, press enter, and type import cv2, I get no error message.

However, when running the same single line of code in a python file in VS Code,

import cv2

I get the following in the terminal of VS Code:

PS C:\Users\George Kalpakis> conda activate base
conda : The term 'conda' is not recognized as the name of a cmdlet, function, script file, 
or operable program. Check the spelling of the name, or if a path was included, verify that 
the path is correct and try again.
At line:1 char:1
  conda activate base
  ~~~~~
      CategoryInfo          : ObjectNotFound: (conda:String) [], CommandNotFoundException   
      FullyQualifiedErrorId : CommandNotFoundException
 
PS C:\Users\George Kalpakis> & "C:/Users/George Kalpakis/anaconda3/python.exe" "c:/Users/George Kalpakis/Desktop/OpenCV/read.py"
Traceback (most recent call last):
  File "c:/Users/George Kalpakis/Desktop/OpenCV/read.py", line 1, in <module>
    import cv2
ModuleNotFoundError: No module named 'cv2'

What is my issue? Please let me know any additional info I can provide to help someone help me solve this.

CodePudding user response:

I think the problem is that you have conda installed and you are trying to access python through conda. are you planning on the library within conda? if so just install it through conda.

If not, I would suggest downloading python if you dont have it already installed (not through cond, make sure its on path - part of the options when downloading it), creating a virtual environment and installing all the libraries you need there (make sure VS Code python interpreter is pointing to the one you just downloaded not the conda one). additional suggestion: preferably use powershell (already installed on your windows pc) rather than cmdlet

CodePudding user response:

Obviously, there are multiple Python environments in your vscode.

According to your error report, you didn't use conda, but you use the Python interpreter in the conda environment.

You can use "ctrl shift P" and choose python interpreter.

You can also use the following code to install the package to the specified directory:

pip install -t floderPath opencv-python

enter image description here

  • Related