DEV Community

Cover image for Exploring Python Environments with PyEnv
Pedro Caldeira
Pedro Caldeira

Posted on

Exploring Python Environments with PyEnv

Introduction

Python developers often encounter scenarios where they must manage multiple Python versions and virtual environments. This is where pyenv comes to the rescue. pyenv is a simple yet powerful tool that allows you to easily install, manage, and switch between different Python versions on your system.

In this blog post, we'll explore the fundamental operations of pyenv, including installation, creating and managing environments, and utilizing the virtualenv plugin for more advanced use cases.

Installing pyenv

Before diving into pyenv, let's install it on your machine. The process is straightforward:

curl https://pyenv.run | bash

# Add the following to your shell profile (.bashrc, .zshrc, etc.)
if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init --path)"
  eval "$(pyenv virtualenv-init -)"
fi
Enter fullscreen mode Exit fullscreen mode

Basic Operations

Listing Available Python Versions

To see which Python versions are available for installation, use:

pyenv install --list
Enter fullscreen mode Exit fullscreen mode

Installing a Python Version

Install a specific Python version with:

pyenv install 3.8.12
Enter fullscreen mode Exit fullscreen mode

Setting the Global Python Version

Set a global Python version for your system:

pyenv global 3.8.12
Enter fullscreen mode Exit fullscreen mode

Creating a Virtual Environment

Create a virtual environment for your project:

pyenv virtualenv 3.8.12 myenv
Enter fullscreen mode Exit fullscreen mode

Activating and Deactivating Environments

Activate an environment:

pyenv activate myenv
Enter fullscreen mode Exit fullscreen mode

Deactivate the current environment:

pyenv deactivate
Enter fullscreen mode Exit fullscreen mode

Advanced Usage with the Virtualenv Plugin

pyenv can be extended with plugins. One such helpful plugin is pyenv-virtualenv. Install it via:

git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
Enter fullscreen mode Exit fullscreen mode

Creating Virtual Environments with the Plugin

pyenv virtualenv 3.8.12 myenv
Enter fullscreen mode Exit fullscreen mode

Activating and Deactivating Environments with the Plugin

pyenv activate myenv
Enter fullscreen mode Exit fullscreen mode
pyenv deactivate
Enter fullscreen mode Exit fullscreen mode

Listing Virtual Environments

pyenv virtualenvs
Enter fullscreen mode Exit fullscreen mode

Understanding pyenv local, pyenv global, and pyenv shell

When working with pyenv, you have three main commands for managing Python versions on different levels: pyenv local, pyenv global, and pyenv shell.

1. pyenv local

The pyenv local command allows you to set a local Python version for a specific project or directory. This is particularly useful when different projects require different Python versions. When you run pyenv local, a file named .python-version is created inside the project directory, specifying the desired Python version.

Usage:

cd your_project_directory
pyenv local 3.8.12
Enter fullscreen mode Exit fullscreen mode

This creates a .python-version file in your project directory containing the version information.

Now, whenever you navigate this project directory or its subdirectories, pyenv will automatically switch to the specified Python version. This local configuration is project-specific, making it convenient to manage dependencies per-project basis.

2. pyenv global

On the other hand, pyenv global sets the global Python version for your entire system. This default Python version will be used without a local or shell-specific configuration.

Usage:

pyenv global 3.9.5
Enter fullscreen mode Exit fullscreen mode

This command sets the global Python version to 3.9.5. It's handy when you want to define a default Python version for your entire development environment.

3. pyenv shell

The pyenv shell command sets a Python version for the current shell session. Unlike pyenv local, it does not create a configuration file. Instead, it affects only the current shell session.

Usage:

pyenv shell 3.7.9
Enter fullscreen mode Exit fullscreen mode

This command sets the Python version for the current shell session to 3.7.9. It's useful for temporary changes within a terminal window.

Focus on pyenv local

The real strength of pyenv local lies in its ability to create a project-specific configuration. By generating a .python-version file within your project directory, pyenv ensures that the correct Python version is automatically activated every time you enter that directory.

This feature is especially valuable in collaborative projects or when working on multiple projects with different Python requirements. It promotes consistency and avoids conflicts by encapsulating the Python version within the project's directory.

Next time you embark on a new project, consider using pyenv local to manage Python versions effortlessly on a per-project basis.

Conclusion

In conclusion, pyenv stands out as a straightforward and efficient tool for managing Python versions and virtual environments, providing simplicity for Python-centric projects with commands like pyenv local, pyenv global, and pyenv shell. Notably, the elegance of pyenv local lies in its ability to create project-specific configurations, facilitating seamless transitions between diverse project environments. However, as the complexities of projects grow, the environment management landscape expands to encompass broader needs.

Resources

Top comments (0)