LinuxCommandLibrary

pyenv-virtualenv

Create isolated Python environments

TLDR

Create a new Python 3.6.6 virtual environment

$ pyenv virtualenv [3.6.6] [virtualenv_name]
copy

List all existing virtual environments
$ pyenv virtualenvs
copy

Activate a virtual environment
$ pyenv activate [virtualenv_name]
copy

Deactivate the virtual environment
$ pyenv deactivate
copy

SYNOPSIS

pyenv virtualenv [-f|--force] [-p|--prompt <prompt>] [--system-site-packages] <python_version> <environment_name>
pyenv activate <environment_name>
pyenv deactivate
pyenv virtualenvs
pyenv virtualenv-delete <environment_name>

PARAMETERS

<python_version>
    The pyenv-installed Python version to base the new virtual environment on (e.g., 3.9.10, 3.8.5).

<environment_name>
    The unique name to assign to the new virtual environment. This name is used for activation and deletion.

-f, --force
    Forces the creation of the virtual environment, overwriting an existing one with the same name if it already exists.

-p, --prompt <prompt>
    Sets a custom prefix for the shell prompt when the virtual environment is activated.

--system-site-packages
    Gives the new virtual environment access to the global site-packages directory of the base Python installation.

DESCRIPTION

pyenv-virtualenv is a pyenv plugin that provides robust integration with virtualenv and conda, streamlining the management of Python virtual environments. It extends pyenv's core functionality, allowing users to effortlessly create, activate, deactivate, and list isolated Python environments directly via pyenv commands. This eliminates the need to manually invoke virtualenv or conda commands, simplifying dependency management for different projects. By integrating with pyenv's shim mechanism, pyenv-virtualenv ensures that when an environment is activated, the shell's PATH is correctly adjusted to prioritize the environment's binaries. This makes it an indispensable tool for developers who rely on pyenv for managing multiple Python versions and require isolated, project-specific dependencies.

CAVEATS

For pyenv-virtualenv to function correctly, pyenv must be properly installed and initialized in your shell. Crucially, you must include eval "$(pyenv init -)" and eval "$(pyenv virtualenv-init -)" in your shell's configuration file (e.g., .bashrc, .zshrc) to enable the necessary shell functions for activation and deactivation. Without proper shell integration, environment switching will not work as expected.

CORE WORKFLOW

The typical workflow with pyenv-virtualenv involves three main steps:
1. Create: Use pyenv virtualenv <python_version> <environment_name> to establish a new isolated environment.
2. Activate: Use pyenv activate <environment_name> to switch into the desired environment, making its Python and installed packages available.
3. Deactivate: Use pyenv deactivate to exit the current virtual environment, returning to the global or pyenv-selected Python version. This structured approach simplifies managing dependencies across multiple projects.

LISTING ENVIRONMENTS

To view all currently available virtual environments managed by pyenv-virtualenv, you can use the command pyenv virtualenvs. This provides a clear overview of your created environments, including the Python version they are based on.

HISTORY

pyenv-virtualenv emerged as a natural extension for pyenv, addressing the growing need for seamless integration between Python version management and virtual environment isolation. Prior to its development, developers often had to manage pyenv and virtualenv workflows separately, leading to potential inconsistencies. This plugin bridged that gap, providing a unified interface for creating and managing project-specific Python environments. Its release significantly enhanced pyenv's utility, making it a more comprehensive tool for Python development by automating and simplifying what were previously manual and error-prone steps.

SEE ALSO

pyenv(1), virtualenv(1), pip(1), python(1)

Copied to clipboard