pyenv - Simple Python version management
Contents
Advanced Configuration
Skip this section unless you must know what every line in your shell profile is doing.
pyenvinit is the only command that crosses the line of loading extra commands into your shell. Coming
from rvm, some of you might be opposed to this idea. Here's what pyenvinit actually does:
1. Setsupyourshimspath. This is the only requirement for pyenv to function properly. You can do this
by hand by prepending $(pyenvroot)/shims to your $PATH.
2. Rehashesshims. From time to time you'll need to rebuild your shim files. Doing this on init makes
sure everything is up to date. You can always run pyenvrehash manually. You can disable this
functionality by adding --no-rehash to the end of your pyenvinit command line.
3. Installstheshdispatcher. This bit is also optional, but allows pyenv and plugins to change
variables in your current shell, making commands like pyenvshell possible. The sh dispatcher doesn't
do anything crazy like override cd or hack your shell prompt, but if for some reason you need pyenv
to be a real script rather than a shell function, you can safely skip it.
To see exactly what happens under the hood for yourself, run "pyenvinit-".
Command Line Options
Like git, the pyenv command delegates to subcommands based on its first argument.
Someusefulpyenvcommandsare:commands
List all available pyenv commands
exec Run an executable with the selected Python version
global
Set or show the global Python version(s)
help Display help for a command
hooks
List hook scripts for a given pyenv command
init Configure the shell environment for pyenv
install
Install a Python version using python-build
local
Set or show the local application-specific Python version(s)
prefix
Display prefix for a Python version
rehash
Rehash pyenv shims (run this after installing executables)
root Display the root directory where versions and shims are kept
shell
Set or show the shell-specific Python version
shims
List existing pyenv shims
uninstall
Uninstall Python versions
version
Show the current Python version(s) and its origin
version-file
Detect the file that sets the current pyenv version
version-name
Show the current Python version
version-origin
Explain how the current Python version is set
versions
List all Python versions available to pyenv
whence
List all Python versions that contain the given executable
which
Display the full path to an executable
See `pyenv help <command>' for information on a specific command. For full documentation, see COMMANDREFERENCE section
Command Reference
The most common subcommands are:
pyenvcommands
Lists all available pyenv commands.
pyenvlocal
Sets a local application-specific Python version by writing the version name to a .python-version file in
the current directory. This version overrides the global version, and can be overridden itself by setting
the PYENV_VERSION environment variable or with the pyenvshell command.
$ pyenv local 2.7.6
When run without a version number, pyenvlocal reports the currently configured local version. You can
also unset the local version:
$ pyenv local --unset
Previous versions of pyenv stored local version specifications in a file named .pyenv-version. For
backwards compatibility, pyenv will read a local version specified in an .pyenv-version file, but a
.python-version file in the same directory will take precedence.
You can specify multiple versions as local Python at once.
Let's say if you have two versions of 2.7.6 and 3.3.3. If you prefer 2.7.6 over 3.3.3,
$ pyenv local 2.7.6 3.3.3
$ pyenv versions
system
* 2.7.6 (set by /Users/yyuu/path/to/project/.python-version)
* 3.3.3 (set by /Users/yyuu/path/to/project/.python-version)
$ python --version
Python 2.7.6
$ python2.7 --version
Python 2.7.6
$ python3.3 --version
Python 3.3.3
or, if you prefer 3.3.3 over 2.7.6,
$ pyenv local 3.3.3 2.7.6
$ pyenv versions
system
* 2.7.6 (set by /Users/yyuu/path/to/project/.python-version)
* 3.3.3 (set by /Users/yyuu/path/to/project/.python-version)
venv27
$ python --version
Python 3.3.3
$ python2.7 --version
Python 2.7.6
$ python3.3 --version
Python 3.3.3
pyenvglobal
Sets the global version of Python to be used in all shells by writing the version name to the
~/.pyenv/version file. This version can be overridden by an application-specific .python-version file, or
by setting the PYENV_VERSION environment variable.
$ pyenv global 2.7.6
The special version name system tells pyenv to use the system Python (detected by searching your $PATH).
When run without a version number, pyenvglobal reports the currently configured global version.
You can specify multiple versions as global Python at once.
Let's say if you have two versions of 2.7.6 and 3.3.3. If you prefer 2.7.6 over 3.3.3,
$ pyenv global 2.7.6 3.3.3
$ pyenv versions
system
* 2.7.6 (set by /Users/yyuu/.pyenv/version)
* 3.3.3 (set by /Users/yyuu/.pyenv/version)
$ python --version
Python 2.7.6
$ python2.7 --version
Python 2.7.6
$ python3.3 --version
Python 3.3.3
or, if you prefer 3.3.3 over 2.7.6,
$ pyenv global 3.3.3 2.7.6
$ pyenv versions
system
* 2.7.6 (set by /Users/yyuu/.pyenv/version)
* 3.3.3 (set by /Users/yyuu/.pyenv/version)
venv27
$ python --version
Python 3.3.3
$ python2.7 --version
Python 2.7.6
$ python3.3 --version
Python 3.3.3
pyenvshell
Sets a shell-specific Python version by setting the PYENV_VERSION environment variable in your shell.
This version overrides application-specific versions and the global version.
$ pyenv shell pypy-2.2.1
When run without a version number, pyenvshell reports the current value of PYENV_VERSION. You can also
unset the shell version:
$ pyenv shell --unset
Note that you'll need pyenv's shell integration enabled (step 3 of the installation instructions) in
order to use this command. If you prefer not to use shell integration, you may simply set the
PYENV_VERSION variable yourself:
$ export PYENV_VERSION=pypy-2.2.1
You can specify multiple versions via PYENV_VERSION at once.
Let's say if you have two versions of 2.7.6 and 3.3.3. If you prefer 2.7.6 over 3.3.3,
$ pyenv shell 2.7.6 3.3.3
$ pyenv versions
system
* 2.7.6 (set by PYENV_VERSION environment variable)
* 3.3.3 (set by PYENV_VERSION environment variable)
$ python --version
Python 2.7.6
$ python2.7 --version
Python 2.7.6
$ python3.3 --version
Python 3.3.3
or, if you prefer 3.3.3 over 2.7.6,
$ pyenv shell 3.3.3 2.7.6
$ pyenv versions
system
* 2.7.6 (set by PYENV_VERSION environment variable)
* 3.3.3 (set by PYENV_VERSION environment variable)
venv27
$ python --version
Python 3.3.3
$ python2.7 --version
Python 2.7.6
$ python3.3 --version
Python 3.3.3
pyenvinstall
Install a Python version
Usage: pyenv install [-f] [-kvp] <version>
pyenv install [-f] [-kvp] <definition-file>
pyenv install -l|--list
-l, --list List all available versions
-f, --force Install even if the version appears to be installed
already
-s, --skip-existing Skip the installation if the version appears to be
installed already
python-build options:
-k, --keep Keep source tree in $PYENV_BUILD_ROOT after installation
(defaults to $PYENV_ROOT/sources)
-v, --verbose Verbose mode: print compilation status to stdout
-p, --patch Apply a patch from stdin before building
-g, --debug Build a debug version
To list the all available versions of Python, including Anaconda, Jython, pypy, and stackless, use:
$ pyenv install --list
Then install the desired versions:
$ pyenv install 2.7.6
$ pyenv install 2.6.8
$ pyenv versions
system
2.6.8
* 2.7.6 (set by /home/yyuu/.pyenv/version)
pyenvuninstall
Uninstall Python versions.
Usage: pyenv uninstall [-f|--force] <version> ...
-f Attempt to remove the specified version without prompting
for confirmation. If the version does not exist, do not
display an error message.
pyenvrehash
Installs shims for all Python binaries known to pyenv (i.e., ~/.pyenv/versions/*/bin/*). Run this command
after you install a new version of Python, or install a package that provides binaries.
$ pyenv rehash
pyenvversion
Displays the currently active Python version, along with information on how it was set.
$ pyenv version
2.7.6 (set by /home/yyuu/.pyenv/version)
pyenvversions
Lists all Python versions known to pyenv, and shows an asterisk next to the currently active version.
$ pyenv versions
2.5.6
2.6.8
* 2.7.6 (set by /home/yyuu/.pyenv/version)
3.3.3
jython-2.5.3
pypy-2.2.1
pyenvwhich
Displays the full path to the executable that pyenv will invoke when you run the given command.
$ pyenv which python3.3
/home/yyuu/.pyenv/versions/3.3.3/bin/python3.3
pyenvwhence
Lists all Python versions with the given command installed.
$ pyenv whence 2to3
2.6.8
2.7.6
3.3.3
Comparison
pyenvdoes...
• Let you changetheglobalPythonversion on a per-user basis.
• Provide support for per-projectPythonversions.
• Allow you to overridethePythonversion with an environment variable.
• Search commands from multipleversionsofPythonatatime. This may be helpful to test across Python
versions with tox
Incontrastwithpythonbrewandpythonz,pyenvdoesnot...
• DependonPythonitself. pyenv was made from pure shell scripts. There is no bootstrap problem of
Python.
• Needtobeloadedintoyourshell. Instead, pyenv's shim approach works by adding a directory to your
$PATH.
• Managevirtualenv. Of course, you can create virtualenv yourself, or pyenv-virtualenv to automate
the process.
Description
pyenv lets you easily switch between multiple versions of Python. It's simple, unobtrusive, and follows
the UNIX tradition of single-purpose tools that do one thing well.
To start using pyenv
1. Append the following to $HOME/.bashrc
if command -v pyenv 1>/dev/null 2>&1;
eval "$(pyenv init - bash)"
fi
Appending this line enables shims. Please make sure this line is placed toward the end of the shell
configuration file since it manipulates PATH during the initialization.
Debiannote: Modify only your ~/.bashrc file instead of creating
~/.bash_profileZshnote: Modify your ~/.zshrc file instead of ~/.bashrcWarning: If you configured your system so that BASH_ENV variable points to .bashrc. You should al‐
most certainly put the above mentioned line into .bash_profile, and not into .bashrc. Otherwise you
may observe strange behaviour, such as pyenv getting into an infinite loop. See #264
https://github.com/pyenv/pyenv/issues/264 for details.
2. Restartyourshellsothepathchangestakeeffect. You can now begin using pyenv.
exec "$SHELL"
3. InstallPythonversionsinto$(pyenvroot)/versions. For example, to download and install Python
3.6.12, run:
pyenvinstall3.6.12NOTE: If you need to pass configure option to build, please use CONFIGURE_OPTS environment variable. If
you are having trouble installing a python version, please visit the wiki page about Common Build Prob‐
lems https://github.com/pyenv/pyenv/wiki/Common-build-problems
Proxynote: If you use a proxy, export HTTP_PROXY and HTTPS_PROXY environment variables.
Stopusingpyenv
The simplicity of pyenv makes it easy to temporarily disable it, or uninstall from the system. To dis‐able pyenv managing your Python versions, simply remove the pyenvinit line from your shell startup con‐
figuration. This will remove pyenv shims directory from PATH, and future invocations like python will ex‐
ecute the system Python version, as before pyenv.
pyenv will still be accessible on the command line, but your Python apps won't be affected by version
switching.
Environment Variables
You can affect how pyenv operates with the following settings:
name(default)descriptionPYENV_VERSION Specifies the Python version to be used. Also see pyenvshellPYENV_ROOT(~/.pyenv) Defines the directory under which Python versions and shims reside. Also see
pyenvrootPYENV_DEBUG Outputs debug information.
Also as: pyenv--debug<subcommand>PYENV_HOOK_PATH Colon-separated list of paths searched for pyenv hooks.
PYENV_DIR($PWD) Directory to start searching for .python-version files.
HTTP_PROXY,HTTPS_PROXY Proxy Variables
CONFIGURE_OPTS Pass configure options to build.
PYTHON_BUILD_ARIA2_OPTS Used to pass additional parameters to aria2 https://aria2.github.io/ If the
aria2c binary is available on PATH, pyenv uses aria2c instead of curl or wget
to download the Python Source code. If you have an unstable internet
connection, you can use this variable to instruct aria2 to accelerate the
download. In most cases, you will only need to use -x10-k1M as value to
PYTHON_BUILD_ARIA2_OPTS environment variable
How It Works
At a high level, pyenv intercepts Python commands using shim executables injected into your PATH, deter‐
mines which Python version has been specified by your application, and passes your commands along to the
correct Python installation.
UnderstandingPATH
When you run a command like python or pip, your operating system searches through a list of directories
to find an executable file with that name. This list of directories lives in an environment variable
called PATH, with each directory in the list separated by a colon:
/usr/local/bin:/usr/bin:/bin
Directories in PATH are searched from left to right, so a matching executable in a directory at the be‐
ginning of the list takes precedence over another one at the end. In this example, the /usr/local/bin di‐
rectory will be searched first, then /usr/bin, then /bin.
UnderstandingShims
pyenv works by inserting a directory of shims at the front of your PATH:
$(pyenv root)/shims:/usr/local/bin:/usr/bin:/bin
Through a process called rehashing, pyenv maintains shims in that directory to match every Python command
(python,pip,etc...) across every installed version of Python
Shims are lightweight executables that simply pass your command along to pyenv. So with pyenv installed,
when you run, say, pip, your operating system will do the following:
• Search your PATH for an executable file named pip
• Find the pyenv shim named pip at the beginning of your PATH
• Run the shim named pip, which in turn passes the command along to pyenv
ChoosingthePythonVersion
When you execute a shim, pyenv determines which Python version to use by reading it from the following
sources, in this order:
1. The PYENV_VERSION environment variable (if specified). You can use the pyenvshell command to set
this environment variable in your current shell session.
2. The application-specific .python-version file in the current directory (if present). You can modify
the current directory's .python-version file with the pyenvlocal command.
3. The first .python-version file found (if any) by searching each parent directory, until reaching the
root of your filesystem.
4. The global $(pyenvroot)/version file. You can modify this file using the pyenvglobal command. If
the global version file is not present, pyenv assumes you want to use the "system" Python. (In other
words, whatever version would run if pyenv weren't in your PATH.)
NOTE: You can activate multiple versions at the same time, including multiple versions of Python2 or
Python3 simultaneously. This allows for parallel usage of Python2 and Python3, and is required with tools
like tox. For example, to set your path to first use your system Python and Python3 (set to 2.7.9 and
3.4.2 in this example), but also have Python 3.3.6, 3.2, and 2.5 available on your PATH, one would first
pyenvinstall the missing versions, then set pyenvglobalsystem3.3.63.22.5. At this point, one should
be able to find the full executable path to each of these using pyenvwhich, e.g. pyenvwhichpython2.5
(should display $(pyenvroot)/versions/2.5/bin/python2.5), or pyenvwhichpython3.4 (should display path
to system Python3). You can also specify multiple versions in a .python-version file, separated by
newlines or any whitespace. hy
LocatingthePythonInstallation
Once pyenv has determined which version of Python your application has specified, it passes the command
along to the corresponding Python installation.
Each Python version is installed into its own directory under
$(pyenvroot)/versions.
For example, you might have these versions installed:
• $(pyenvroot)/versions/2.7.8/
• $(pyenvroot)/versions/3.4.2/
• $(pyenvroot)/versions/pypy-2.4.0/
As far as pyenv is concerned, version names are simply the directories in $(pyenvroot)/versions.
ManagingVirtualEnvironments
There is a pyenv plugin named pyenv-virtualenv which comes with various features to help pyenv users to
manage virtual environments created by virtualenv or Anaconda. Because the activate script of those
virtual environments are relying on mutating $PATH variable of user's interactive shell, it will
intercept pyenv's shim style command execution hooks. We'd recommend to install pyenv-virtualenv as well
if you have some plan to play with those virtual environments.
License
The MIT License
PYENV 24 Apr 2023 PYENV(1)
Name
pyenv - Simple Python version management
Options
-h,--help
Show summary of options.
-v,--version
Show version of program.
Synopsis
pyenv<command> [<args>]
Uninstalling Python Versions
As time goes on, you will accumulate Python versions in your $(pyenvroot)/versions directory.
To remove old Python versions, pyenvuninstall command to automate the removal process.
Alternatively, simply rm-rf the directory of the version you want to remove. You can find the directory
of a particular Python version with the pyenvprefix command,
e.g. pyenvprefix2.6.8.
