Two-headed Snake: Setup a Separate Python3 Installation on OSX

DZone 's Guide to

Two-headed Snake: Setup a Separate Python3 Installation on OSX

Trying to override OSX's native 2.7 version of Python would cause problems; use this guide to create virtual environments for different versions of the popular language.

· Web Dev Zone ·
Free Resource

Python2 Default Setup on Mac

Default MacOS comes with latest Python 2.7. It will be pre-installed along with some pyobjc packages as well.

If you'd like to play around with python and external packages, it's best not to touch the original Python install from the System. Unfortunately, this Python (version 2.7) comes with neither pip nor virtualenv packages to help setup a separate environment! 

Download get-pip.py from https://bootstrap.pypa.io/get-pip.py, then run:

sudo python get-pip.py

Now, install VirtualEnv:

sudo /usr/local/bin/pip install virtualenv

Setup a Python2 virtual environment in a separate directory:

python -m virtualenv mypy source mypy/bin/activate
python -c 'import sys; print sys.path'

You should able to verify with the output above that you are indeed using your own setup of an isolated Python2 environment, and it even has your own pip installed list, so you won't mess up the system's version of Python2!

Setup Python3

OSX comes with 2.7, but not Python3. You can download the latest Python3 from python.org site and install it. It will give you the new ' python3' command, which will not conflict with your existing Python2. But if you want to use python commands for Python3 for your development activities, then I suggest you create a new virtual environment of your own. The Python3 install should come with a pyvenv command. For example:

pyvenv mypy3 source mypy3/bin/activate
python -c 'import sys; print(sys.path)'
python 2.7 ,python 3

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}