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 command we surprise to see it's still pointing to the python 2.x version.
% 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-1126.96.36.199) (-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 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, cursed of 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 help us in managing multiple Python versions effectively on Mac. It does following work:
- Global Python version per user basis.
- Python version per-project basis.
- Allowing overriding of Python version using environmental variable.
Once you are done with Pyenv installation you can use 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 solution ((Pyenv plugin to use existing Python)), which doesn't work for me.
I believe for me to use Pyenv require me to remove all the existing Python installation (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 /python-app folder, we want to run this application with 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 similar setting for local.
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.