Installing Python 2 and Python 3 on OS X | Getting them straight

For the life of me I’ve installed competing Python environments too many times so I want to get this down in one place so I can come back to it when I inevitably need do this config again.

Assuming you’re in a year sometime in the future when Python 2 is still installed by default on OS X, you’ll want to get Python 3 up and running. Even though you might be, like me, tethered to Python 2.7 for some odd reason. If you want to run both versions, and you want to remember how to run in protected, virtual environments but you keep forgetting how to do it — this is the guide for you.

The basis for all these instructions are from the Python master at Guide to Python three posts: 1 2 3. I am but a mere Python mortal so if anything I typed below is bad, go to the source posts for the real truth.


0/ You may need to install Xcode command line tools to start

%terminal xcode-select --install

1/ Install Homebrew

terminal% ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2/ Add it to your path by tacking this to the bottom of your ~/.login

export PATH="/usr/local/opt/python/libexec/bin:$PATH"

3/ To ingest that new command, type ‘source ~/.login’

terminal% 'source ~/.login'

4/ Now install Python 3 as both python3 and python (it will become your default)

terminal% brew install python

5/ Test out that your Python 3 has been installed by asking what its version is

terminal% python3 --version
Python 3.7.3 (<-- This is my version in summer of 2019)
terminal% python --version
Python 3.7.3

Sign up for the Resilience Tech Briefing with no more than 2021 characters, zero images, and all in plain-text.

Processing…
Success! You're on the list.

6/ Panic a little when you realize that Python 2.7 isn’t running on your system, but then recover quickly by re-installing Python 2

terminal% brew install python@2

7/ Get a little happy when you realize it’s really there

terminal% python2 --version
Python 2.7.16 (<-- This is my version in summer of 2019)

8/ Tack a new path on to your default path in your ~/.login file and source it after you’ve saved the file

export PATH="/usr/local/opt/python@2/libexec/bin:$PATH"

9/ Take note there’s two pips now that correspond to the two pythons running and note you’ll get different stuff back probably when you type ‘-V’s

terminal% pip2 -V
pip 19.1.1 from /usr/local/lib/python2.7/site-packages/pip (python 2.7)
terminal% pip -V
pip 19.1.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)

10/ Let’s set up virtual environments so you don’t have to sudo anymore. You’ll first need to do something a bit contorted because virtualenv comes pre-installed with Python 3

terminal% python -m site --user-base

11/ Take the results of the user-base directory command and add a /bin to the end. Then add it to your ~/.profile and source it

export PATH="/Users/maeda/Library/Python/3.7/bin:$PATH" # mine looked like this

12/ You now have pipenv and virtualenv ready to go from your command line. The command pipenv makes you feel a bit like running npm and manages your dependencies from the Requests library. If it’s not there, do this beforehand “pip install –user pipenv”

terminal% pipenv # reveals all the options

13/ This is an unlucky numbered step but still a beautiful one if it’s there, and if it’s not do this beforehand: “pip install virtualenv”

terminal% virtualenv --version # will give you back a version number if it's there

Or you might take Ryan Herr’s advice instead



Running Python 3

To get your environment setup

terminal% cd your_project_directory
terminal% virtualenv venv

Then to activate the environment

terminal% source venv/bin/activate

And when you run python it’ll all be in that environment! When you’re done

terminal% deactivate

If you ever want to get rid of the virtualenv, just delete the special directory it creates

terminal% rm -rf venv

To install a library to be used locally to your machine and to avoid a sudo

terminal% pip install -user <packagename>

Running Python 2

To get your environment setup, only one step is different with the rest all exactly the same

terminal% cd your_project_directory
terminal% virtualenv -p /usr/bin/python2.7 venv

Then to activate the environment

terminal% source venv/bin/activate

And when you run python it’ll all be in that environment! When you’re done

terminal% deactivate

If you ever want to get rid of the virtualenv, just delete the special directory it creates

terminal% rm -rf venv

To install a library to be used locally to your machine and to avoid a sudo (note the pip2)

terminal% pip2 install -user <packagename>

2 Comments

Got hints for VPython (which if you haven’t tried it already, give you a full-featured 3-d geometric environment with the simplicity of python).