Setting Up Your Python Environment with Pip, VirtualEnv and PyCharm (Mac)

This tutorial will cover setting up a Python Environment from scratch on a Mac.

Install Python

Python comes installed on the latest Mac OS versions. To see if you already have Python installed, type Command+SpaceBar to bring up search, and search for “Terminal”. Locate your Python installation on the command line by typing:
$ which python

If a path to python shows up (likely /usr/bin/python), you have python already and do NOT need to install it. If you saw no output, you need to install python. You are welcome to use either Python 2 or Python 3, but for the sake of this tutorial we are using Python 2.7. Download Python 2.7 for Mac from the official Python website here. Once the download is done, click on the package and follow along with the install wizard.

Install Pip

Pip is a package manager. A package manager is a tool that is used to keep your third-party libraries managed in a simple and orderly way on your machine.

Go to to download the script The website should prompt you to save the file. Save it to your Downloads directory.

On the command line, navigate to your Downloads directory and use python to run the script.

$ cd ~/Downloads
$ sudo python

After the installation has finished, run

$ pip freeze

If you see a list of contents, or nothing at all, that’s good! That means that you successfully ran the pip command. If you see a bunch of errors, that’s bad.

FYI: The command we just ran, pip freeze is how you ask pip to show you all of the packages that you have installed. That’s why it’s ok if you saw nothing when you ran the command. That just means you haven’t installed any packages yet.

Congrats! We now have the ability to easily install third-party packages.

Install VirtualEnvWrapper

Even though we won’t do it today, it is common to work on many python projects at the same time. You might need to install a lot of packages for one project, but only need a few packages for another. Even worse, you might need one version of a package for one project, and a different version for another.

This could potentially be a real headache. Take this example:
You have project CatsBlogs which is using the package Django version 1.5. You have another project, DogsBlog, which is on Django 1.8. Every time you want to work on CatsBlog, you have to downgrade Django to 1.5… but then if you want to work on DogsBlog, you have to upgrade it back to 1.8! Ugghhhh.

Fortunately, we have a solution to this problem, and that solution is virtual environments. A virtual environment will keep all of your packages for each project isolated from each other, allowing you to install multiple versions of multiple packages on your computer at the same time, without them colliding with each other.

Let’s install our virtual environment software, and we’ll go through an example.

Use pip, our handy package manager, to download the virtualenvwrapper package.

$ sudo pip install virtualenvwrapper --ignore-installed six

Next, use our new package to create a new virtual environment for this tutorial. I’m going to name my virtual environment “myenv”.

$ export WORKON_HOME=~/Envs
$ mkdir -p $WORKON_HOME
$ source /usr/local/bin/
$ mkvirtualenv myenv

Notice how your command line now has (myenv) showing on each line. This is the command line telling me that I am now inside of my "myenv" virtual environment.

Also note that your output told you where your new python executable is. Mine looks like this:

This path is very important and we will need it later. So take a mental note of where it is.

Now that we have a virtual environment, let's look around a bit to help us get comfortable with how it works. While we're here inside the virtual environment, let's install another package with pip. We'll install the package tweepy.

$ pip install tweepy

(Note: no sudo! Sudo is not needed inside the virtualenv.)

Now do a pip freeze. Notice that tweepy is in there now!

Now turn your virtual environment off.

$ deactivate

Notice that the "(myenv)" has disappeared from your command line. This means you are no longer inside of your virtual environment.

Now type "pip freeze". Notice that tweepy is gone! This is because we are now outside of the virtual environment. Tweepy is installed only inside our virtual environment.

Now turn your virtual environment back on.

$ workon myenv

Type "pip freeze" again from inside of the virtual environment and see that tweepy is back again!

Congrats again! We now have the ability to keep our packages contained and safe for each project we make.

Setting up PyCharm

PyCharm is an Integrated Development Environment, which is used to write python code.

If you don't have PyCharm yet, download the free Community edition here.

Our first order of business is to get PyCharm set up to use our new virtual environment, so that we can use the packages that we install with pip.

Open up PyCharm and create a new project.

Navigate to:
PyCharm > Preferences > Project > Project Interpreter

Remember earlier when I told you to remember where the python executable for your virtual environment is? Mine was:

Click the "..." icon in the top-right corner. Select "Add Local". This is going to let us point PyCharm to the version of Python that is inside of our virtual environment. Navigate to the python that is in your virutal environment. (Again, mine is at: /Users/michelle/Envs/myenv/bin/python. Yours should be a similar location.)
Screen Shot 2016-06-29 at 12.12.35 AM

Click "OK".

If you did this correctly, you will now see your virtual environment selected in the Python Interpreter window.
Screen Shot 2016-06-29 at 12.14.22 AM

Now, let's test and make sure that we can access our python packages from inside of PyCharm! Remember that the package that we installed was called tweepy. Let's try to import it in PyCharm.

In PyCharm, right-click the name of your project (mine is "tutorial" in the screenshot) and choose "New > Python File". Name it

Just to test that our package is available to us, type "import tweepy". If PyCharm underlines your import statement with red squiggles, that means that PyCharm cannot find the package, and your setup was not successful. If PyCharm greys out your import statement like in the screenshot below, that means that PyCharm has successfully found your package and is now just waiting to use it!
Screen Shot 2016-06-29 at 12.15.46 AM

Congrats! You can now access you packages from inside of PyCharm.