sorin
sorin

Reputation: 170340

How can I install multiple versions of Python on latest OS X and use them in parallel?

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

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.

Upvotes: 98

Views: 98911

Answers (9)

Dogweather
Dogweather

Reputation: 16769

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 tensorflow isn't compat. with 3.7 :-(

You can set a 'default' version everywhere else:

$ pyenv global 3.7.2

UPDATE, AUG 2024: I've gradually switched over to asdf because I work with a lot of different languages. The asdf project also publishes new version recipes very quickly. The asdf CLI interface is a little clunkier (verbose) because it can handle all languages. But pyenv never let me down for all the years I used it. If I was only doing Python development I might still be using it.

Upvotes: 104

Ngen CMS
Ngen CMS

Reputation: 156

Install pyenv and then use pyenv to switch between different versions, setting this in .zshrc should solve all the problem eval "$(pyenv init --path)"

Upvotes: 0

Frank Wu
Frank Wu

Reputation: 141

My understanding below may help manage different versions of python:

      python3.10 -> python3.10 has its own pip module 
                -> how to install?
                  curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
                  sudo  /opt/local/bin/python3.10  get-pip.py
                      -> package is installed based on this pip, it belong to python3.10
                        /opt/local/bin/python3.10 -m pip install pandas
                              -> package location
                              /Users/frank/Library/Python/3.10/lib/python/site-packages

      python3.9 -> python3.9 has its own pip module 
                -> how to install?                  
                  curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
                  sudo  /opt/local/bin/python3.9  get-pip.py
                      -> package is installed based on this pip, it belong to python3.9
                          /opt/local/bin/python3.9 -m pip install pandas
                              -> package location
                                /Users/frank/Library/Python/3.9/lib/python/site-packages

Upvotes: 2

Andrew Moon
Andrew Moon

Reputation: 79

I strongly recommend DO NOT USE pyenv in most cases. You will face deep problems with pyenv - check this: https://stackoverflow.com/a/66797993/10849913

Upvotes: 5

Asclepius
Asclepius

Reputation: 63272

brew alone has been sufficient for me to use multiple versions of Python. I haven't needed pyenv or conda for it.

To install various versions using brew, run commands such as:

brew install [email protected]
brew install [email protected]

When creating a virtual environment, create it using one of:

/usr/local/opt/[email protected]/bin
/usr/local/opt/[email protected]/bin

Please list the bin directory above using ls in order to find and use the python executable in it.

For macOS M1 (not Intel) (see also, M1 brew setup), modify brew install path, eg:

/opt/homebrew/Cellar/[email protected]/bin

Please list the bin directory above using ls in order to find and use the python executable in it.

Lastly, the version of /usr/local/bin/python3 is probably not the version you want for your virtual environment.

Upvotes: 70

walter
walter

Reputation: 639

As previous answers also mentioned.. no pyenv needed, this works perfect for me:

brew install [email protected]
brew install [email protected]
brew install [email protected]

Then just add the corresponding version lines to the ~/.bashrc

export PATH="$PATH:/usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.7/bin"
export PATH="$PATH:/usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.8/bin"
export PATH="$PATH:/usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/bin"

Upvotes: 13

Carlos
Carlos

Reputation: 1925

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.

Upvotes: -2

flybonzai
flybonzai

Reputation: 3931

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

Upvotes: 3

user2943160
user2943160

Reputation: 537

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.

Upvotes: 26

Related Questions