Skip to content

Instantly share code, notes, and snippets.

@nrollr

nrollr/Python.md

Last active Jun 1, 2021
Embed
What would you like to do?
Python environments in macOS

Python environments in macOS

The latest version of macOS 10.13.3 has Python 2.7.10 installed by default, yet Python has been available on macOS and previously OS X for quite a while now.

Consult the Apple's Open Source Reference Library, and browse through the various releases of the OS to find out which Python version was included). But what if you have a project which requires Python 3 ?

The following instructions will guide you through the process of:

  • installing Python 3 using Homebrew
  • running multiple Python verions as sandboxed environments

Installed version of Python

Open Terminal.app (~/Applications/Utilities) and type python --version If you execute this command in macOS 10.13.x for example, the returned output will read:

Python 2.7.10

If you execute which python via the command line, it will return the location of the program file, in this case:

/usr/bin/python

Install Python 3 with Homebrew

Great so we have access to Python 2.x, now lets add Python 3.x

Assuming you have Homebrew installed, in Terminal.app execute the following command to install Python 3 : brew install python3. After installation, run the python3 --version command to verify the exact version:

Python 3.6.4

Now run which python3 to determine the path where Homebrew installed the progam file:

/usr/local/bin/python3

At this stage we have two different version of Python available, yet both version are invoked differently :

  • to run a script with Python 2, eg. python test.py
  • to run a script with Python 3, eg. python3 test.py

Isolated Python environments using virtualenv

If you have different projects developed in either Python 2 or 3 and each requiring specific libraries, you might consider using sandboxed Python environments. This can be achieved with virtualenv

As an example we have two directories: folderA and folderB.

  • ~/folderA will be used for the Python 2 virtual environment
  • ~/folderB will be used for the Python 3 virtual environment

install virtualenv

Open Terminal.app and type pip3 install virtualenv, which should result in:

Successfully installed virtualenv-15.1.0

Create the different environments:

  • Python 2 based virtual environment via virtualenv -p python ~/folderA
  • Python 3 based virtual environment via virtualenv -p python3 ~/folderB

Activate the Python environment:

  • via source ~/folderA/bin/activate, if you wish to use the Python 2 environment
  • via source ~/folderB/bin/activate, if you wish to use the Python 3 environment

To exit an environment use deactivate Once you've activated a virtual environment, you can just use python to execute your scripts.

@SimonCarryer

This comment has been minimized.

Copy link

@SimonCarryer SimonCarryer commented Aug 19, 2018

Thanks for this! I keep forgetting how to do this, googling, and coming back to this page. This is the third or fourth time I've come back, so I figured I owed you a thank you.

@eatjeff

This comment has been minimized.

Copy link

@eatjeff eatjeff commented Nov 19, 2018

Thanks. It works well on my mac mojave 10.14.1.

@bboc

This comment has been minimized.

Copy link

@bboc bboc commented Apr 23, 2019

I found that virtualenvwrapper is really helpful when working with many virtualenvs, it brings a lot of useful commands as well as tab completion for names of environments etc.

Also interesting, especially if one wants to use several different versions of Python, is the answer to this question on stackoverflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment