Skip to content

1.2. pyenv

What is pyenv?

Pyenv is a tool specifically designed for managing multiple Python versions on a single computer. It addresses a common challenge developers encounter: needing to work on multiple projects simultaneously, each requiring a different Python version. Pyenv allows for seamless switching between Python versions, ensuring each project runs within its required environment. This capability prevents interference with the system-wide Python installation or with other projects.

Why should you use pyenv?

Opting for pyenv as your Python version management tool offers several benefits:

  • Flexibility: Pyenv enables the effortless management and transition between various Python versions, accommodating the unique requirements of different projects.
  • Non-root Installation: It supports installing Python versions without necessitating system-wide alterations or administrator privileges. This feature is particularly beneficial for users lacking root access.
  • System Python Independence: Pyenv operates at the user level, managing Python versions independently of the system's Python. This approach mitigates conflicts with the operating system's Python version, promoting a more stable and predictable development environment.

How to install pyenv on your computer?

For pyenv installation, refer to the official pyenv GitHub repository, which provides detailed installation instructions for various operating systems. These guidelines facilitate a smooth setup process on your system, regardless of the operating system you're using.

Is there a specific setup for MLOps projects?

When setting up pyenv for MLOps projects, the key consideration is to select a Python version that ensures compatibility with the project's dependencies, libraries, and frameworks. Other than choosing the appropriate Python version, MLOps projects do not have unique setup requirements with pyenv.

How to install the required version of Python for your project?

To install a particular Python version, like Python 3.12, use the command below with pyenv:

pyenv install 3.12

You can select the global version of Python to use on your system with this command:

pyenv global 3.12

How can you select the version of Python for your project?

To set a Python version for your project, proceed as follows:

  1. Go to your project's root directory.
  2. Create a .python-version file, adding the desired Python version (e.g., 3.12) to it:
3.12
  1. Once pyenv is configured and active, it will automatically switch to the version specified in the .python-version file upon entering the project directory. You can verify the active Python version with:
# Confirming the currently active Python version
$ python --version

If the expected version switch does not occur, ensure that your shell is properly set up to integrate with pyenv. This typically involves appending pyenv initialization commands to your shell's configuration file. Detailed instructions are available on the pyenv GitHub page.

How can you determine the currently active Python version in your shell?

To check the active Python version in your shell, use the command:

pyenv version

For information on the Python version and its executable location, independent of pyenv, the following commands are useful:

# To see the Python version
python --version

# To locate the Python executable
which python

PyEnv additional resources