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.
Python for Finance: Unlock Financial Freedom and Build Your Dream Life
Discover the key to financial freedom and secure your dream life with Python for Finance!
Say goodbye to financial anxiety and embrace a future filled with confidence and success. If you’re tired of struggling to pay bills and longing for a life of leisure, it’s time to take action.
Imagine breaking free from that dead-end job and opening doors to endless opportunities. With Python for Finance, you can acquire the invaluable skill of financial analysis that will revolutionize your life.
Make informed investment decisions, unlock the secrets of business financial performance, and maximize your money like never before. Gain the knowledge sought after by companies worldwide and become an indispensable asset in today’s competitive market.
Don’t let your dreams slip away. Master Python for Finance and pave your way to a profitable and fulfilling career. Start building the future you deserve today!
Python for Finance a 21 hours course that teaches investing with Python.
Learn pandas, NumPy, Matplotlib for Financial Analysis & learn how to Automate Value Investing.
“Excellent course for anyone trying to learn coding and investing.” – Lorenzo B.