How to manage multiple versions of Python on a Mac?
Published 16. may 2018 07:27 by Stein Ove Helset
Using multiple versions of Python is something that a Python developer is going to face a few times. Maybe you need to test a old site or test how your script is working with the newest version of Python. This is where "pyenv" is coming to the rescue. Pyenv helps you to install and switch between multiple versions of Python.
How to install pyenv?
It's really easy to install, you just need to make sure you got brew installed on your Mac and then run these commands:
$ brew update
$ brew install pyenv
When the installation is finished we need to add pyenv to our environment. Run these two commands:
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
After this you can restart your shell and pyenv should be working as expected.
How to use pyenv?
First we can install a couple of Python versions, you do this by running a simple command.
$ pyenv install 2.6.0
$ pyenv install 3.5.0
Now you got multiple versions of Python on your Mac. To switch between the versions you run this command:
$ pyenv use 3.5.0
If you want to check which version of Python pyenv is currenty using you can run this command:
$ pyenv version
When you activate a version of Python using pyenv is just active in your current shell. But pyenv also makes it possible to set a global version of Python:
pyenv global 2.6.0