macOS, like Linux, ships with Python already installed. But you don’t want to mess with the system Python (some system tools rely on it, etc.), so we’ll install our own version(s). There are two ways to install Python, (1) Homebrew and (2) Pyenv. If you plan to use multiple versions of Python (e.g. 2, 3, and anaconda) then you should use pyenv.
Python 3 is the default version when installing with Homebrew, so if you want to install Python 2.7 you’ll have to be explicit about it.
brew install python
brew install [email protected]
pip install --upgrade setuptools pip install --upgrade pip
Executable scripts from Python packages you install will be put in
/usr/local/share/python, make sure it’s on your
pyenv is a Python version manager that can
manage and install different versions of Python. Works very much like
for Ruby. First, we must install
pyenv using Homebrew:
brew install pyenv
pyenv in the future, use
upgrade instead of
pyenv init to your shell to enable shims and autocompletion
.zshrc if you’re using
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
Restart your shell to make sure the path changes take effect.
You can now begin using
pyenv. To list the all available versions of Python,
including Anaconda, Jython, PyPy and Stackless, use:
pyenv install --list
Then install the desired versions:
pyenv install 2.7.12 pyenv install 3.5.2
global command to set global version(s) of Python to be used in all
shells. For example, if you prefer 2.7.12 over 3.5.2:
pyenv global 2.7.12 3.5.2 pyenv rehash
The leading version takes priority. All installed Python versions can be
~/.pyenv/versions. Alternatively, you can run:
$ pyenv versions system (set by /Users/your_account/.pyenv/version) * 2.7.12 * 3.5.2
This shows an asterisk
* next to the currently active version.
Application-specific Python version
local command will set local application-specific Python version(s) by
writing the version name to a
.python-version file in the current directory.
This version overrides the global version. For example, to install
$ pyenv install anaconda3-4.1.1 $ cd path/to/directory $ pyenv local anaconda3-4.1.1 $ pyenv rehash $ pyenv versions system 2.7.12 3.5.2 * anaconda3-4.1.1 (set by /Users/your_account/path/to/directory/.python-version)
macOS comes with Python so there’s a chance
pip is already installed on your machine.
curl https://bootstrap.pypa.io/get-pip.py > get-pip.py sudo python get-pip.py
pip is installed properly run
If it returns a version
pip was successfully installed.
Here are a few
pip commands to get you started.
Install a Python package
pip install <package>
Upgrade a package
pip install --upgrade <package>
See what’s installed
Uninstall a package
pip uninstall <package>
Virtualenv is a tool that lets you create an
isolated Python environment for your project. It creates an environment that
has its own installation directories, that doesn’t share dependencies with
virtualenv environments (and optionally doesn’t access the globally
installed dependencies either). You can even configure what version of Python
you want to use for each individual environment. It’s very much recommended to
virtualenv when dealing with Python applications.
pip install virtualenv
If you have a project in a directory called
my-project you can set up
virtualenv for that project by running:
cd my-project/ virtualenv venv
If you want your
virtualenv to also inherit globally installed packages run:
virtualenv venv --system-site-packages
These commands create a
venv/ directory in your project where all
dependencies are installed. You need to activate it first though (in every
terminal instance where you are working on your project):
You should see a
(venv) appear at the beginning of your terminal prompt
indicating that you are working inside the
virtualenv. Now when you install
something like this:
pip install <package>
It will get installed in the
venv/ folder, and not conflict with other
To leave the virtual environment run:
Important: Remember to add
venv to your project’s
.gitignore file so
you don’t include all of that in your source code.
It is preferable to install big packages (like Numpy), or packages you always
use (like IPython) globally. All the rest can be installed in a
To make it easier to work on multiple projects that has separate environments
you can install
virtualenvwrapper. It’s an extension to
makes it easier to create and delete virtual environments without creating
pip install virtualenvwrapper
Depending on your setup you might need to install it using
sudo. Read the
for more information.
virtualenvwrapper keeps all the virtual environments in
virtualenv keeps them in the project directory.
Numpy, Scipy and Matplotlib
There are two ways to install these libraries:
Python provides an inbuilt package management system
pip which can be used to install these libraries
python -m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nose
- Using MacPorts and Python 3.5
sudo port install py35-numpy py35-scipy py35-matplotlib py35-ipython +notebook py35-pandas py35-sympy py35-nose
IPython is an improved Python shell than the one you get from running
python in the command-line. It has many cool functions (running Unix commands from the Python shell, easy copy & paste, creating Matplotlib charts in-line etc.). You can find all functions in the documentation.
IPython is a kernel for Jupyter Notebook. If you are confused IPython with Jupyter, refer to the Jupyter documentation
pip install ipython
If you want a more fine-grained command you can try the following:
For zsh ->
pip install 'ipython[zmq,qtconsole,notebook,test]'
For bash ->
pip install ipython[zmq,qtconsole,notebook,test]