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 and Backblaze, 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.
PyCharm, virtual environments, and OpenCV
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 and virtualenvwrapper 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 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:
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”:
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”:
After you have selected your virtual environment, PyCharm will spend a few seconds updating the project skeletons:
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”:
What's next? I recommend PyImageSearch University.
30+ total classes • 39h 44m video • Last updated: 12/2021
★★★★★ 4.84 (128 Ratings) • 3,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:
- ✓ 30+ courses on essential computer vision, deep learning, and OpenCV topics
- ✓ 30+ Certificates of Completion
- ✓ 39h 44m on-demand video
- ✓ Brand new courses released every month, 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 500+ tutorials on PyImageSearch
- ✓ Easy one-click downloads for code, datasets, pre-trained models, etc.
- ✓ Access on mobile, laptop, desktop, etc.
Summary
In this blog post I showed you how to utilize virtual environments and OpenCV inside my favorite IDE, 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.
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.