Skip to content

Pyenv Environment Management Script

Source: Notion | Last edited: 2024-11-25 | ID: 045bb72d-630...


This script is designed to simplify the management of Python environments using pyenv and pyenv-virtualenv. It provides an interactive interface for creating, activating, deactivating, and deleting virtual environments, as well as managing Python packages within those environments.


Managing multiple Python environments can be challenging, especially when dealing with different projects requiring different Python versions and packages. This script leverages pyenv and pyenv-virtualenv to provide an interactive and automated way to manage Python environments and packages.

  • Environment Initialization: Automatically initializes pyenv and sets the latest stable Python version as global.
  • Environment Listing and Activation: Lists available Python environments and allows you to activate them interactively.
  • Environment Deactivation: Provides an option to deactivate the current virtual environment.
  • Python Version Expunging: Allows you to remove a base Python installation and its associated virtual environments.
  • Package Management: Facilitates installing, uninstalling, and updating Python packages within an environment.
  • Virtual Environment Creation and Deletion: Enables you to create new virtual environments or delete existing ones.
  • Prompt Modification: Modifies your shell prompt to reflect the active virtual environment.

Before using the script, ensure you have the following installed:

  • pyenv: A Python version management tool.
  • pyenv-virtualenv: A pyenv plugin that provides features to manage virtual environments.
  • fzf: A command-line fuzzy finder for interactive selection.
  • curl: For downloading scripts (used if pyenv is not installed).
  • Git: Required by pyenv for cloning repositories.
  • Build Tools: Compilers and libraries required to build Python from source (e.g., gcc, make, libssl-dev, zlib1g-dev).

  1. Clone or Download the Script Save the script to a file, for example, manage_pyenv.sh.

  2. Make the Script Executable

Terminal window
chmod +x manage_pyenv.sh
  1. Ensure the Script is in Your PATH Move the script to a directory that’s in your PATH, or run it using ./manage_pyenv.sh.

Run the script from your terminal:

Terminal window
./manage_pyenv.sh

Upon running the script, it checks if pyenv is installed:

  • If not installed, it prompts you to install it.
  • Initializes pyenv by setting up the necessary environment variables.
  • Sets the latest stable Python version as the global version.

The script lists available Python environments:

  • Virtual Environments: Environments you’ve created, usually for specific projects.
  • Base Installations: The core Python versions installed via pyenv. You can select an environment to activate by entering the corresponding number.

Example:

Virtual Environments:
1 my_project_env
2 data_science_env
Base Installations:
3 3.9.7
4 3.8.12
Press the number key corresponding to the environment you want to activate, or press q to quit:

To deactivate the currently active environment:

  • Press D or d when prompted to select an environment.
  • The script will deactivate the environment and reset your shell.

To remove a base Python version and all its associated virtual environments:

  • Press e or E when prompted to select an environment.
  • Use the interactive selector to choose the Python version to expunge.
  • Confirm the deletion when prompted. Warning: This action is irreversible and will delete all virtual environments associated with the selected Python version.

After activating an environment, the script provides options for package management.

  • Displays all installed packages within the current environment along with their versions.
  • Uses pip list and allows detailed inspection with pip show.
  • Analyzes your project files to suggest packages that may be needed.
  • Uses fzf for interactive selection of packages to install.
  • Supports batch installation and handles dependency checks.
  • Lists all installed packages.
  • Allows you to select multiple packages to uninstall.
  • Checks for dependencies to prevent breaking other packages.
  • Checks for outdated packages.
  • Allows you to select packages to update.
  • Handles dependency conflicts and warns if manual intervention is needed.
  • Provides an interactive way to create a new virtual environment.
  • Allows you to select the Python version for the new environment.
  • Suggests a best-practice naming convention for the environment.
  • Automatically installs the pathspec package in the new environment.
  • Lists all existing virtual environments.
  • Allows you to select an environment to delete.
  • Confirms deletion before proceeding.

The script supports several command-line options:

  • n: Specify the number of the environment to activate.
  • a: Specify an action to perform (e.g., D for deactivate, e for expunge).
  • x: Exit the script after activating the environment. Example:
Terminal window
./manage_pyenv.sh -n 2 -x

This command activates environment number 2 and exits the script immediately after activation.


Run the script and select the environment you wish to activate by entering its number.

Run the script and press D when prompted to select an environment.

  • Run the script.
  • Activate any environment (or just proceed to package management).
  • In the package management menu, select “Create new virtual environment.”
  • Follow the prompts to select a Python version and name your environment.

After activating an environment, the package management menu will appear. Use it to install, update, or uninstall packages.


  • Pyenv Not Found: Ensure pyenv is installed and properly configured in your shell.
  • FZF Not Found: Install fzf for interactive selection features.
  • Permission Issues: Make sure the script has execute permissions.
  • Dependency Conflicts: When updating or uninstalling packages, the script checks for dependencies. Follow the prompts to handle conflicts.

  • Use Virtual Environments: Always work within a virtual environment to isolate project dependencies.
  • Regular Updates: Keep your Python versions and packages updated to receive the latest features and security patches.
  • Naming Conventions: Use clear and descriptive names for your virtual environments, including the Python version.
  • Version Control: Keep track of your requirements.txt or Pipfile for consistent environments across different setups.

The script manages .python-version files, which tell pyenv which Python version to use in the current directory.

  • When you activate an environment, the script updates or creates a .python-version file with the selected environment.
  • This ensures consistent environment usage within the directory.

The script modifies your shell prompt to display the active virtual environment:

  • Updates the PS1 environment variable.
  • The change is temporary and applies only to the current shell session.
  • To make permanent changes, consider adding prompt modifications to your shell’s configuration file (e.g., .bashrc, .zshrc).

Note: This script automates many tasks associated with Python environment management. Always review actions, especially when deleting environments or expunging Python versions, to prevent accidental data loss.

Disclaimer: Use this script at your own risk. Ensure you have backups of important data and environments before performing destructive operations.