Title: The perfect computer vision environment: PyCharm, OpenCV, and Python virtual environments - PyImageSearch

URL Source: https://pyimagesearch.com/2015/08/17/the-perfect-computer-vision-environment-pycharm-opencv-and-python-virtual-environments/

Published Time: 2015-08-17T14:00:51+00:00

Markdown Content:
[![Image 1: pycharm_opencv_logos](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20240'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/03/pycharm_opencv_logos.jpg)

You know what makes for a (not so) fun weekend?

Reconfiguring and reinstalling OSX on your MacBook Pro. Apparently, the 13in MacBook Pro that I use when I’m traveling decided to shit the bed.

No worries though, I use [Carbon Copy Cloner](https://bombich.com/) and [Backblaze](https://www.backblaze.com/), so no data was lost. And to be honest, I was considering rebuilding the development environment on my travel system for awhile now. While I use my 13in MacBook Pro while I travel, I have a second MacBook Pro that I use on a daily basis as my main development system. And over the past two years the development environments between the two have become horribly out of sync and almost unusable.

As I sat down Sunday night, looking out at the anvil-shaped thunderclouds rolling in across the Long Island sound, I took a second and sipped some tea (spiked with some peppermint schnapps; it is the weekend, of course) from my mug and watched as the lighting danced haphazardly across the sky.

There is a certain calming serenity that comes with watching a thunderstorm roll in — and hopefully the rest of this guide gives you some calming serenity yourself when you go to setup PyCharm to play nice with OpenCV and virtual environments.

The rest of this blog post will assume that you have **already installed OpenCV** and the appropriate Python bindings on your system. I’m also going to assume that you have [virtualenv](https://virtualenv.pypa.io/en/latest/) and [virtualenvwrapper](https://virtualenvwrapper.readthedocs.org/en/latest/) installed as well.

These installation instructions and associated screenshots were gathered on my OSX machine, but these instructions will work on both Linux and Windows (for Windows you’ll have to change the various paths to files of course, but that’s okay).

I’ll also be setting up my system with **Python 2.7** and **OpenCV 2.4.X**; however, you can use the same instructions to setup your environment with Python 3 and OpenCV as well, you’ll just need to change the paths to the Python and OpenCV files.

Step 1: Create your virtual environment
---------------------------------------

The first thing we are going to do is setup our virtual environment. Open up a terminal and create your virtual environment. For this example, let’s name the virtual environment `pyimagesearch` :

$ mkvirtualenv pyimagesearch

Now that our virtual environment has been setup, let’s install NumPy, Scipy, matplotlib, scikit-learn, and scikit-image which are all commonly used for computer vision development:

$ pip install numpy
$ pip install scipy
$ pip install matplotlib
$ pip install scikit-learn
$ pip install -U scikit-image

Step 2: Sym-link your cv2.so and cv.py files
--------------------------------------------

As I’m sure you already know, OpenCV is not pip-installable. You’ll need to manually sym-link your `cv2.so` and `cv.py` files into the site-packages directory of the `pyimagesearch` virtual environment.

On my system, OpenCV is installed in `/usr/local/lib/python2.7/site-packages/`

This may not be the case for your system, so be sure to find your OpenCV install path and make note of it — you’ll need this path for the following step.

Now that we have the path to our OpenCV install, we can sym-link it into our virtual environment:

$ cd ~/.virtualenvs/pyimagesearch/lib/python2.7/site-packages/
$ ln -s /usr/local/lib/python2.7/site-packages/cv.py cv.py
$ ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so

Step 3: Configure PyCharm
-------------------------

If you are not already using the [PyCharm IDE](https://www.jetbrains.com/pycharm/) for editing your code, it’s definitely worth a look. They have both a free community edition and a paid version with a bunch of nice bells and whistles.

It’s hard to believe, but for years I turned the other way at PyCharm and dismissed it.

You see, back in college I was forced to use Eclipse for Java development — and since I was never fond of Java or Eclipse, I (ignorantly) turned my back on any IDE that reminded me of it. Boy, that was a huge mistake.

About six months ago I decided to give PyCharm a _real_ chance and not let my previous experiences bias my opinion. In short, it was one of the best choices I’ve ever made in terms of development environments.

Anyway, now that our virtual environment is all setup let’s connect it to a PyCharm project.

Open up PyCharm and create a new “Pure Python” project:

[![Image 2: Creating a new Pure Python project in PyCharm.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20490'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/03/pycharm_new_project.jpg)

**Figure 1:** Creating a new Pure Python project in PyCharm.

From here we need to set the location of our Python Interpreter. In most cases this location will point to your system install of Python. However, we do not want to use the system Python — we want to use the Python that is part of our `pyimagesearch`  virtual environment, so click the gear icon and select “Add Local”:

[![Image 3: Figure 2: Specifying that we want to use a local Python environment rather than the default system Python.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20477'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/03/pycharm_add_local.jpg)

**Figure 2:** Specifying that we want to use a local Python environment rather than the default system Python.

Next up, we need to specify the path to the **Python binary** **inside our virtual environment.**

In my case, the `pyimagesearch`  virtual environment is located in `~/.virtualenvs/pyimagesearch/` with the actual Python binary in `~/.virtualenvs/pyimagesearch/bin` .

In any case, be sure to navigate to the Python binary for your virtual environment, followed by clicking “Choose”:

[![Image 4: Figure 3: Selecting the pyimagesearch Python binary.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20490'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/03/pycharm_select_path.jpg)

**Figure 3:** Selecting the pyimagesearch Python binary.

After you have selected your virtual environment, PyCharm will spend a few seconds updating the project skeletons:

[![Image 5: Figure 4: PyCharm updating the project skeletons for our computer vision virtual environment.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20480'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/03/pycharm_updating_skeletons.jpg)

**Figure 4:** PyCharm updating the project skeletons for our computer vision virtual environment.

Once the skeletons are done updating, click the “Create” button to launch your project.

And that’s all there is to it!

Done!
-----

After this, you’re all set. PyCharm will use your `pyimagesearch`  virtual environment and will recognize the OpenCV library.

And if you ever want to update the virtual environment for a project you are using, just to the PyCharm Preferences panel, select the Project tab on the left sidebar, followed by “Project Interpreter”:

[![Image 6: Figure 5: Updating the virtual environment for a project already created with PyCharm.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20367'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/03/pycharm_preferences.jpg)

**Figure 5:** Updating the virtual environment for a project already created with PyCharm.

### What's next? We recommend [PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend).

**Course information:**  
84 total classes • 114+ hours of on-demand code walkthrough videos • Last updated: February 2024  
★★★★★ 4.84 (128 Ratings) • 16,000+ Students Enrolled

**I strongly believe that if you had the right teacher you could _master_ computer vision and deep learning.**

Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?

That’s _not_ the case.

All you need to master computer vision and deep learning is for someone to explain things to you in _simple, intuitive_ terms. _And that’s exactly what I do_. My mission is to change education and how complex Artificial Intelligence topics are taught.

If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to _successfully_ and _confidently_ apply computer vision to your work, research, and projects. Join me in computer vision mastery.

**Inside PyImageSearch University you'll find:**

*   ✓ **84 courses** on essential computer vision, deep learning, and OpenCV topics
*   ✓ **84 Certificates** of Completion
*   ✓ **114+ hours** of on-demand video
*   ✓ **Brand new courses released _regularly_**, ensuring you can keep up with state-of-the-art techniques
*   ✓ **Pre-configured Jupyter Notebooks in Google Colab**
*   ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
*   ✓ Access to **centralized code repos for _all_ 536+ tutorials** on PyImageSearch
*   ✓ **Easy one-click downloads** for code, datasets, pre-trained models, etc.
*   ✓ **Access** on mobile, laptop, desktop, etc.

[Click here to join PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend)

Summary
-------

In this blog post I showed you how to utilize virtual environments and OpenCV inside my favorite IDE, [PyCharm](https://www.jetbrains.com/pycharm/).

I hope you find this post useful when you go to setup your next Python + OpenCV project!

And if you aren’t using PyCharm yet, it’s definitely worth a look.

![Image 7](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20410%20567'%3E%3C/svg%3E)

#### Join the PyImageSearch Newsletter and Grab My FREE 17-page Resource Guide PDF

Enter your email address below to **join the PyImageSearch Newsletter** and **download my FREE 17-page Resource Guide PDF** on Computer Vision, OpenCV, and Deep Learning.