pyenv
1. Overview
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.
This project was forked from rbenv and ruby-build, and modified for Python.
2. Installation
2.1. Install Python build dependencies
Install Python build dependencies before attempting to install a new Python version.
For macOS:
If you haven't done so, install Xcode Command Line Tools (xcode-select --install) and Homebrew. Then:
2.2. Getting pyenv
2.2.1. Homebrew in macOS
Consider installing with Homebrew:
Then follow the rest of the post-installation steps, starting with Set up your shell environment for pyenv.
2.2.2. Automatic installer
Visit our other project pyenv-installer.
2.3. Upgrading
If you've installed pyenv using Homebrew, upgrade using:
2.4. Set up your shell environment for pyenv
(Optional) Define environment variable
PYENV_ROOTto point to the path where pyenv will store its data.$HOME/.pyenvis the default.(Optional) Add the
pyenvexecutable to yourPATHif it's not already there(Important) run
eval "$(pyenv init -)"to installpyenvinto your shell as a shell function, enable shims and autocompletion(Optional) You may run
eval "$(pyenv init --path)"instead to just enable shims, without shell integration
The below setup should work for the vast majority of users for common use cases.
See Advanced configuration for details and more configuration options.
For Zsh:
If you wish to get pyenv in non-interactive login shells as well, also add the commands to
~/.zprofileor~/.zlogin.
Proxy note: If you use a proxy, export http_proxy and https_proxy, too.
3. Usage
3.1. Install additional Python versions
To search standard python only:
To install additional Python versions, use pyenv install.
For example, to download and install Python 3.11.4, run:
NOTE: Most pyenv-provided Python releases are source releases and are built from source as part of installation (that's why you need Python build dependencies preinstalled).
To install the latest version of Python without giving a specific version use the :latest syntax.
For example, to install the latest patch version for Python
3.8you could do:
To install the latest major release for Python
3try:
3.2. Switch between Python versions
To select a pyenv-installed Python as the version to use, run one of the following commands:
pyenv shell <version>-- select just for current shell sessionpyenv local <version>-- automatically select whenever you are in the current directory (or its subdirectories)pyenv global <version>-- select globally for your user account
E.g. to select the above-mentioned newly-installed Python 3.11.4 as your preferred version to use:
Now whenever you invoke python, pip etc., an executable from the pyenv-provided 3.11.4 installation will be run instead of the system Python.
Using "system" as a version name would reset the selection to your system-provided Python.
If you want to use multiple python versions in shell, you can specify the versions in sequence:
For the scenario above,
pythoncommand will use 3.11.4 version;python3/python3.11/python3.11.4commands will all use 3.11.4 version;python2/python2.7/python2.18commands will use 2.7.18 version.
Note: You can specify multiple versions with pyenv global and invoke them by the specific version number, e.g. pythonX or pythonX.Y or pythonX.Y.Z name.
3.3. Uninstall Python versions
As time goes on, you will accumulate Python versions in your $(pyenv root)/versions directory.
To remove old Python versions, use
pyenv uninstall <version>.Alternatively, you can simply
rm -rfthe directory of the version you want to remove. You can find the directory of a particular Python version with thepyenv prefixcommand, e.g.pyenv prefix 2.6.8. Note however that plugins may run additional operations on uninstall which you would need to do by hand as well. E.g. pyenv-Virtualenv also removes any virtual environments linked to the version being uninstalled.
4. Uninstalling pyenv
The simplicity of pyenv makes it easy to temporarily disable it, or uninstall from the system.
To disable pyenv managing your Python versions, simply remove the
pyenv initinvocations from your shell startup configuration. This will remove pyenv shims directory fromPATH, and future invocations likepythonwill execute the system Python version, as it was before pyenv.(
pyenvwill still be accessible on the command line, but your Python apps won't be affected by version switching.)To completely uninstall pyenv, remove all pyenv configuration lines from your shell startup configuration, and then remove its root directory. This will delete all Python versions that were installed under the
$(pyenv root)/versions/directory:If you've installed pyenv using a package manager, as a final step, perform the pyenv package removal. For instance, for Homebrew:
5. Advanced Configuration
Skip this section unless you must know what every line in your shell profile is doing.
pyenv init 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 eval "$(pyenv init -)" actually does:
Sets up the shims path. This is what allows pyenv to intercept and redirect invocations of
python,pipetc. transparently. It prepends$(pyenv root)/shimsto your$PATH. It also deletes any other instances of$(pyenv root)/shimsonPATHwhich allows to invokeeval "$(pyenv init -)"multiple times without getting duplicatePATHentries.Installs autocompletion. This is entirely optional but pretty useful. Sourcing
$(pyenv root)/completions/pyenv.bashwill set that up. There are also completions for Zsh and Fish.Rehashes shims. 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
pyenv rehashmanually.Installs
pyenvinto the current shell as a shell function. This bit is also optional, but allows pyenv and plugins to change variables in your current shell. This is required for some commands likepyenv shellto work. The sh dispatcher doesn't do anything crazy like overridecdor hack your shell prompt, but if for some reason you needpyenvto be a real script rather than a shell function, you can safely skip it.
Note:
eval "$(pyenv init --path)"only does items 1 and 3.To see exactly what happens under the hood for yourself, run
pyenv init -orpyenv init --path.eval "$(pyenv init -)"is supposed to run at any interactive shell's startup (including nested shells -- e.g. those invoked from editors) so that you get completion and convenience shell functions.eval "$(pyenv init --path)"can be used instead ofeval "$(pyenv init -)"to just enable shims, without shell integration. It can also be used to bump shims to the front ofPATHafter some other logic has prepended stuff toPATHthat may shadow pyenv's shims.
5.1. Using pyenv without shims
If you don't want to use pyenv init and shims, you can still benefit from pyenv's ability to install Python versions for you. Just run pyenv install and you will find versions installed in $(pyenv root)/versions.
You can manually execute or symlink them as required, or you can use pyenv exec <command> whenever you want <command> to be affected by pyenv's version selection as currently configured.
pyenv exec works by prepending $(pyenv root)/versions/<selected version>/bin to PATH in the <command>'s environment, the same as what e.g. RVM does.
5.2. Environment variables
You can affect how pyenv operates with the following environment variables:
PYENV_ROOT
~/.pyenv
Defines the directory under which Python versions and shims reside.
(Also see pyenv root)
PYENV_DEBUG
Outputs debug information.
(Also as: pyenv --debug <subcommand>)
PYENV_DIR
$PWD
Directory to start searching for .python-version files.
PYTHON_BUILD_ARIA2_OPTS
Used to pass additional parameters to aria2.
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 -x 10 -k 1M as value to PYTHON_BUILD_ARIA2_OPTS environment variable
6. How It Works
At a high level, pyenv intercepts Python commands using shim executables injected into your PATH, determines which Python version has been specified by your application, and passes your commands along to the correct Python installation.
6.1. Understanding PATH
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:
Directories in PATH are searched from left to right, so a matching executable in a directory at the beginning of the list takes precedence over another one at the end. In this example, the /usr/local/bin directory will be searched first, then /usr/bin, then /bin.
6.2. Understanding Shims
pyenv works by inserting a directory of shims at the front of your PATH:
Through a process called rehashing, pyenv maintains shims in that directory to match every Python command across every installed version of Python—python, pip, and so on.
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
PATHfor an executable file namedpipFind the pyenv shim named
pipat the beginning of yourPATHRun the shim named
pip, which in turn passes the command along to pyenv
6.3. Understanding Python version selection
When you execute a shim, pyenv determines which Python version to use by reading it from the following sources, in this order:
The
PYENV_VERSIONenvironment variable (if specified). You can use thepyenv shellcommand to set this environment variable in your current shell session.The application-specific
.python-versionfile in the current directory (if present). You can modify the current directory's.python-versionfile with thepyenv localcommand.The first
.python-versionfile found (if any) by searching each parent directory, until reaching the root of your filesystem.The global
$(pyenv root)/versionfile. You can modify this file using thepyenv globalcommand. If the global version file is not present, pyenv assumes you want to use the "system" Python (see below).
A special version name "system" means to use whatever Python is found on PATH after the shims PATH entry (in other words, whatever would be run if pyenv shims weren't on PATH). Note that pyenv considers those installations outside its control and does not attempt to inspect or distinguish them in any way.
So e.g. if you are on MacOS and have OS-bundled Python 3.8.9 and Homebrew-installed Python 3.9.12 and 3.10.2 -- for pyenv, this is still a single "system" version, and whichever of those is first on PATH under the executable name you specified will be run.
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 instruct pyenv to first use your system Python and Python3 (which are e.g. 2.7.9 and 3.4.2) but also have Python 3.3.6, 3.2.1, and 2.5.2 available, you first
pyenv installthe missing versions,then set
pyenv global system 3.3.6 3.2.1 2.5.2. Then you'll be able to invoke any of those versions with an appropriatepythonXorpythonX.Yname.You can also specify multiple versions in a
.python-versionfile by hand, separated by newlines. Lines starting with a#are ignored.
pyenv which <command> displays which real executable would be run when you invoke <command> via a shim.
E.g. if you have 3.3.6, 3.2.1 and 2.5.2 installed of which 3.3.6 and 2.5.2 are selected and your system Python is 3.2.5, pyenv which python2.5 should display $(pyenv root)/versions/2.5.2/bin/python2.5, pyenv which python3 -- $(pyenv root)/versions/3.3.6/bin/python3 and pyenv which python3.2 -- path to your system Python due to the fall-through (see below).
Shims also fall through to anything further on PATH if the corresponding executable is not present in any of the selected Python installations.
This allows you to use any programs installed elsewhere on the system as long as they are not shadowed by a selected Python installation.
6.4. Locating pyenv-provided Python installations
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 $(pyenv root)/versions.
For example, you might have these versions installed:
$(pyenv root)/versions/2.7.8/$(pyenv root)/versions/3.4.2/$(pyenv root)/versions/pypy-2.4.0/
As far as pyenv is concerned, version names are simply directories under $(pyenv root)/versions.
7. Command Reference
Like git, the pyenv command delegates to subcommands based on its first argument.
7.1. pyenv global
pyenv globalSets 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.
The special version name system tells pyenv to use the system Python (detected by searching your $PATH).
Note: When running without a version number, pyenv global prints the currently configured global version.
7.1.1. pyenv global (advanced)
pyenv global (advanced)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,
or, if you prefer 3.3.3 over 2.7.6,
7.2. pyenv local
pyenv localSets 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 pyenv shell command.
Note: When run without a version number, pyenv local prints the currently configured local version.
You can also unset the local version:
7.2.1. pyenv local (advanced)
pyenv local (advanced)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,
or, if you prefer 3.3.3 over 2.7.6,
7.3. pyenv shell
pyenv shellSets 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. (Priority level: pyenv shell > pyenv local > pyenv global)
Note: When run without a version number, pyenv shell prints the current value of PYENV_VERSION.
You can also unset the shell version:
7.3.1. Set PYENV_VERSION Manually
PYENV_VERSION ManuallyNote 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:
And also unset it manually:
7.3.2. pyenv shell (advanced)
pyenv shell (advanced)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,
or, if you prefer 3.3.3 over 2.7.6,
7.4. pyenv install
pyenv installInstall a Python version (using python-build).
To list the all available versions of Python, including Anaconda, Jython, pypy, and stackless, use:
To install the latest version of Python without giving a specific version use the :latest syntax.
For example, to install the latest patch version for Python
3.8you could do:
To install the latest major release for Python
3try:
7.5. pyenv uninstall
pyenv uninstallUninstall a specific Python version.
7.6. pyenv rehash
pyenv rehashInstalls 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.
7.7. pyenv version
pyenv versionDisplays the currently active Python version, along with information on how it was set.
7.8. pyenv versions
pyenv versionsLists all Python versions known to pyenv, and shows an asterisk next to the currently active version.
7.9. pyenv which
pyenv whichDisplays the full path to the executable that pyenv will invoke when you run the given command.
Use --nosystem argument in case when you don't need to search command in the system environment.
7.10. pyenv whence
pyenv whenceLists all Python versions with the given command installed.
7.11. pyenv exec
pyenv execRuns an executable by first preparing PATH so that the selected Python version's bin directory is at the front.
For example, if the currently selected Python version is 3.9.7:
is equivalent to:
7.12. pyenv root
pyenv rootDisplays the root directory where versions and shims are kept.
7.13. pyenv prefix
pyenv prefixDisplays the directories where the given Python versions are installed, separated by colons. If no version is given, pyenv prefix displays the locations of the currently selected versions.
7.14. pyenv hooks
pyenv hooksLists installed hook scripts for a given pyenv command.
7.15. pyenv shims
pyenv shimsList existing pyenv shims.
7.16. pyenv init
pyenv initConfigure the shell environment for pyenv
7.17. pyenv completions
pyenv completionsLists available completions for a given pyenv command.
8. Compare with other version tools
8.1. What pyenv does...
Lets you change the global Python version on a per-user basis.
Provides support for per-project Python versions.
Allows you to override the Python version with an environment variable.
Searches for commands from multiple versions of Python at a time. This may be helpful to test across Python versions with tox.
8.2. In contrast with pythonbrew and pythonz, pyenv does not...
Depend on Python itself. pyenv was made from pure shell scripts. There is no bootstrap problem of Python.
Need to be loaded into your shell. Instead, pyenv's shim approach works by adding a directory to your
PATH.Manage virtualenv. Of course, you can create virtualenv yourself, or pyenv-virtualenv to automate the process.
9. Development
The pyenv source code is hosted on GitHub. It's clean, modular, and easy to understand, even if you're not a shell hacker.
Tests are executed using Bats:
Last updated