Pyenv Environment Management Script
Source: Notion | Last edited: 2024-11-25 | ID: 045bb72d-630...
-
pyenv and pyenv-virtualenv: Managing Python Environments
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.
Overview
Section titled “Overview”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.
Key Features
Section titled “Key Features”- Environment Initialization: Automatically initializes
pyenvand 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.
Prerequisites
Section titled “Prerequisites”Before using the script, ensure you have the following installed:
- pyenv: A Python version management tool.
- pyenv-virtualenv: A
pyenvplugin that provides features to manage virtual environments. - fzf: A command-line fuzzy finder for interactive selection.
- curl: For downloading scripts (used if
pyenvis not installed). - Git: Required by
pyenvfor cloning repositories. - Build Tools: Compilers and libraries required to build Python from source (e.g.,
gcc,make,libssl-dev,zlib1g-dev).
Installation
Section titled “Installation”-
Clone or Download the Script Save the script to a file, for example,
manage_pyenv.sh. -
Make the Script Executable
chmod +x manage_pyenv.sh- 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:
./manage_pyenv.shInitializing Pyenv
Section titled “Initializing Pyenv”Upon running the script, it checks if pyenv is installed:
- If not installed, it prompts you to install it.
- Initializes
pyenvby setting up the necessary environment variables. - Sets the latest stable Python version as the global version.
Listing and Activating Environments
Section titled “Listing and Activating Environments”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_env2 data_science_envBase Installations:3 3.9.74 3.8.12
Press the number key corresponding to the environment you want to activate, or press q to quit:Deactivating the Current Environment
Section titled “Deactivating the Current Environment”To deactivate the currently active environment:
- Press
Dordwhen prompted to select an environment. - The script will deactivate the environment and reset your shell.
Expunging Python Versions
Section titled “Expunging Python Versions”To remove a base Python version and all its associated virtual environments:
- Press
eorEwhen 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.
Package Management
Section titled “Package Management”After activating an environment, the script provides options for package management.
Listing Installed Packages
Section titled “Listing Installed Packages”- Displays all installed packages within the current environment along with their versions.
- Uses
pip listand allows detailed inspection withpip show.
Installing Packages
Section titled “Installing Packages”- Analyzes your project files to suggest packages that may be needed.
- Uses
fzffor interactive selection of packages to install. - Supports batch installation and handles dependency checks.
Uninstalling Packages
Section titled “Uninstalling Packages”- Lists all installed packages.
- Allows you to select multiple packages to uninstall.
- Checks for dependencies to prevent breaking other packages.
Updating Packages
Section titled “Updating Packages”- Checks for outdated packages.
- Allows you to select packages to update.
- Handles dependency conflicts and warns if manual intervention is needed.
Creating New Virtual Environments
Section titled “Creating New Virtual Environments”- 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
pathspecpackage in the new environment.
Deleting Virtual Environments
Section titled “Deleting Virtual Environments”- Lists all existing virtual environments.
- Allows you to select an environment to delete.
- Confirms deletion before proceeding.
Command-Line Options
Section titled “Command-Line Options”The script supports several command-line options:
n: Specify the number of the environment to activate.a: Specify an action to perform (e.g.,Dfor deactivate,efor expunge).x: Exit the script after activating the environment. Example:
./manage_pyenv.sh -n 2 -xThis command activates environment number 2 and exits the script immediately after activation.
Examples
Section titled “Examples”Activating an Environment
Section titled “Activating an Environment”Run the script and select the environment you wish to activate by entering its number.
Deactivating an Environment
Section titled “Deactivating an Environment”Run the script and press D when prompted to select an environment.
Creating a New Virtual Environment
Section titled “Creating a New Virtual 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.
Managing Packages
Section titled “Managing Packages”After activating an environment, the package management menu will appear. Use it to install, update, or uninstall packages.
Troubleshooting
Section titled “Troubleshooting”- Pyenv Not Found: Ensure
pyenvis installed and properly configured in your shell. - FZF Not Found: Install
fzffor 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.
Best Practices
Section titled “Best Practices”- 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.txtorPipfilefor consistent environments across different setups.
Appendix
Section titled “Appendix”Understanding .python-version Files
Section titled “Understanding .python-version Files”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-versionfile with the selected environment. - This ensures consistent environment usage within the directory.
Modifying the Shell Prompt
Section titled “Modifying the Shell Prompt”The script modifies your shell prompt to display the active virtual environment:
- Updates the
PS1environment 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.