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.
List of Commands
For Installing Python
-
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>
For Checking (or Setting) Python Version
-
Check (or set) the Python version active in the current session:
(based on thePYENV_VERSIONenvironment variable)pyenv shell (<VERSION>) -
Check (or set) the Python version active in the current directory:
(based on the.python-versionfile value, searching up to the root directory)pyenv local (<VERSION>) -
Check (or set) the globally active Python version:
(based on the value in$(pyenv root)/versionorsystem)pyenv global (<VERSION>)
Installation
-
For the latest details, refer to the official documentation. If using Windows, install on WSL:
curl https://pyenv.run | bash -
If you’re using bash, add the following to
~/.bashrcto ensure it’s loaded every time you open a shell.
Also, add it to~/.profile,~/.bash_profile, or~/.bash_loginfor login sessions:export PYENV_ROOT="$HOME/.pyenv" command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH" eval "$(pyenv init -)" -
Load the changes:
source ~/.bashrc -
Install packages necessary for Python builds.
Mechanism
~/.pyenv/shimsis added at the beginning of$PATH.- 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.