Python

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.

Installation

Homebrew method

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.

Python 3

brew install python

Python 2.7

brew install [email protected]

Installing Python also installs pip (and its dependency Setuptools), which is the package manager for Python. Let’s upgrade them both:

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 PATH.

Pyenv method

pyenv is a Python version manager that can manage and install different versions of Python. Works very much like rbenv for Ruby. First, we must install pyenv using Homebrew:

brew install pyenv

To upgrade pyenv in the future, use upgrade instead of install. After installing, add pyenv init to your shell to enable shims and autocompletion (use .zshrc if you’re using zsh).

echo 'eval "$(pyenv init -)"' >> ~/.bash_profile

Restart your shell to make sure the path changes take effect.

exec $SHELL

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

Use the 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 located in ~/.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

The 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 anaconda3-4.1.1 in path/to/directory:

$ 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)

pip

macOS comes with Python so there’s a chance pip is already installed on your machine.

Installation

curl https://bootstrap.pypa.io/get-pip.py > get-pip.py
sudo python get-pip.py

To verify pip is installed properly run

pip --version

If it returns a version pip was successfully installed.

Usage

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

pip freeze

Uninstall a package

pip uninstall <package>

Virtualenv

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 other 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 use virtualenv when dealing with Python applications.

Installation

To install virtualenv run:

pip install virtualenv

Usage

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):

source venv/bin/activate

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 projects.

To leave the virtual environment run:

deactivate

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 virtualenv.

Virtualenvwrapper

To make it easier to work on multiple projects that has separate environments you can install virtualenvwrapper. It’s an extension to virtualenv and makes it easier to create and delete virtual environments without creating dependency conflicts.

To install virtualenvwrapper run:

pip install virtualenvwrapper

Depending on your setup you might need to install it using sudo. Read the installation documentation for more information.

Note: virtualenvwrapper keeps all the virtual environments in ~/.virtualenv while virtualenv keeps them in the project directory.

Numpy, Scipy and Matplotlib

The NumPy, SciPy and Matplotlib scientific libraries for Python are always a little tricky to install from source because they have all these dependencies they need to build correctly.

There are two ways to install these libraries:

  • Using pip

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
sudo port install py35-numpy py35-scipy py35-matplotlib py35-ipython +notebook py35-pandas py35-sympy py35-nose

IPython

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

Installation

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]

Licenses and Attributions


Speak Your Mind