Quick Note on pyenv: Python Version Management Tool

This is a quick note on how to use pyenv, a tool for switching between different Python versions.

  • Display the list of installed Python versions:

    pyenv versions
    
  • Show the list of available Python versions for installation:

    pyenv install --list
    
  • Update pyenv:

    pyenv update
    
  • Install a specific version of Python:

    pyenv install <VERSION>
    
  1. Check (or set) the Python version active in the current session:
    (based on the PYENV_VERSION environment variable)

    pyenv shell (<VERSION>)
    
  2. Check (or set) the Python version active in the current directory:
    (based on the .python-version file value, searching up to the root directory)

    pyenv local (<VERSION>)
    
  3. Check (or set) the globally active Python version:
    (based on the value in $(pyenv root)/version or system)

    pyenv global (<VERSION>)
    
  1. For the latest details, refer to the official documentation. If using Windows, install on WSL:

    curl https://pyenv.run | bash
    
  2. If you’re using bash, add the following to ~/.bashrc to ensure it’s loaded every time you open a shell.
    Also, add it to ~/.profile, ~/.bash_profile, or ~/.bash_login for login sessions:

    export PYENV_ROOT="$HOME/.pyenv"
    command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
    eval "$(pyenv init -)"
    
  3. Load the changes:

    source ~/.bashrc
    
  4. Install packages necessary for Python builds.

  1. ~/.pyenv/shims is added at the beginning of $PATH.
  2. This directory contains commands such as python, pip, etc., which are set up to select the appropriate Python version based on the active environment when executed.

Related Content