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 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:
$ xcode-select --install
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:
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
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
$ brew search python
you will see the available python-related packages to install, and python3 should be among them. Let’s install it!
$ brew install python3
You can check which version is installed by typing
$ python3 --version
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:
$ pyvenv myenv
$ source myenv/bin/activate
Virtualenvwrapper with Python 3
$ pip install virtualenv $ pip install virtualenvwrapper
$ mkdir ~/.virtualenvs
export WORKON_HOME=~/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh
you can activate these changes by typing
$ source .bashrc
$ mkvirtualenv --python=python3_path myenv
$ which python3
$ mkvirtualenv --python=/usr/local/bin/python3 myenv
and to activate it again
$ workon myenv
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
$ pip install Django==1.7
Now, you’re ready to code!
Please give it a +1 and share it to your friends! Thanks! 🙂
Marina Mele has experience in artificial intelligence implementation and has led tech teams for over a decade. On her personal blog (marinamele.com), she writes about personal growth, family values, AI, and other topics she’s passionate about. Marina also publishes a weekly AI newsletter featuring the latest advancements and innovations in the field (marinamele.substack.com)