Master Python Version Control with Pyenv: A Comprehensive Guide
Written on
Chapter 1: Introduction to Pyenv
For both new and seasoned Python developers, incorporating Pyenv into your toolkit is essential before delving into Python programming. This tool offers a crucial capability: the ability to choose the specific Python version you wish to work with.
Why Should You Use Pyenv?
Every programming language relies on some form of compilation at various stages. Let’s examine a few scenarios:
- If you've written code that utilizes features from Python 3, but your compiler only supports Python 2, you’ll encounter significant issues. Simply updating the compiler to manage both versions isn’t always feasible.
- If your system depends on a particular compiler version, updating it could disrupt existing applications that require an older version, leading to a cascade of complications.
- If you’re employed at a company that relies on an older Python version for a project, it’s not practical to alter your main compiler just for that task.
Pyenv resolves these challenges by allowing you to maintain your system’s integrity while managing multiple Python versions in a controlled way.
How to Get Started with Pyenv
First, you'll need to install Pyenv, but the installation process varies depending on your operating system. Therefore, I recommend visiting the official repository to find installation instructions tailored to your platform:
GitHub - pyenv/pyenv: Simple Python version management
Now that you have it installed, let’s explore its functionalities.
How to Check Available Python Versions
All operations will be performed via your terminal, starting with the command: pyenv. To see the versions you currently have installed, use the command:
pyenv versions
The version marked with an asterisk (*) indicates the "global" version, which is used by the entire system. In my case, since I'm using a Mac, I've retained the default setup.
To view all available commands, simply type:
pyenv
If you're curious about the purpose of a specific command, such as pyenv local, you can obtain detailed information by typing:
pyenv local --help
Setting a Local Python Version
Let’s set a local Python version by first creating an empty directory. To view all available Python versions for installation, use:
pyenv install --list
For this demonstration, I will install version 3.6.15:
pyenv install 3.6.15
After installation, you can verify its availability by listing the versions again:
To set this version locally in your new folder, use:
pyenv local 3.6.15
Within that directory, typing pyenv version will confirm the active Python version:
Key points to note:
- This version is only valid within the specified directory.
- A file named .python-version will be created, indicating which Python version is to be used in that folder.
To confirm your setup, running pyenv versions again should show the selected version:
Using the Activated Python Version
When executing Python commands, you’ll now prefix them with pyenv. For instance, if you want to create a virtual environment, you might encounter issues with the default Python version that doesn’t support the command python -m venv.
Instead, try using:
pyenv exec python -m venv testEnvironment
To verify the Python version in this virtual environment, simply check:
You can now navigate into your environment and activate it as you normally would. Any commands can be executed using the Python version you’ve selected through pyenv exec <any command>.
Conclusion
I have also launched a YouTube channel where I aim to teach concepts related to this guide. If you found this tutorial helpful, please consider subscribing!
The first video, "Manage Multiple Python Versions with PyEnv," delves deeper into utilizing Pyenv effectively.
The second video, "How to Create and Use Virtual Environments in Python With Poetry," provides further insights into managing your Python environments.
Connect with me on LinkedIn:
Felipe Florencio Garcia — iOS Developer — ABN AMRO Bank N.V. | LinkedIn
And follow me on Instagram: @thedevproject