pyenv is a popular tool for managing multiple versions of Python on a single system. It allows you to easily switch between different versions of Python for various projects, which is particularly useful when some projects require specific Python versions.
Key Features:
pyenv in combination with pipenv or virtualenv to manage specific Python versions inside virtual environments.Pros:
virtualenv, pipenv, and pyenv-virtualenv for managing environments.Cons:
Choose pyenv if you need to manage multiple Python versions on your system. It’s ideal for switching between different Python interpreters, particularly when you’re working with projects that require different Python versions.
To install pyenv, follow these steps (for Unix-based systems like Linux or macOS):
Install Dependencies:
sudo apt update
sudo apt install -y make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev \
python3-openssl git
Install pyenv:
Run the following commands to install pyenv:
curl https://pyenv.run | bash
Update Shell Configurations:
Add the following lines to your shell configuration file (~/.bashrc, ~/.bash_profile, ~/.zshrc, etc.):
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
Restart the Shell:
Restart your terminal or run the following command:
exec "$SHELL"
Verify Installation:
Check if pyenv is installed by running:
pyenv --version
List available Python versions:
pyenv install --list
Install a specific Python version:
pyenv install 3.9.5
Set global Python version:
pyenv global 3.9.5
Set a local Python version for a project:
pyenv local 3.8.10
Check current Python version:
pyenv version
Uninstall a Python version:
pyenv uninstall 3.9.5
With pyenv, you can seamlessly manage different Python environments without conflicts.