When I start Python development on a new Apple, I immediately hit two problems:
- I need a version of Python that’s not installed.
- I need to install a bunch of packages from PyPI for ProjectA and a different bunch for ProjectB.
Virtualenv is not the answer! That’s the first tool you’ll hear about but it only partially solves one of these problems. You need more. There are a ton of tools and a ton of different ways to use them. Here’s how I do it on Apple’s Mac OS X.
If you’re asking questions like, “Why do you need multiple versions installed? Isn’t latest enough?” or “Why not just
pip install all the packages for ProjectA and ProjectB?” then this article probably isn’t where you should start. Great answers to those questions have already been written. This is just a disambiguation page that shows you which tools to use for which problems and how to use them.
Installing Python Versions
Note: You can use homebrew to install other versions of Python, but only a single version of Python 2 and a single version of Python 3 at a time. You can’t easily switch between two projects each frozen at 3.4 and 3.6 (for example). There’s also a limited list of versions available.
$ brew update $ brew install pyenv
Ensure pyenv loads when you login by adding this to
$ eval "$(pyenv init -)"
Activate pyenv now by either closing and re-opening Terminal or running:
$ source ~/.profile
List which versions are available and install one:
$ pyenv install --list $ pyenv install 3.6.4
If the version you wanted was missing, update pyenv via homebrew:
$ brew update $ brew upgrade pyenv
If you get weird errors about missing gcc or zlib, install the XCode Command Line Tools and try again:
$ xcode-select --install
I always set my global (aka default) version to the latest 3:
$ pyenv global 3.6.4
Update 2018-10-23: If I need several versions available, for example to run tests in tox:
$ pyenv global 3.6.4 3.7.0
Setting these makes versioned Python commands available:
$ python3.6 --version $ python3.7 --version
Pyenv has lots of great features, like support for setting a different version whenever you’re in a specific directory. Check out its commands reference.
Installing PyPI Packages
In the old days, virtualenv was always the right solution. Today, it depends on the version of Python you’re using.
Python <= 3.3 (Including 2)
This is legacy Python, when environment management wasn’t native. In these ancient times, you needed a third party tool called virtualenv.
$ pyenv global 2.7.14 $ pip install virtualenv $ virtualenv ~/my_env $ source ~/my_env/bin/activate (my_env) $ pip install
This installs the virtualenv Python package into the root environment for the legacy version of Python I need, then creates a virtual Python environment where I can install project-specific dependencies.
Python >= 3.3
Note: Virtualenv works with newer versions of Python, but it’s better to use a core library than to add a dependency. I only use the third party tool when I have to.
$ pyenv global 3.6.4 $ python -m venv ~/my_env $ source ~/my_env/bin/activate (my_env) $ pip install