Pyenv Commands Cheat Sheet & Reference Guide
Managing multiple Python environments across diverse microservices and backend applications often requires handling competing runtime requirements on a single development machine. In my daily backend engineering workflow, pyenv is an indispensable tool for seamless Python version switching without corrupting system binaries or relying on brittle global setups.
This guide provides a complete technical reference to pyenv subcommands, including CLI flags, attribute options, real-world terminal session examples, and architectural insights into how pyenv resolves environment precedence.
Architecture and Precedence Mechanicsβ
To use pyenv effectively, it helps to understand how it intercepts command calls. Rather than modifying your system python binaries directly, pyenv inserts a directory of shims at the front of your shell's PATH.
When you execute python, pip, or any script installed via a Python package, your shell hits a shim first. The shim analyzes your current environment state and routes the call to the appropriate Python installation.
pyenv evaluates Python version precedence in the following strict order:
PYENV_VERSIONEnvironment Variable: Overrides all other settings for the active shell session (configured viapyenv shell).- Local Directory Specification: Read from a
.python-versionfile in the current working directory or any parent directory traversed upward (configured viapyenv local). - Global User Setting: Read from
~/.pyenv/version(configured viapyenv global). - System Python: The default binary shipped with your operating system path fallback.
Core Command Referenceβ
Like git, pyenv delegates execution to subcommands based on its primary argument. Below is the full command suite detailing options, attributes, and terminal session outputs.
Version Selection Commandsβ
pyenv localβ
Sets an application-specific Python version by creating or updating a .python-version file in the current working directory.
Flags and Attributesβ
| Flag / Option | Description | Usage Example |
|---|---|---|
<version> ... | One or more Python versions to set locally | pyenv local 3.11.8 3.10.13 |
--unset | Removes local .python-version override in current directory | pyenv local --unset |
-f, --force | Force setting versions even if not currently installed | pyenv local -f 3.12.0 |
Terminal Sessionβ
$ pyenv local 3.11.8
$ cat .python-version
3.11.8
$ pyenv version
3.11.8 (set by /Users/serhiihrekov/projects/api/.python-version)
Advanced Multi-Version Selectionβ
When specifying multiple versions, the first version serves as primary python while remaining versions are accessible through version-qualified binaries:
$ pyenv local 3.11.8 3.10.13
$ pyenv versions
system
* 3.11.8 (set by /Users/serhiihrekov/projects/api/.python-version)
* 3.10.13 (set by /Users/serhiihrekov/projects/api/.python-version)
$ python --version
Python 3.11.8
$ python3.10 --version
Python 3.10.13
Backwards Compatibility: Older versions of
pyenvstored local settings in.pyenv-version.pyenvstill reads.pyenv-versionif present, but a.python-versionfile in the same directory takes precedence.
pyenv globalβ
Sets the default global Python version across all shell sessions by writing to ~/.pyenv/version.
Flags and Attributesβ
| Flag / Option | Description | Usage Example |
|---|---|---|
<version> ... | Target version(s) to activate globally across all shells | pyenv global 3.11.8 |
system | Special version name targeting OS system Python | pyenv global system |
Terminal Sessionβ
$ pyenv global 3.11.8
$ cat ~/.pyenv/version
3.11.8
$ python --version
Python 3.11.8
Advanced Multi-Version Global Setupβ
$ pyenv global 3.11.8 3.9.18
$ pyenv versions
system
3.8.18
* 3.9.18 (set by /Users/serhiihrekov/.pyenv/version)
* 3.11.8 (set by /Users/serhiihrekov/.pyenv/version)
pyenv shellβ
Sets an environment-scoped Python version by defining the PYENV_VERSION variable in your current shell session.
Flags and Attributesβ
| Flag / Option | Description | Usage Example |
|---|---|---|
<version> ... | Python version(s) to set for current shell session | pyenv shell 3.12.1 |
--unset | Unset PYENV_VERSION environment variable | pyenv shell --unset |
Terminal Sessionβ
$ pyenv shell 3.12.1
$ echo $PYENV_VERSION
3.12.1
$ pyenv version
3.12.1 (set by PYENV_VERSION environment variable)
$ pyenv shell --unset
$ pyenv version
3.11.8 (set by /Users/serhiihrekov/projects/api/.python-version)
Toolchain and Installation Managementβ
pyenv installβ
Downloads and compiles Python releases from source using the python-build plugin.
Flags and Attributesβ
| Flag / Option | Description | Usage Example |
|---|---|---|
-l, --list | List all available installable Python versions and distributions | pyenv install --list |
-f, --force | Force compilation and overwrite existing target installation | pyenv install -f 3.11.8 |
-s, --skip-existing | Skip installation cleanly if target version already exists | pyenv install -s 3.11.8 |
-k, --keep | Keep source code trees under $PYENV_BUILD_ROOT | pyenv install -k 3.11.8 |
-v, --verbose | Enable verbose compilation logs to stdout | pyenv install -v 3.11.8 |
-p, --patch | Apply patch from stdin before compiling | cat patch.diff | pyenv install -p 3.11.8 |
-g, --debug | Build a debug binary release | pyenv install -g 3.11.8 |
Terminal Sessionβ
$ pyenv install --list | grep "3.11"
3.11.0
...
3.11.8
$ pyenv install 3.11.8
Downloading Python-3.11.8.tar.xz...
-> https://www.python.org/ftp/python/3.11.8/Python-3.11.8.tar.xz
Installing Python-3.11.8...
Installed Python-3.11.8 to /Users/serhiihrekov/.pyenv/versions/3.11.8
Prefix Resolutionβ
$ pyenv install 3.10
Installing Python-3.10.13...
Installed Python-3.10.13 to /Users/serhiihrekov/.pyenv/versions/3.10.13
pyenv uninstallβ
Removes installed Python distributions from $PYENV_ROOT/versions.
Flags and Attributesβ
| Flag / Option | Description | Usage Example |
|---|---|---|
<version> ... | One or more Python versions to uninstall | pyenv uninstall 3.9.18 |
-f, --force | Force removal without interactive confirmation prompt | pyenv uninstall -f 3.9.18 |
Terminal Sessionβ
$ pyenv uninstall 3.9.18
pyenv: remove /Users/serhiihrekov/.pyenv/versions/3.9.18? y
$ pyenv uninstall -f 3.8.18
# Removes 3.8.18 cleanly without prompting
pyenv latestβ
Resolves and prints the highest installed or known version matching a given prefix.
Flags and Attributesβ
| Flag / Option | Description | Usage Example |
|---|---|---|
<prefix> | Version prefix to search (e.g., 3.11 or 3) | pyenv latest 3.11 |
-k, --known | Search across all available definitions instead of installed | pyenv latest -k 3.11 |
-q, --quiet | Suppress error messages on resolution failure | pyenv latest -q 3.99 |
Terminal Sessionβ
$ pyenv latest 3.10
3.10.13
$ pyenv latest -k 3.12
3.12.2
Environment Introspection and Executable Routingβ
pyenv versionβ
Displays the currently active Python version along with the location or setting mechanism that specified it.
Flags and Attributesβ
| Flag / Option | Description | Usage Example |
|---|---|---|
--bare | Show only version name without path or environment source | pyenv version --bare |
Terminal Sessionβ
$ pyenv version
3.11.8 (set by /Users/serhiihrekov/projects/api/.python-version)
$ pyenv version --bare
3.11.8
pyenv versionsβ
Lists all Python versions known to pyenv, placing an asterisk (*) next to the active version.
Flags and Attributesβ
| Flag / Option | Description | Usage Example |
|---|---|---|
--bare | Show only installed version names without active markers | pyenv versions --bare |
--skip-aliases | Omit version alias symlinks from listed results | pyenv versions --skip-aliases |
Terminal Sessionβ
$ pyenv versions
system
3.9.18
3.10.13
* 3.11.8 (set by /Users/serhiihrekov/projects/api/.python-version)
3.12.1
pyenv whichβ
Displays the full, absolute file path to the executable binary that pyenv will invoke for a given command.
Flags and Attributesβ
| Flag / Option | Description | Usage Example |
|---|---|---|
<command> | Command executable name to locate (e.g. python, pip) | pyenv which pytest |
--nosystem | Exclude OS system $PATH lookups during resolution | pyenv which --nosystem python |
Terminal Sessionβ
$ pyenv which python3
/Users/serhiihrekov/.pyenv/versions/3.11.8/bin/python3
$ pyenv which pytest
/Users/serhiihrekov/.pyenv/versions/3.11.8/bin/pytest
pyenv whenceβ
Lists all installed Python versions that contain a specified binary executable.
Flags and Attributesβ
| Flag / Option | Description | Usage Example |
|---|---|---|
<command> | Executable binary name to query across toolchains | pyenv whence pytest |
--path | Output absolute executable paths instead of version strings | pyenv whence --path pytest |
Terminal Sessionβ
$ pyenv whence pytest
3.10.13
3.11.8
$ pyenv whence --path pytest
/Users/serhiihrekov/.pyenv/versions/3.10.13/bin/pytest
/Users/serhiihrekov/.pyenv/versions/3.11.8/bin/pytest
pyenv execβ
Executes a command by prepending the active Python version's bin path directly to $PATH, bypassing shim overhead.
Flags and Attributesβ
| Flag / Option | Description | Usage Example |
|---|---|---|
<command> [args] | Executable binary and argument list to run directly | pyenv exec pip install -r reqs.txt |
Terminal Sessionβ
$ pyenv exec pip --version
pip 24.0 from /Users/serhiihrekov/.pyenv/versions/3.11.8/lib/python3.11/site-packages/pip (python 3.11)
pyenv rootβ
Displays the absolute path to pyenv's root directory where installed versions, shims, and plugins reside.
Terminal Sessionβ
$ pyenv root
/Users/serhiihrekov/.pyenv
pyenv prefixβ
Displays the installation directory path for one or more Python versions. If no version argument is passed, it outputs the path of the active version.
Flags and Attributesβ
| Flag / Option | Description | Usage Example |
|---|---|---|
[version] | Target version to inspect | pyenv prefix 3.10.13 |
Terminal Sessionβ
$ pyenv prefix
/Users/serhiihrekov/.pyenv/versions/3.11.8
$ pyenv prefix 3.10.13
/Users/serhiihrekov/.pyenv/versions/3.10.13
pyenv shimsβ
Lists all existing binary shims currently registered under $PYENV_ROOT/shims.
Flags and Attributesβ
| Flag / Option | Description | Usage Example |
|---|---|---|
--short | Display only shim executable filenames without full path | pyenv shims --short |
Terminal Sessionβ
$ pyenv shims --short
2to3
idle
pip
pip3
python
python3
pytest
pyenv rehashβ
Regenerates binary shims for all executables present across installed Python versions ($PYENV_ROOT/versions/*/bin/*).
Terminal Sessionβ
$ pyenv rehash
Tip: Run
pyenv rehashafter installing packages that introduce new command-line executables (such asflit,poetry, orcelery) if automatic rehashing is disabled.
pyenv hooksβ
Lists installed hook scripts registered for a given pyenv command lifecycle.
Flags and Attributesβ
| Flag / Option | Description | Usage Example |
|---|---|---|
<command> | Target command lifecycle to inspect hooks for | pyenv hooks rehash |
Terminal Sessionβ
$ pyenv hooks rehash
/Users/serhiihrekov/.pyenv/pyenv.d/rehash/source.bash
Shell Integration and Discoveryβ
pyenv initβ
Generates the shell integration logic required to configure PATH, inject shims, enable tab completion, and activate pyenv shell.
Flags and Attributesβ
| Flag / Option | Description | Usage Example |
|---|---|---|
- | Initialize shims, print PYENV_SHELL, setup completion paths | eval "$(pyenv init -)" |
--path | Output shims path setup statements only | pyenv init --path |
--no-push-path | Prevent pushing shims to start of $PATH if already present | pyenv init --no-push-path |
--no-rehash | Omit automatic rehash execution from shell startup logic | pyenv init --no-rehash |
Terminal Sessionβ
$ pyenv init -
export PATH="/Users/serhiihrekov/.pyenv/shims:${PATH}"
export PYENV_SHELL=zsh
source '/Users/serhiihrekov/.pyenv/libexec/../completions/pyenv.zsh'
pyenv completionsβ
Lists available shell completions for a given pyenv command.
Flags and Attributesβ
| Flag / Option | Description | Usage Example |
|---|---|---|
<command> [arg] | Command and optional arguments to fetch completion targets | pyenv completions install |
Terminal Sessionβ
$ pyenv completions install
--list
--force
--skip-existing
--keep
--verbose
--patch
--debug
pyenv help and pyenv commandsβ
Discovery tools for viewing command documentation and listing registered subcommands.
Terminal Sessionβ
$ pyenv commands
commands
completions
exec
global
help
hooks
init
install
latest
local
prefix
rehash
root
shell
shims
uninstall
version
version-file
version-name
version-origin
versions
whence
which
$ pyenv help install
Usage: pyenv install [-f] [-kvp] <version>
pyenv install [-f] [-kvp] <definition-file>
pyenv install -l|--list
For official project documentation and installation guides, consult the pyenv GitHub repository.
