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.
- 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.
- 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.
- Installation
- Usage
- Upgrading
- Uninstalling pyenv
- Pyenv plugins
- How It Works
- Advanced Configuration
- Development
The Homebrew option from the MacOS section below would also work if you have Homebrew installed.
curl -fsSL https://pyenv.run | bash
For more details 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
The options from the Linux section above also work but Homebrew is recommended for basic usage.
Homebrew in macOS
-
Update homebrew and install pyenv:
brew update brew install pyenv
If you want to install (and update to) the latest development head of Pyenv rather than the latest release, instead run:
brew install pyenv --head
-
Then follow the rest of the post-installation steps, starting with Set up your shell environment for Pyenv.
-
OPTIONAL. To fix
brew doctor
's warning ""config" scripts exist outside your system or Homebrew directories"If you're going to build Homebrew formulae from source that link against Python like Tkinter or NumPy (This is only generally the case if you are a developer of such a formula, or if you have an EOL version of MacOS for which prebuilt bottles are no longer provided and you are using such a formula).
To avoid them accidentally linking against a Pyenv-provided Python, add the following line into your interactive shell's configuration:
-
Bash/Zsh:
alias brew='env PATH="${PATH//$(pyenv root)\/shims:/}" brew'
-
Fish:
alias brew="env PATH=(string replace (pyenv root)/shims '' \"\$PATH\") brew"
-
Pyenv does not officially support Windows and does not work in Windows outside the Windows Subsystem for Linux. Moreover, even there, the Pythons it installs are not native Windows versions but rather Linux versions running in a virtual machine -- so you won't get Windows-specific functionality.
If you're in Windows, we recommend using @kirankotari's pyenv-win
fork --
which does install native Windows Python versions.
The below setup should work for the vast majority of users for common use cases. See Advanced configuration for details and more configuration options.
Stock Bash startup files vary widely between distributions in which of them source
which, under what circumstances, in what order and what additional configuration they perform.
As such, the most reliable way to get Pyenv in all environments is to append Pyenv
configuration commands to both .bashrc
(for interactive shells)
and the profile file that Bash would use (for login shells).
-
First, add the commands to
~/.bashrc
by running the following in your terminal:echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc echo 'eval "$(pyenv init - bash)"' >> ~/.bashrc
-
Then, if you have
~/.profile
,~/.bash_profile
or~/.bash_login
, add the commands there as well. If you have none of these, create a~/.profile
and add the commands there.- to add to
~/.profile
:echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.profile echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.profile echo 'eval "$(pyenv init - bash)"' >> ~/.profile
- to add to
~/.bash_profile
:echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile echo 'eval "$(pyenv init - bash)"' >> ~/.bash_profile
- to add to
Bash 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
eval "$(pyenv init - bash)"
line 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.
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init - zsh)"' >> ~/.zshrc
If you wish to get Pyenv in noninteractive login shells as well, also add the commands to ~/.zprofile
or ~/.zlogin
.
-
If you have Fish 3.2.0 or newer, execute this interactively:
set -Ux PYENV_ROOT $HOME/.pyenv test -d $PYENV_ROOT/bin; and fish_add_path $PYENV_ROOT/bin
-
Otherwise, execute the snippet below:
set -Ux PYENV_ROOT $HOME/.pyenv test -d $PYENV_ROOT/bin; and set -U fish_user_paths $PYENV_ROOT/bin $fish_user_paths
-
Now, add this to
~/.config/fish/config.fish
:pyenv init - fish | source
Add the following lines to your config.nu
to add Pyenv and its shims to your PATH
.
Shell integration (completions and subcommands changing the shell's state)
isn't currently supported.
$env.PYENV_ROOT = "~/.pyenv" | path expand
if (( $"($env.PYENV_ROOT)/bin" | path type ) == "dir") {
$env.PATH = $env.PATH | prepend $"($env.PYENV_ROOT)/bin" }
$env.PATH = $env.PATH | prepend $"(pyenv root)/shims"
for the PATH
changes to take effect.
exec "$SHELL"
Install Python build dependencies before attempting to install a new Python version.
You can now begin using Pyenv.
if you have upgraded from pyenv version 2.0.x-2.2.x
The startup logic and instructions have been updated for simplicity in 2.3.0. The previous, more complicated configuration scheme for 2.0.0-2.2.5 still works.
- Define environment variable
PYENV_ROOT
to point to the path where Pyenv will store its data.$HOME/.pyenv
is the default. If you installed Pyenv via Git checkout, we recommend to set it to the same location as where you cloned it. - Add the
pyenv
executable to yourPATH
if it's not already there - run
eval "$(pyenv init -)"
to installpyenv
into your shell as a shell function, enable shims and autocompletion- You may run
eval "$(pyenv init --path)"
instead to just enable shims, without shell integration
- You may run
To install additional Python versions, use pyenv install
.
For example, to download and install Python 3.10.4, run:
pyenv install 3.10.4
Running pyenv install -l
gives the list of all available versions.
Notes about python releases
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).
You can pass options to Python's configure
and compiler flags to customize the build,
see Special environment variables in Python-Build's README
for details.
NOTE: If you are having trouble installing a Python version, please visit the wiki page about Common Build Problems.
NOTE: If you want to use proxy for download, please set the http_proxy
and https_proxy
environment variables.
NOTE: If you'd like a faster interpreter at the cost of longer build times, see Building for maximum performance in Python-Build's README.
All Pyenv subcommands except uninstall
automatically resolve full prefixes to the latest version in the corresponding version line.
pyenv install
picks the latest known version, while other subcommands pick the latest installed version.
E.g. to install and then switch to the latest 3.10 release:
pyenv install 3.10
pyenv global 3.10
You can run pyenv latest -k <prefix>
to see how pyenv install
would resolve a specific prefix, or pyenv latest <prefix>
to see how other subcommands would resolve it.
See the pyenv latest
documentation for details.
For the following Python releases, Pyenv applies user-provided patches that add support for some newer environments. Though we don't actively maintain those patches, since existing releases never change, it's safe to assume that they will continue working until there are further incompatible changes in a later version of those environments.
- 3.7.8-3.7.15, 3.8.4-3.8.12, 3.9.0-3.9.7 : XCode 13.3
- 3.5.10, 3.6.15 : MacOS 11+ and XCode 13.3
- 2.7.18 : MacOS 10.15+ and Apple Silicon
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.10.4 as your preferred version to use:
pyenv global 3.10.4
Now whenever you invoke python
, pip
etc., an executable from the Pyenv-provided
3.10.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.
See Understanding shims and Understanding Python version selection for more details on how the selection works and more information on its usage.
You can select multiple Python versions at the same time by specifying multiple arguments. E.g. if you wish to use the latest installed CPython 3.11 and 3.12:
pyenv global 3.11 3.12
Whenever you run a command provided by a Python installation, these versions will be searched for it in the specified order.
Due to the shims' fall-through behavior, system
is always implicitly searched afterwards.
As time goes on, you will accumulate Python versions in your
$(pyenv root)/versions
directory.
To remove old Python versions, use pyenv uninstall <versions>
.
Alternatively, you can simply rm -rf
the directory of the version you want
to remove. You can find the directory of a particular Python version
with the pyenv prefix
command, 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.
Run pyenv commands
to get a list of all available subcommands.
Run a subcommand with --help
to get help on it, or see the Commands Reference.
Note that Pyenv plugins that you install may add their own subcommands.
If you've installed Pyenv using Homebrew, upgrade using:
brew upgrade pyenv
To switch from a release to the latest development head of Pyenv, use:
brew uninstall pyenv
brew install pyenv --head
then you can upgrade it with brew upgrade pyenv
as usual.
If you've installed Pyenv with Pyenv-installer, you likely have the Pyenv-Update plugin that would upgrade Pyenv and all installed plugins:
< 6D40 div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="pyenv update">pyenv update