I want to run tests with multiple Python versions on OS X 10.11, including:
pyenv
is all well and good but I feel that we should give a mention to the wonderful pipenv
library from Kenneth Reitz.
https://github.com/pypa/pipenv
It provides the functionality of pyenv plus dependency locking, support for .env
out-of-the-box and much more.
This blog post suggests using pyenv
with the desired detox
. The basic setup with brew
requires:
brew install pyenv pyenv-virtualenv pyenv-virtualenvwrapper
Then installing the desired Python versions with pyenv install [version]
, rather than installing Python using brew
. You can check the available versions using pyenv versions
.
Finally, pip install detox
will ensure you've got tox
and detox
installed. Then you should be able to specify the desired testing versions in your tox.ini
.
brew
alone has been sufficient for me to use multiple versions of Python. I haven't strictly needed pyenv
or conda
for it.
To install various versions using brew
, run commands such as:
brew install python@3.8
brew install python@3.9
When creating your virtual environments, create them using one of:
/usr/local/opt/python@3.8/bin
/usr/local/opt/python@3.9/bin
I would avoid using /usr/local/bin/python3
when creating a virtual environment because the version that it points to can change.
pyenv
is the thing you want. It works very very well:
pyenv lets you easily switch between multiple versions of Python. It's simple, unobtrusive, and follows the UNIX tradition of single-purpose tools that do one thing well. This project was forked from rbenv and ruby-build, and modified for Python.
https://github.com/pyenv/pyenv
Install it via Homebrew:
$ brew update
$ brew install pyenv
It handles the download, compilation, and installation of various pythons for you, e.g.:
$ pyenv install 3.7.2
It can show you which versions you've installed, and which is active:
$ pyenv versions
system
3.6.7
* 3.7.2
When you're in a new project directory, just tell pyenv which python version to use there:
$ pyenv local 3.6.7 # Because e.g. tensorflow isn't compat. with 3.7 :-(
You can set a 'default' version everywhere else:
$ pyenv global 3.7.2
I'd highly recommend using a package manager such as Anaconda
, https://www.continuum.io/downloads, which it makes it trivially easy to install different self-contained virtual-envs
.
For example, to create a virtual environment with numpy
and Python 2.7
this is the command:
conda create --name py2_env numpy python=2.7
And then to switch to that environment:
source activate py2_env