What will we cover?
You want to start you first OpenCV project in PyCharm.
And you get.
You press Install package cv2, but you get.
What to do? No worries. We will cover that in this survival guide and it is not complex.
If you only want the solution go to Step 3.
Or see the tutorial on YouTube.
Step 1: Understand how PyCharm works with a Virtual Environment
When you create a new project in PyCharm you get promoted by this screen (PyCharm 2020.2).
It says Python interpreter: New Virtualenv environment. What does that mean?
Well, it creates a isolated environment to have your project in. Then each project can have it own dependencies and libraries without impacting other projects.
Remember kindergarten? There was only one sandbox, and there was not enough for multiple projects in it. Like building a sand castle, making a river, and what ever you did as kid. The problem was, if you wanted to build a castle while your fellow kindergarten friends wanted to play mountain collapse (you know when a mountain collapses). Then their game would destroy your well engineered 5 feet tall castle. It was the beginning of a riot.
Think of a kindergarten where there is one sandbox for each project you could image. One for castle building. One for mountain collapse. You see. Now everyone can play in their own world or environment.
The virtual environment is like that. You can go crazy in it without destroying other awesome projects you do. Hence, if you feel like making a mountain collapse project, you should not fear it will destroy your well engineered castle project.
Step 2: How does this virtual environment work, and why does it matter for OpenCV?
If you follow some manual online you might end up installing OpenCV on your base system and not in the virtual environment in your project.
But where is the virtual environment located. It depends on two things. First, where are your projects located. Second, what is the name of your project.
I used the default location when I installed PyCharm, which is PyCharmProjects in my home folder. Further, in this case I called the project OpenCV.
If I open a command line I can type the following to get to the location.
Then you will see a folder called venv, which is short for virtual environment. Go into that folder and follow down in the bin (binary) folder.
Now you are located where you can install the OpenCV library.
Step 3: Method 1 – Install OpenCV library in your virtual environment
Go to PyCharm menu and choose Preferences…
On the left side find Project (with the name of the project you are working on) and choose subitem Python Interpreter.
Press the little plus-sign in the bottom of the window and an install will show .
Write opencv-python in the window that opens and press Install
And you are ready.
If it worked (no read line under cv2) then skip ahead to Step 5 to try it out.
Step 4: Method 2 (if Method 1 fails) Install the OpenCV library in your virtual environment
Use pip is the package manager system for Python. You want to ensure you use the pip from the above library.
./pip install opencv-python
You might get a bit different output, as I already had the library cached.
Back in PyCharm it will update and look like this.
Now you are ready for your first test.
Step 5: Testing that OpenCV works
Let’s find a picture.
Download the above image and save it as Castle.png in your project folder.
import cv2 img = cv2.imread("Castle.png") gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.imshow("Over the Clouds", img) cv2.imshow("Over the Clouds - gray", gray) cv2.waitKey(0) cv2.destroyAllWindows()
Which should result in something like this.