Cannot import module after installing, Anaconda/Spyder/Jupyter

Question:

I installed opencv by using the windows command prompt and entered: pip install opencv-python

I verified that it installed by entering python followed by import cv2

>>> import cv2

I did not receive any error messages, in fact received no message at all, which indicates to me everything installed correctly.

However, when I attempt to import cv2 on Spyder IDE or on Jupyter Notebook, I get the following error:

ModuleNotFoundError: No module named ‘cv2’

I’ve tried reinstalling it, tried to install it through Anaconda prompt, and tried looking for solutions in other forums, but cannot seem to solve why I cannot import cv2.

I am able to import other modules such as import numpy. Please help, thank you.

1st Edit:
I open Juypter Notebook with Python 3 (ipykernel), and my system runs Python 3.10.5

2nd Edit:
I originally installed Spyder alone without Anaconda. However, recently I downloaded both Jupyter and Spyder through Anaconda. I downloaded it from the official Anaconda website: https://www.anaconda.com/

So yes! I did install them as part of the Anaconda platform.

3rd Edit:
I see your point. I believe you, it most likely is a environment problem. I have only downloaded one version of Python, which is 3.10.5.

I have not downloaded any other "Pythons". I do have multiple IDEs however such as Notepad++. However, no previous versions of Python are on this computer.

You are right about me running into this problem with other packages! I am currently also attempting to pip install scikit-learn. However, when I import sklearn, I also obtain an error:

ModuleNotFoundError: No module named ‘sklearn’

Do you know how I can fix that to locate the packages for both Spyder and Jupyter? I tried to configure the settings as the previous comment said, but I’m probably not doing it correctly.

Asked By: PKrange

||

Answers:

You have multiple Python installations on your system.

The one you are accessing form the Windows command prompt is your system-wide Python installation. If you run python -V in the cmd, you should see version 3.10.5 which you said you have deployed.

At the same time, you have at least one other Python installation that came with Anaconda. If you open Anaconda prompt, you should see a (base) prefix before the regular path prompt. This prefix indicates that you are in a separate Python environment managed by conda. If you run python -V there, you should see a different Python version.

If you can confirm that the versions are different, than what happens is that you install packages in one Python environment (system-wide), while trying to use them in the other Python environment which is not aware of them.

Your Jupyter Notebook, as well as Spyder IDE, most likely, are using your base Anaconda environment. To install packages in it use Anaconda prompt, verify that you can see (base) prefix in the prompt, and use conda install or pip install in there.

To better understand how Python environments can be managed with conda, have a look here.

Answered By: wombatonfire