PyEnv is a tool that allows Python users to switch between multiple versions of Python easily without tampering with the system’s default Python installation. It achieves this by overriding the python command on the command line, which helps you use a specific version for your projects. PyEnv creates isolated environments, each with its specific version of Python.
Which problem does PyEnv solve?
Python, being an evolving language, has multiple versions available for use. At times, developers need to juggle between these versions to ensure compatibility or utilize features from a specific version for a specific project. Managing these versions manually can be challenging and may lead to conflicts or unexpected behaviors.
Another frequent issue developers run into is unexpected Python upgrades. For example, many developers on Macs use Brew. Many brew packages have Python as a dependency. As such, any package upgrade can trigger a change in the Python version installed by Brew. This, in turn, can break a virtual environment since the referenced Python binary is suddenly missing. A similar situation may occur when you use the default Python installation on a Linux machine.
PyEnv solves these issues with:
- Isolation: Each project can have its Python version, ensuring dependencies and environments don’t clash.
- Flexibility: Developers can test their code against multiple Python versions without changing the system settings.
- Consistency: Teams can ensure that every member uses the same Python version, leading to a consistent development environment.
How to install PyEnv
It’s important to note that PyEnv works on Unix-like environments, like Linux and Macs. There’s a Windows port (see the linked install instructions). The installation instructions for PyEnv can change occasionally, so it’s best to check the most recent instructions on the PyEnv Github repository. Ensure you follow the steps for your specific shell (bash, zsh, etc.), otherwise the pyenv command won’t work.
Choosing a global Python version with PyEnv
Let’s first set a default, global Python version for your system. This version will be the default when you haven’t set a specific version for a directory or project.
Before we can do so, we need to know the list of available versions:
pyenv install --listCode language: plaintext (plaintext