-
-
Notifications
You must be signed in to change notification settings - Fork 0
Pyenv Installation
pyenv is a powerful tool for managing multiple Python versions on your system. It allows you to install, switch between, and set global or project-specific Python versions without interfering with the system Python installation.
- Why Use pyenv?
- Installing pyenv
- Using pyenv
- Using pyenv with Virtual Environments
- Troubleshooting
- Further Information
- Multiple Python Versions: Allows you to easily install and use multiple Python versions (e.g., Python 3.8, 3.10, 3.11) on the same system.
- Project Isolation: You can specify a specific Python version for each project, ensuring compatibility and preventing conflicts between projects that require different Python versions.
- No System Python Interference: pyenv leaves your system's Python installation untouched, preventing accidental modifications or breakage.
- Easy Switching: Switching between Python versions is straightforward.
The installation process for pyenv varies slightly depending on your operating system and shell. Here's a general overview and some common examples:
-
Prerequisites:
- Ensure you have the necessary build tools installed (e.g., compilers, headers). These are often required to build Python from source. The install instructions will vary based on your OS.
-
Install pyenv:
-
Using a Package Manager (Recommended for some Linux distributions):
# Example for Debian/Ubuntu (check pyenv's documentation for exact details) sudo apt update sudo apt install pyenv -
Using git (General method): This is a more general method that works on most systems, and is used when a package manager is not available.
git clone https://github.com/pyenv/pyenv.git ~/.pyenv # Add pyenv to your shell (see instructions below for different shells).
-
-
Configure Your Shell: After installing pyenv, you need to configure your shell to initialize it. This usually involves adding some lines to your shell's configuration file (e.g.,
.bashrc,.zshrc,.config/fish/config.fish).-
Bash/Zsh:
Add the following lines to your
.bashrcor.zshrcfile (replace<your_username>as needed):export PYENV_ROOT="$HOME/.pyenv" [[ -f "$PYENV_ROOT/bin/pyenv" ]] && eval "$(pyenv init -)"
Then, source your shell configuration file to apply the changes:
source ~/.bashrc # or source ~/.zshrc
-
FISH:
Add these lines to your
~/.config/fish/config.fishfile:set -x PYENV_ROOT "$HOME/.pyenv" if test -d "$PYENV_ROOT" set -x PATH "$PYENV_ROOT/bin" $PATH eval (pyenv init -|psub) end
Then, restart your FISH terminal or run
source ~/.config/fish/config.fish.
-
-
Verify Installation:
pyenv --version
This command should display the pyenv version if the installation was successful.
-
List Available Python Versions:
pyenv install --list
This command lists the Python versions that are available for installation.
-
Install a Specific Python Version:
pyenv install 3.11.x # Replace 3.11.x with the version you wantThis will download, compile, and install the specified Python version. This can take a while.
-
List Installed Python Versions:
pyenv versions
This lists the Python versions you have installed.
-
Set a Global Python Version:
pyenv global 3.10.x # Set a global version. This will be the default.This sets the global Python version used by default for all projects.
-
Set a Local Python Version for a Project:
cd <your_project_directory> pyenv local 3.10.x # Sets a version for this project only.
This creates a
.python-versionfile in your project directory, specifying the Python version to use for this project. pyenv will automatically use that version when you're in that directory. -
Check Which Python Version is Active:
python --version
pyenv version
pyenv works seamlessly with virtual environments.
-
Create a Virtual Environment:
python3 -m venv .venv # This creates a new one from the python version set by pyenvThis will use the currently active Python version (determined by
pyenv) to create the virtual environment. -
Activate the Virtual Environment: (Standard activation method for your system - see main documentation for details)
-
Use pip to install packages within the activated virtual environment: (as always)
pip install -r requirements.txt
-
Shell Configuration: Ensure your shell is configured correctly with
pyenv init. Double-check your shell configuration file for any typos. -
Rehashing: After installing a new Python version, run
pyenv rehashto update the shims. -
PATH Issues: If you're still encountering issues, verify that
pyenvis correctly adding the necessary directories to yourPATHenvironment variable. - Build Dependencies: You might need to install build tools (e.g., compilers, headers) before installing Python versions. The specific packages needed depend on your Linux distribution (see troubleshooting in the Advanced Installation Guide).
- Proxy Settings: pyenv may use proxy settings.
- pyenv Documentation: Link to the official pyenv documentation - e.g., https://github.com/pyenv/pyenv