jkisolo.com

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:

  1. 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.
  2. 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.
  3. 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

List of installed Python 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

List of Pyenv commands

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

Help documentation for Pyenv command

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

Available Python versions for installation

For this demonstration, I will install version 3.6.15:

pyenv install 3.6.15

Successful installation of Python version

After installation, you can verify its availability by listing the versions again:

Updated list of installed Python versions

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:

Active Python version in local directory

Key points to note:

  1. This version is only valid within the specified directory.
  2. 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:

Confirmation of selected Python 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

Creating a virtual environment with Pyenv

To verify the Python version in this virtual environment, simply check:

Checking Python version in the virtual environment

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

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Maximizing Competitive Advantage in Business Strategy

Insights on building and sustaining competitive advantages in business, focusing on user experience and company culture.

The Surprising Benefits of Selfishness: A Journey to Joy

Exploring how intentional selfishness can lead to greater happiness and compassion for others.

# 12 Simple Habits to Boost Your Intelligence and How to Adopt Them

Discover 12 fun and effective habits to enhance your intellect and creativity, making you smarter and more productive in daily life.

Why Projects Fail: Insights from Kodak's Experience

An exploration of why projects fail despite solid planning, using Kodak's downfall as a case study.

Unlocking Your Full Potential: A Guide to Empathetic Communication

Explore the art of empathetic communication to enhance relationships and foster deeper connections.

Exploring the Intricacies of Chaos Theory in Science and Life

Discover the complexities of chaos theory and its applications across various fields, illuminating its unpredictable yet structured nature.

What Dreams Are Composed Of: Exploring the Nature of Dreams

An exploration into the nature of dreams, their significance, and their impact on our lives.

# Seamless Transition: Transferring Data from iPhone to Samsung

Discover effective methods for transferring data from iPhone to Samsung using Smart Switch, along with essential tips for a smooth process.