Install Python 3 on Mac OS X and use virtualenv and virtualenvwrapper

python3This post explains how to install Python 3 in a Mac OS X, both Mavericks and Yosemite. Check this post if you want to clean install Python 2.7. You can also install both! šŸ™‚

It also shows how to use virtualenv and virtualenvwrapper with Python 3. Don’t miss it!

Like with Python 2.7, we need to install first Xcode and Homebrew.

Install Xcode and Homebrew

First of all, installĀ XcodeĀ if you don’t have it already. You can find it in the Apple Store.

Next, we need to install theĀ Command Line ToolsĀ of Xcode. Open a Terminal and type:

this should trigger a pop-up window that will ask you to install the Command Line Tools. If you have some trouble installing these tools, you might find useful this post onĀ Stackoverflow.

Next, we need to installĀ Homebrew. In the Terminal, type this command line:

Now, we need to insert the Homebrew directory at the top of the PATH environment variable. In this way, some Homebrew installations will take precedence over stock OS X binaries. Open or create the file ~/.bash_profile and write:

Close your Terminal and open it again to make these changes effective.

Install Python 3

If you type

you will see the available python-related packages to install, and python3 should be among them. Let’s install it!

You can check which version is installed by typing

And you can open it with:

Moreover, when you install python with Homebrew, you also install:

  • the corresponding pip package manager, which is called pip3
  • the corresponding Setuptools
  • pyvenv, and alternative to virtualenv — cool!!

Create Virtual environments with pyvenv

Now that you have Python3 you also have pyvenv, a tool to create virtual environments (similar to virtualenv). However, there is one important remark about the version of pyvenv you have installed: only if you installed Python 3.4 or latter, pyvenv will also install pip when creating a new virtual environment.

Let’s create a new virtual envirnoment, named myenv, using pyvenv:

This will create a folder named myenv in the current directory. To activate this environment just type:

and you can start Python 3 by just typing:

Note that as you are inside the virtual environment, you don’t need to use the command python3 to open Python 3.

Virtualenvwrapper with Python 3

Many of you will be familiar with virtualenvwrapper, a nice tool to manage more easily your virtual environments.To install it, we will first install virtualenv:
Next, create a folder that will contain all your virtual environments:
Open your .bashrc file and add:
you can activate these changes by typing
We are ready to create a new virtual environment using Python 3 with
where python3_path is the path of python3, which can be found with
in my case it was
This creates a folder myenv inside the environments folder ~/.virtualenvs.The new environment will be active after running the previous command. To deactivate it, just type:
and to activate it again
While being in your python3 virtual envirnoment, if you type
you activate python 3! Moreover, you can use pip to call pip3 and install python3 packages.

For example, you can installĀ Django 1.7 using

Now, you’re ready to code!

Please give it aĀ +1 and share it to your friends! Thanks! šŸ™‚

Google+TwitterLinkedInFacebookReddit

Please, add +Marina Mele in your comments. This way I will get a notification email and I will answer you as soon as possible! :-)