Solve the download problem of the CV2 library in pycharm, as well as the installation of Miniconda and the configuration in pycharm

Solve the download problem of the CV2 library in pycharm, as well as the installation of Miniconda and the configuration in pycharm


This article is about the problems I encountered when using pycharm for image processing when I was learning image processing and machine vision. The pycharm version used in this article is the 2021.3 Chinese version.

Solve CV2 package problem

When learning image processing and machine vision, pycharm is used to sample and quantify images. When importing CV2 is entered in pycharm, it shows that there is no CV2 package. After choosing to install the CV2 installation package, it prompts that the installation fails. The problem is in the picture below.
import CV2 error
Installation package download failed
If the environment you are using is a python interpreter, open pycharm, select File –> Settings –> Project –> python interpreter
Here we can see some packages that have been installed
python interpreter
If your python interpreter column is empty Next, you need to click on the settings on the right, select add–>existing environment,
pycharm add environment
select the icon in the red box, and find the python.exe path of your python interpreter similar to the blue box. If you have not downloaded the python interpreter, Download one and proceed. click here to download

After completing the above steps, click the plus sign above the software package to add the software package, and some versions are on the right.
pycharm add package
Enter opencv in the search bar, select opencv-python and opencv-contrib-python, and install them in turn.
install package
Returning to the python editor interface, we can find that two software packages have been added to the library.
Installed
At this time, we return to the code page, and we can find that import cv2 no longer reports an error.
Error resolution
It is worth noting that the cv2 after import must be in lowercase letters. If it is an uppercase letter, it will still report an error. This point has stuck with me for a long time, and it was not resolved until I discovered it by accident. If you fail to install the above two opencv software packages, you can install them several times, and update the pip software package in the library to the latest version and try again.
update package

Miniconda installation and configuration in pycharm

Let's explain the second solution, install the conda environment for pycharm. This is also a method I recommend. I personally think that conda is more useful and comprehensive than the python interpreter. So don't be afraid of trouble, if you can install it or install it, it will save a lot of trouble in the future.
First of all, we are going to Miniconda's official website to download him. I downloaded the py3.9 version here. Click here
to download After downloading, we double-click to run the installation package, pay attention to select all the two options below .
Miniconda installation
After the installation is complete, we run cmd and enter conda in the command prompt window. A long list pops up, indicating that our installation is successful.
insert image description here
Then we enter the following code and run

conda creat -n python=3.9

insert image description here
Here we enter y and press Enter, and
insert image description here
this page appears, which means that we have successfully created it.
Next, we open pycharm, select File->Settings->Python Interpreter,
insert image description here
select Add
insert image description here
, click on the Conda environment here, select the existing environment, and click the icon with three dots on the right to add the interpreter
insert image description here
path. You can refer to me, which is in The folder where you installed conda, open the envs folder, find the hello folder we just created, find python.exe and double-click it.
insert image description here
Here you can see that we have successfully added the conda environment.
insert image description here
Next, we choose to add a package and install the py-opencv package.

Guess you like

Origin blog.csdn.net/weixin_50472671/article/details/126798745