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

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.

Related

How to fix "No module named 'nmap'"

import nmap
ModuleNotFoundError: No module named 'nmap'
the above is the result when trying to run the code after importing nmap. (I have installed python-nmap using pip in cmd)
i found out the issue. when i chose a python env eg- python 3.8 32bit from the IDE, i chose a custom version thats why the imports dont work. when i chose the default env it worked. anyways thank for the help. there is nothing wrong with the IDE
I had to do two things to get it working:
Install the latest version of Python3 (3.9.6) https://www.python.org/downloads/release/python-396/
On your terminal:
pip3 install python-nmap
I had to do the following to get things to work:
On your terminal, switch to root privilege:
su - root
Then, install python-nmap module:
pip3 install python-nmap
Reference: https://github.com/securipy/nmap-scan/issues/26
STEP 1:
Uninstall the package using:
pip3 uninstall python-nmap
STEP 2:
Install the package:
pip3 install --user python-nmap

Issue with Jupyter notebook and virtual environment pip

I have created a virtual environment using the following commands:
python3 -m venv venv
.\venv\Scripts\activate.bat
pip3 install ipykernel
pip3 install jupyter
python3 -m ipykernel install --user --name=venv
I have created a Jupyter notebook using the venv that I created, but when I install a package in the virtual environment, it is not recognised in the notebook.
For example, I tried to install pandas using pip3 install pandas, but when I try and import it into my notebook I get the error ModuleNotFoundError: No module named 'pandas'
The module has installed in the right place venv\Lib\site-packages\pandas\
Any ideas on what I should do?
EDIT:
I noticed that even though I created the notebook using the venv, it uses the normal python environment rather than the virtual one. Ideas on how to fix this?
Well I think I solved it. If ran the following command:
python -c "import IPython"
Which just installs IPython in my venv.
You need to restart the notebook kernel, it will probably work then.
In the future, in a notebook cell you can run
%%bash
pip install pandas
then you should be able to continue without restarting

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

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

Trying to install a pip package in Anaconda

I'm trying to follow this tutorial:
https://learn.microsoft.com/en-us/azure/machine-learning/service/tutorial-data-prep
As part of this I'm trying to do a pip install of azureml as it's not available on conda. However doing a pip install will by default install it to my default python install, and not my conda install.
So I tried following the steps here:
https://conda.io/docs/user-guide/tasks/manage-environments.html#using-pip-in-an-environment
However after following these steps I then launch Jupyter notebook after activating myenv, navigate to the notebook, and try and run:
import azureml.dataprep as dprep
But get the error: ModuleNotFoundError: No module named 'azureml'
Also - I cannot tell if myenv is active in the notebook. The kernel simply says python3.
Be careful, when using pip in anaconda, it is possible that you are mixing pip and pip3.
Run which pip3 to be sure you are using the version that correspond to the virtual environment.
If you are using python3 in the environment, then pip will typically be the correct version to use. Do not use pip3 in that case.
This problem has been documented elsewhere on the web. The problem is that Jupyter notebooks itself only launches in the root environment by default. The simplest solution to getting it to launch for your env (e.g. myenv) is to install Jupyter within your env first. So from the Anaconda command prompt:
activate myenv
pip install jupyter
jupyter
Ps. Use source activate myenv for non-windows machines

Install Numpy in pydev(eclipse)

I am trying to install a package called 'numpy'.
i have python setup in eclipse luna with the help of pydev.
how do i install numpy in pydev.
tried putting numpy in site-packages folder. doesnt seem to work
In Eclipse, Goto Windows->Preferences
Select PyDev->Interpreters->Python Interpreters
Then click on Manage with pip button, then you will see a new window like below, you just need to type your package and click Run/Press enter.
After this, Click Apply and OK, then Right click on Project then click on PyDev->Remove error markers.
Hope it works.
do you have pip installed with your python?
How to install pip
Then if you have your path variable set you can simply type "pip install numpy" into command line.
download the required version of numpy from here http://sourceforge.net/projects/numpy/files/NumPy/1.9.2/
and the install directly ,it will run on eclipse automatically
If it does not solve your purpose use Pip:
pip install numpy
Correct way is to create a virtualenv virtualenv ~/venvs/eclipse, install numpy (source ~/venv/eclipse/bin/activate;pip install numpy), then add the virtualenv to eclipse (see https://www.rose-hulman.edu/class/csse/resources/Eclipse/eclipse-python-configuration.htm)
Pandas can be installed after install python in to your pc.
to install pandas go to command prompt and type "pip install pandas" this command collecting packages related to pandas. After if it asking to upgrade pip or if pip is not recognized by the command prompt use this command:
python -m pip install --upgrade pip.

Categories

Resources