Install OpenCV 4 in PyCharm

What will we cover?

You want to start you first OpenCV project in PyCharm.

import cv2

And you get.

From PyCharm

You press Install package cv2, but you get.

Error message from PyChar (lower right corner).

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).

Creating a project OpenCV in PyCharm

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?

Good question.

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.

Command line terminal

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.

Command line terminal

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
From command line terminal

You might get a bit different output, as I already had the library cached.

Back in PyCharm it will update and look like this.

Back in PyCharm the red line disappeared

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)

Which should result in something like this.

The end result

Learn Python


  • 70 pages to get you started on your journey to master Python.
  • How to install your setup with Anaconda.
  • Written description and introduction to all concepts.
  • Jupyter Notebooks prepared for 17 projects.

Python 101: A CRASH COURSE

  1. How to get started with this 8 hours Python 101: A CRASH COURSE.
  2. Best practices for learning Python.
  3. How to download the material to follow along and create projects.
  4. A chapter for each lesson with a descriptioncode snippets for easy reference, and links to a lesson video.

Expert Data Science Blueprint

Expert Data Science Blueprint

  • Master the Data Science Workflow for actionable data insights.
  • How to download the material to follow along and create projects.
  • A chapter to each lesson with a Description, Learning Objective, and link to the lesson video.

Machine Learning

Machine Learning – The Simple Path to Mastery

  • How to get started with Machine Learning.
  • How to download the material to follow along and make the projects.
  • One chapter for each lesson with a Description, Learning Objectives, and link to the lesson video.

Leave a Comment