Installing the cv package in the virtual environment reports an error: ModuleNotFoundError: No module named 'cv2'

 Problem background:

First of all, I have created a virtual environment python37 in the envs folder under the anaconda folder before, and then when I use pycharm to create a new project, I directly use this created virtual environment. At this time, I clicked into the new project folder and found that there was an additional folder teaching venv.

At first I thought that the system automatically copied the virtual environment python37 I built here, which means that this project directory has its own virtual environment, not the one I created before.

So when I installed the cv package, I installed it directly in the Scripts folder in this new virtual environment (as shown below), and entered it in the Scripts directory of this file

pip install python-opencv

Then it was installed successfully.

But when I go back to the previous level and run python according to the tutorial, and enter import cv2 or import cv2 as cv, it shows ModuleNotFoundError: No module named 'cv2'.

import cv2
import cv2 as cv

 

At that time, I was very puzzled, and I went to see that many methods on the Internet did not meet my situation, because I installed it successfully, and I can find the cv2 package in the Lib folder under the venv folder of the project (as shown in the figure)

 And I tried to enter import cv2 directly under the site-packages of the upper level folder of cv2, and found that there was no error at this time

 But at the next level, the error ModuleNotFoundError: No module named 'cv2' will be reported again.

But what the teacher demonstrated in the online class video I watched can be run directly on the D drive without error.

So I watched the video several times and found that the teacher ran pip install in the Scripts under the anaconda folder, not under the environment folder in the project.

So I found the location of the python37 virtual environment I created at the beginning, pip install it under the Scripts folder, and found that the problem was solved.

Principle thinking:

I feel that because the cv package is added to the project, it is only used by this project (non-global), so because of some attribute problems of the folder, it may only be searched in the upper level. (I tried to use it in pycharm, but it may only be searchable in its sibling directory in the cmd window)

But for the virtual environment created in anaconda, once activated, it is universal, so there will be no error in running it in any directory.

Guess you like

Origin blog.csdn.net/m0_64054405/article/details/128367713