| PYENV(1) | General Commands Manual | PYENV(1) |
pyenv - Simple Python version management
pyenv <command> [<args>]
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
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.
Warning: If you configured your system so that BASH_ENV variable points to .bashrc. You should almost 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.
NOTE: 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 Problems https://github.com/pyenv/pyenv/wiki/Common-build-problems
Proxy note: If you use a proxy, export HTTP_PROXY and HTTPS_PROXY environment variables.
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 init line from your shell startup configuration. This will remove pyenv shims directory from PATH, and future invocations like python will execute 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.
Like git, the pyenv command delegates to subcommands based on its first argument.
See `pyenv help <command>' for information on a specific command. For full documentation, see COMMAND REFERENCE section
pyenv does...
In contrast with pythonbrew and pythonz, pyenv does not...
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 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 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.
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:
When you execute a shim, pyenv determines which Python version to use by reading it from the following sources, in this order:
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 pyenv install the 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-version file, separated by newlines or any whitespace. hy
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:
As far as pyenv is concerned, version names are simply the directories in $(pyenv root)/versions.
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.
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 pyenv init actually does:
To see exactly what happens under the hood for yourself, run "pyenv init -".
As time goes on, you will accumulate Python versions in your $(pyenv root)/versions directory.
To remove old Python versions, pyenv uninstall 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 pyenv prefix command,
e.g. pyenv prefix 2.6.8.
The most common subcommands are:
Lists all available pyenv commands.
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 pyenv shell command.
$ pyenv local 2.7.6
When run without a version number, pyenv local 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
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, pyenv global 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
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, pyenv shell 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
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)
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.
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
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)
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
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
Lists all Python versions with the given command installed.
$ pyenv whence 2to3 2.6.8 2.7.6 3.3.3
You can affect how pyenv operates with the following settings:
The MIT License
| 24 Apr 2023 | PYENV |