This 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 HomebrewFirst 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 3If 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:
Create Virtual environments with pyvenvNow 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 3Many 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:
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:
For example, you can install Django 1.7 using Now, you’re ready to code! |
|