Simple Python version management
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.
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.
When you run a command like
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:
PATHare 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/bindirectory will be searched first, then
pyenv works by inserting a directory of shims at the front of your
Through a process called rehashing, pyenv maintains shims in that directory to match every Python command across every installed version of 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:
PATHfor an executable file named
pipat the beginning of your
pip, which in turn passes the command along to pyenv
When you execute a shim, pyenv determines which Python version to use by reading it from the following sources, in this order:
PYENV_VERSIONenvironment variable (if specified). You can use the
pyenv shellcommand to set this environment variable in your current shell session.
.python-versionfile in the current directory (if present). You can modify the current directory's
.python-versionfile with the
.python-versionfile found (if any) by searching each parent directory, until reaching the root of your filesystem.
$(pyenv root)/versionfile. You can modify this file using the
pyenv globalcommand. 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
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
systemPython 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
pyenv installthe missing versions, then set
pyenv global system 3.3.6 3.2 2.5. At this point, one should be able to find the full executable path to each of these using
pyenv which, e.g.
pyenv which python2.5(should display
$(pyenv root)/versions/2.5/bin/python2.5), or
pyenv which python3.4(should display path to system Python3). You can also specify multiple versions in a
.python-versionfile, separated by newlines. Lines starting with a
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
For example, you might have these versions installed:
As far as Pyenv is concerned, version names are simply directories under
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
activatescript of those virtual environments are relying on mutating
$PATHvariable 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.
For pyenv to install python correctly you should install the Python build dependencies.
sh brew update brew install pyenv
Visit our other project: https://github.com/pyenv/pyenv-installer
This will get you going with the latest version of Pyenv and make it easy to fork and contribute any changes back upstream.
Check out Pyenv where you want it installed. A good place to choose is
$HOME/.pyenv(but you can install it somewhere else):
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
Optionally, try to compile a dynamic Bash extension to speed up Pyenv. Don't worry if it fails; Pyenv will still work normally:
cd ~/.pyenv && src/configure && make -C src
Note: The below instructions for specific shells are designed for common shell setups.
If you have an uncommon setup and they don't work for you, use the guidance text and the Advanced Configuration section below to figure out what you need to do in your specific case.
Adjust the session-wide environment for your account. Define the
PYENV_ROOTenvironment variable to point to the path where you cloned the Pyenv repo, add the
pyenvcommand-line utility to your
PATH, run the output of
pyenv init --pathto enable shims.
These commands need to be added into your shell startup files in such a way that they are executed only once per session, by its login shell. This typically means they need to be added into a per-user shell-specific
~/.*profilefile, and into
~/.profile, too, so that they are also run by GUI managers (which typically act as a
MacOS note: If you installed Pyenv with Homebrew, you don't need to add the
PATH=lines. You also don't need to add commands into
~/.profileif your shell doesn't use it.
- For **bash**:
~~~ bash echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.profile echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.profile echo 'eval "$(pyenv init --path)"' >> ~/.profile ~~~ - **If your `~/.profile` sources `~/.bashrc` (Debian, Ubuntu, Mint):** Put these lines into `~/.profile` _before_ the part that sources `~/.bashrc`: ~~~bash export PYENV_ROOT="$HOME/.pyenv" export PATH="$PYENV_ROOT/bin:$PATH" eval "$(pyenv init --path)" ~~~ <!--This is an alternative option and needn't be replicated to `pyenv init`--> Alternatively, for an automated installation, you can run the following: ~~~ bash echo -e 'if shopt -q login_shell; then' \ '\n export PYENV_ROOT="$HOME/.pyenv"' \ '\n export PATH="$PYENV_ROOT/bin:$PATH"' \ '\n eval "$(pyenv init --path)"' \ '\nfi' >> ~/.bashrc echo -e 'if [ -z "$BASH_VERSION" ]; then'\ '\n export PYENV_ROOT="$HOME/.pyenv"'\ '\n export PATH="$PYENV_ROOT/bin:$PATH"'\ '\n eval "$(pyenv init --path)"'\ '\nfi' >>~/.profile ~~~ **Note:** If you have `~/.bash_profile`, make sure that it too executes the above-added commands, e.g. by copying them there or by `source`'ing `~/.profile`.
Same as for Bash above, but add the commands into both
For Fish shell:
Execute this interactively:
set -Ux PYENV_ROOT $HOME/.pyenv set -U fish_user_paths $PYENV_ROOT/bin $fish_user_paths
And add this to
status is-login; and pyenv init --path | source
If Fish is not your login shell, also follow the Bash/Zsh instructions to add to
Proxy note: If you use a proxy, export
pyenvinto your shell by running the output of
pyenv init -to enable autocompletion and all subcommands.
This command needs to run at startup of any interactive shell instance. In an interactive login shell, it needs to run after the commands from the previous step.
- For **bash**: ~~~ bash echo 'eval "$(pyenv init -)"' >> ~/.bashrc ~~~
- **If your `/etc/profile` sources `~/.bashrc` (SUSE):** ~~~bash echo 'if command -v pyenv >/dev/null; then eval "$(pyenv init -)"; done' >> ~/.bashrc ~~~
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
For Fish shell:
Add this to
pyenv init - | source
General warning: There are some systems where the
BASH_ENV variable is configured
to point to
.bashrc. On such systems you should almost certainly put the above-mentioned line
eval "$(pyenv init -)" into
.bash_profile, and not into
.bashrc. Otherwise you
may observe strange behaviour, such as
pyenv getting into an infinite loop.
See #264 for details.
In MacOS, restarting terminal windows is enough (because MacOS runs shells in them as login shells by default).
Install Python build dependencies before attempting to install a new Python version.
Install Python versions into
$(pyenv root)/versions. For example, to download and install Python 2.7.8, run:
sh pyenv install 2.7.8NOTE: If you need to pass a
configureoption to a build, please use the
NOTE: If you want to use proxy to download, please set the
NOTE: If you are having trouble installing a Python version, please visit the wiki page about Common Build Problems.
If you've installed Pyenv using Homebrew, upgrade using:
sh brew upgrade pyenv
If you've installed Pyenv using the instructions above, you can upgrade your installation at any time using Git.
To upgrade to the latest development version of pyenv, use
cd $(pyenv root) git pull
To upgrade to a specific release of Pyenv, check out the corresponding tag:
cd $(pyenv root) git fetch git tag git checkout v0.1.0
The simplicity of pyenv makes it easy to temporarily disable it, or uninstall from the system.
pyenv initinvocations from your shell startup configuration. This will remove Pyenv shims directory from
PATH, and future invocations like
pythonwill 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.
rm -rf $(pyenv root)
If you've installed Pyenv using a package manager, as a final step, perform the Pyenv package removal. For instance, for Homebrew:
brew uninstall pyenv
Skip this section unless you must know what every line in your shell profile is doing.
pyenv initis 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
pyenv initactually does. Step 1 is done by
eval "$(pyenv init --path)", the others are done by
eval "$(pyenv init -)".
Sets up your shims path. This is the only requirement for pyenv to function properly. You can do this by hand by prepending
$(pyenv root)/shimsto your
Installs autocompletion. This is entirely optional but pretty useful. Sourcing
$(pyenv root)/completions/pyenv.bashwill set that up. There is also a
$(pyenv root)/completions/pyenv.zshfor Zsh users.
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
Installs the sh dispatcher. This bit is also optional, but allows pyenv and plugins to change variables in your current shell, making commands like
pyenv shellpossible. The sh dispatcher doesn't do anything crazy like override
cdor hack your shell prompt, but if for some reason you need
pyenvto 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
pyenv init -or
pyenv init --path.
If you don't want to use
pyenv initand shims, you can still benefit from pyenv's ability to install Python versions for you. Just run
pyenv installand you will find versions installed in
$(pyenv root)/versions, which you can manually execute or symlink as required.
As time goes on, you will accumulate Python versions in your
To remove old Python versions,
pyenv uninstallcommand to automate the removal process.
rm -rfthe directory of the version you want to remove. You can find the directory of a particular Python version with the
pyenv prefixcommand, e.g.
pyenv prefix 2.6.8.
You can affect how pyenv operates with the following settings:
PYENV_VERSION| | Specifies the Python version to be used.
~/.pyenv| Defines the directory under which Python versions and shims reside.
PYENV_DEBUG| | Outputs debug information.
PYENV_HOOK_PATH| see wiki | Colon-separated list of paths searched for pyenv hooks.
$PWD| Directory to start searching for
PYTHON_BUILD_ARIA2_OPTS| | Used to pass additional parameters to
aria2cbinary is available on PATH, pyenv uses
wgetto download the Python Source code. If you have an unstable internet connection, you can use this variable to instruct
aria2to accelerate the download.
-x 10 -k 1Mas value to
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:
bats test bats/test/.bats
Please feel free to submit pull requests and file bugs on the issue tracker.