Pyenv to manage multiple Python versions

Learn how to effectively manage multiple Python versions globally and locally using Pyenv on Mac operating system.

1. Intro to Pyenv

In this article, we will learn about Pyenv to manage multiple Python versions effectively.

Python comes bundled with Mac OS, however, they are 2.X version. Python 2.X is outdated and they are not suggested for any new development.

The latest Python version is 3.8 (As of 20 April 2020). When we install Python 3.8 using the installer package and try to run python the command we were surprised to see it’s still pointing to the python 2.X version.

Python website download page
% python

WARNING: Python 2.7 is not recommended. 
This version is included in macOS for compatibility with legacy software. 
Future versions of macOS will not include Python 2.7. 
Instead, it is recommended that you transition to using 'python3' from within Terminal.

Python 2.7.16 (default, Dec 13 2019, 18:00:32) 
[GCC 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.32.4) (-macos10.15-objc-s on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
% pip

To use newly installed python 3.8, we have to use python3 commands.

 % python3
Python 3.8.2 (v3.8.2:7b3ab5921f, Feb 24 2020, 17:52:18) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

One of my tools doesn’t support Python 3.8, a curse of the most recent version. I have to install Python 3.7 for this tool to work fine.

I started pondering, what is the best way to manage multiple Python versions, where I can pick and choose the default Python version for specific tools. Without any utility tool, there was a lot of effort required to achieve the same, which involve, changing alias and PATH variables in the Mac very frequently.

Fortunately, we have Pyenv((Pyenv Github Project Page)) tool, which helps us in managing multiple Python versions effectively on Mac. It does the following work:

  1. Global Python version per user basis.
  2. Python version per-project basis.
  3. Allowing overriding of Python version using environmental variable.

I used Pyenv installer ((Pyenv Installer)) to install it, however, you can use homebrew and git checkout also. Read detail about installation from the Pyenv Github page((Pyenv installation guide)).

Once you are done with Pyenv installation you can use the following commands to manage.

# Check all available commands
% pyenv commands

2. Installation of a Python version – pyenv install

# Install a new version of Python
% pyenv install <version>

# See all available versions for installation
% pyenv install --list

One thing which annoyed me was no support to use the existing installed Python with Pyenv, there are some plugin solutions ((Pyenv plugin to use existing Python)), which don’t work for me.

I believe for me to use Pyenv require me to remove all the existing Python installations (of course not the System required 2.x version) and use them exclusively for managing my Pythons installations.

3. Uninstallation of a Python version

% pyenv uninstall <version>

4. Setting global (User wise) Python version

# Set global default version to 3.7.8
% pyenv global 3.7.8

# Check versions info
% pyenv versions

# Setting multiple global versions with default to 3.7.8
% pyenv global 3.7.8 2.7.6

% python --version
Python 3.7.8
% python2.7 --version
Python 2.7.6
% python3.7 --version
Python 3.7.8

5. Setting local (Application wise) Python version

Assume we have a Python application at the/python-app folder, we want to run this application with a different Python Version than global. We can easily do so using Pyenv local command, which creates a file called .python-version in the current directory to save Python local version configurations.

# Change to application directory

% cd /python-app

% pyenv local 2.7.6

# Now when we will run python command it will run as version 2.7.6, overriding the global version
% python --version
Python 2.7.6

# If we want to rollback the local setting to global
% pyenv local --unset

Just like pyenv global configuration to keep multiple versions, we can do a similar setting for local.

6. Conclusion

I believe Pyenv is a smart choice for Python geeks to manage multiple Python versions on Mac OS and other OS (including Windows) without much hassle.

JOIN OUR NEWSLETTER
And get notified everytime we publish a new blog post.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top