How can I import "opencv" library in anaconda spyder on windows - python

I have installed OpenCv library through this line "pip install opencv-python" and it is successfully installed, but I am trying to import it through: "import cv2" and it gives me an error:
ModuleNotFoundError: No module named 'cv2'
please help me!

If your pip is installed with any other python version than conda, it won't work.
Option 1) Install pip with conda python
Run conda create -n venv_name and source activate venv_name, where venv_name is the name of your virtual environment.
Run conda install pip. This will install pip to your venv directory.
Option 2) Run conda install opencv

Related

DLL error while adding the environment into the jupyter notebook

I recently installed anaconda and created a new environment in a different location outside of anaconda base directory.
So I created the environment:
python -m venv tfod
Activated it
.\tensorflowenv\Scripts\activate
Upgraded pip and installed ipykernel
python -m pip install --upgrade pip
pip install ipykernel
Issue starts when i try to install/add this new environment into jupyter kernel so I can use this env
python -m ipykernel install --user --name=tensorflowenv
but getting this DLL error
File "D:\project\Tensorflow_obj\TFODCourse\tensorflowenv\lib\site-packages\zmq\backend\cython\__init__.py", line 6, in <module>
from . import (
ImportError: DLL load failed while importing _device: The file cannot be accessed by the system.
This isn't the exact "solution" for this but its a work around.
I created an environment using conda navigator and downloaded the required packages from navigator itself and then added the env into jupyter kernel using
python -m ipykernel install --user --name=tensorflowenv
Still would be happy if someone can provide a actual solution for this

ModuleNotFoundError: No module named 'cv2' (only in vscode with venv)

When I try to install opencv-python I get the following error:
ModuleNotFoundError: No module named 'cv2'
I tried to install cv2 via these commands:
pip install opencv-python
pip install opencv-contrib-python
pip install opencv-python opencv-python-headless
python3 -m pip install opencv-python
pip3 install opencv-python --upgrade
However, the problem remains.
I have noticed that this problem occurs only in the virtual environment (venv) of VScode.
In fact, if I launch this code from the terminal it does not give me any error and it returns me the version of cv2
import cv2
print (cv2.__version__)
Reply -> 4.6.0
I use Python 3.9
EDIT:
This is a screenshot of the problem
The problem do not appear if I create a new venv
Use Ctrl+Shift+P to open the command palette search and select Python:Select Interpreter, then select the correct interpreter, this will solve your problem.
Tip: Every time you choose a new interpreter, please create a new terminal to activate the environment.
As far as your question is concerned, you can follow these steps:
Select the interpreter in the virtual environment according to the above method
Create a new terminal
Use pip install opencv-python to install the package
Also you can use pip show opencv-python to see where the package is installed.
What solved it for me was adding the following to the .vscode/settings.json config file
// Add venv to python path
"python.analysis.extraPaths": [
"${workspaceFolder}/venv/lib/python3.8/site-packages"
]
change venv to the name of your virtual env folder.

Error when importing cv2 on anaconda prompt but it is working on jupyter notebook

I am importing libraries that I installed using pip install to Jupyter Notebook using the anaconda distribution, which is working. Next, I am trying to import the same libraries in the anaconda command prompt and running it. I am getting this error message:
ModuleNotFoundError: No module named 'cv2'
try these things:
First check if you are able to see your library by this command:
$ pip list or conda list
if not available install this module:
$ pip install opencv-python or conda install opencv-python
But before that, try to check numpy module is available or not. If numpy is not available, then install this first.
$ pip install numpy
If all of them exist then, Uninstall opencv-python and numpy first and upgrade your pip using the below command.
$ pip install --upgrade pip
Two options to install cv2 with the latest conda & python3:
Open the Anaconda Prompt and switch to your environment:
$ conda activate myenv
Then install opencv from the menpo channel:
$ conda install -c menpo opencv
Open the Anaconda Prompt with admin permission and make sure you're in the base environment:
$ conda activate base
Then install opencv from the menpo channel:
$ conda install -c menpo opencv
Reconnect your notebook to the kernel to import cv2.

AWS EC2 Python ModuleNotFoundError

I am using an EC2 instance with Deep Learning AMI based on Ubuntu 18.04.
I am doing the following:
Start the terminal
Activate the conda environment: conda activate tensorflow2_latest_p37
Install the package pip3 install tensorrt
Run my code python3 mycode.py
And I get the following error:
ModuleNotFoundError: No module named 'tensorrt'
I also trying to install as sudo or with -U or -m option. Nothing work. I can't install with conda install because tensorrt is not available in it.
pip3 will use ubuntu's level pip3, not from anaconda. You can confirm this by using which pip3 after you activate your environemnt. ALso tensorrt is not available for python other then 3.6
So to install tensorrt on Ubuntu 18.04 Deep Learnig:
conda activate tensorflow2_p36
# then in tensorflow2_p36 environment
pip install nvidia-pyindex
pip install --upgrade nvidia-tensorrt

install pip3 for conda

Python2.6 was installed by default in my old centos server. Now I want to create a Python3 environment to install python3 specific module by conda
conda create -n py3 python=3.5.3
source activate py3
After activate the py3, I try to install hovercraft by pip3 install hovercraft, the shell tells "command not found: pip3". At first I thought pip3 was installed with Python3, but the result was not the case.
So I think I can install it manually. The package gzip file was downloaded from python package index, and install by conda install --file hovercraft-2.3.tar.gz. But it doesn't work.
Now I have two problems:
how to install pip3 for virtual-env create by conda?
Is it possible to install python package index downloaded package locally in conda?
pip3 and pip would make a difference only when you are not using any environment managers like virualenv (or) conda. Now as you are creating a conda environment which has python==3.x, pip would be equivalent to pip3.

Categories

Resources