35

I want to run tests with multiple Python versions on OS X 10.11, including:

  • Python 2.6 - ?!
  • Python 2.7 - default - solved
  • Python 3.4 - ?!
  • Python 3.5 - installed via brew - works well
  • Conda Python 3.5 - ?!

I want to run the tests via tox so tox needs to be able to find them. Sadly it seems that brew doesn't want to install 3.4 since they added 3.5 and I obviously do not want to remove 3.5 one.

bfontaine
  • 14,702
  • 12
  • 64
  • 87
sorin
  • 137,198
  • 150
  • 472
  • 707
  • 2
    This may help: [Is there a python equivalent of Ruby's 'rvm'?](http://stackoverflow.com/questions/2812471/is-there-a-python-equivalent-of-rubys-rvm) – Jonathan Lonowski May 01 '16 at 15:34

5 Answers5

24

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.

user2943160
  • 499
  • 5
  • 12
  • Note that detox is [allegedly unmaintained](https://pypi.org/project/detox/) at this point. – jarmod Dec 01 '20 at 16:10
20

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
Dogweather
  • 12,603
  • 15
  • 54
  • 73
8

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.

Acumenus
  • 41,481
  • 14
  • 116
  • 107
  • Do you mind sharing what bash_profile should look like if i follow your answer https://stackoverflow.com/questions/66849122/setting-up-python? And also, to navigate between the versions ill need to `brew unlink python@3.8` & `brew link --force python@3.9` – kd12345 Mar 29 '21 at 08:37
  • @kd12345 You do not have to unlink or link to "navigate between versions". Instead use virtual environments for your projects. – Acumenus Apr 29 '21 at 01:12
1

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

flybonzai
  • 3,165
  • 7
  • 28
  • 62
  • 1
    This is not an option because it works only with conda and I am looking for something that works with more than conda. – sorin May 12 '16 at 21:30
-2

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.

Carlos
  • 1,799
  • 3
  • 19
  • 34
  • 2
    Pipenv can only use the versions of python that are already installed, so that does not help with the question. – Toby Mar 13 '19 at 07:34