Manage both Python2 and Python3 on your Mac

· 1 min read

Step 1: Install Python3

Use brew install python to install python3 on the mac, current version is Python 3.6.5. If your mac has a python2 already, brew will ask for an upgrade, which is fine.

# Going to install python3
brew install python 

# Going to upgrade
brew upgrade python

Step 2: Install Python2

Homebrew does provide a python2 version, which you can install by

# Going to install python2
brew install python@2

After finished the above two steps, now you can have both python2 and python3 installed on your Mac OSX. Now you can use:

python2 for python 2.x

python3 for python 3.x

One more thing: use virtualenv for your python2 and python3


# To use virualenv for python2 
virtualenv -p python2 env_name
# Then in the install
pip2 install lib_name

#To use virualenv for python3 
virtualenv -p python3 env_name
# Then in the install
pip3 install lib_name

Update

Sometimes you might see the following errors:

➜ git:(master) ✗ virtualenv -p python3 .venv
zsh: /usr/local/bin/virtualenv: bad interpreter: /usr/local/opt/python/bin/python3.6: no such file or directory

The reason is you when you are installing `virtualenv`, the python version is still python3.6. So you have to reinstall after you upgrade python3.6 to python3.7:

pip3 install virtualenv