pyenv
pyenv is a Python version management tool that allows you to install and manage multiple Python versions in parallel. It is particularly useful for developers who need to work with different Python versions.
Installation and Setup
Dependencies and Installation
Before you can install pyenv, you need to install some dependencies. The required packages differ depending on your Linux distribution:
Ubuntu/Debian
sudo apt-get update
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
Fedora
sudo dnf install -y make gcc zlib-devel bzip2 bzip2-devel readline-devel \
sqlite sqlite-devel openssl-devel xz xz-devel libffi-devel findutils
Arch Linux
The installation of pyenv consists of three steps:
-
Clone the pyenv repository:
-
Add pyenv to your shell configuration. Choose the configuration for your shell:
For bash
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc echo 'eval "$(pyenv init -)"' >> ~/.bashrcFor zsh
-
Reload your shell configuration:
For bash
For zsh
Basic Usage and Python Versions
Managing Python Versions
To install a new Python version, use the following command:
To use a Python version globally, execute this command:
To use a Python version for a specific directory, execute this command in the desired directory:
To show all installed Python versions, use:
Advanced Features and Plugins
Virtual Environments and Plugin System
pyenv supports virtual environments through the pyenv-virtualenv plugin. The setup consists of three steps:
-
Install the plugin:
-
Create a virtual environment:
-
Activate the virtual environment:
pyenv can automatically select the right Python version based on a .python-version file. Create the file with:
pyenv supports various plugins for additional functionality:
- pyenv-virtualenv: Virtual environments
- pyenv-which-ext: Extended path resolution
- pyenv-update: Automatic updates
System Management and Troubleshooting
System Status and Maintenance
To check the currently used Python version:
When you've installed new Python binaries, execute this command:
For seamless integration with your shell, add these lines to your shell configuration:
Troubleshooting
If you encounter issues, proceed as follows:
- Check your shell configuration
- Ensure all dependencies are installed
- Verify the permissions of pyenv directories
- Consult the official pyenv documentation