PYENV(1)
πΈ NAME
pyenv is simple Python version management, and also like the PYENV(1) manual page from the General Commands Manual. It's giving "your Python versions? handled." π
β¨ SYNOPSIS
pyenv <command> [<args>]
π¦ DESCRIPTION
Okay so 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. No cap, that's the whole brand. π«Ά
π To start using pyenv
Bestie, here's the starter pack:
- 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.
Debian note: Modify only your ~/.bashrc file instead of creating ~/.bash_profile.
Zsh note: Modify your ~/.zshrc file instead of ~/.bashrc.
Warning: If you configured your system so thatBASH_ENVvariable 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 aspyenvgetting into an infinite loop. See #264https://github.com/pyenv/pyenv/issues/264for details. π
- Restart your shell so the path changes take effect. You can now begin using
pyenv.
exec "$SHELL"
- Install Python versions into
$(pyenv root)/versions. For example, to download and install Python3.6.12, run:
pyenv install 3.6.12
NOTE: If you need to pass configure option to build, please useCONFIGURE_OPTSenvironment variable. If you are having trouble installing a python version, please visit the wiki page about Common Build Problemshttps://github.com/pyenv/pyenv/wiki/Common-build-problemsπ₯Ί
Proxy note: If you use a proxy, export HTTP_PROXY and HTTPS_PROXY environment variables. Slay. π
π Stop using 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 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. No drama. π«Ά
π₯ COMMAND LINE OPTIONS
Like git, the pyenv command delegates to subcommands based on its first argument. Main character behavior fr fr. π
π Some useful pyenv commands are:
commandsβ List all availablepyenvcommands.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 givenpyenvcommand.initβ Configure the shell environment forpyenv.installβ Install a Python version usingpython-build.localβ Set or show the local application-specific Python version(s).prefixβ Display prefix for a Python version.rehashβ Rehashpyenvshims (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 existingpyenvshims.uninstallβ Uninstall Python versions.versionβ Show the current Python version(s) and its origin.version-fileβ Detect the file that sets the currentpyenvversion.version-nameβ Show the current Python version.version-originβ Explain how the current Python version is set.versionsβ List all Python versions available topyenv.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 COMMAND REFERENCE section. β¨
π OPTIONS
-h,--helpβ Show summary of options.-v,--versionβ Show version of program.
π COMPARISON
pyenv does...
- Let you change the global Python version on a per-user basis.
- Provide support for per-project Python versions.
- Allow you to override the Python version with an environment variable.
- Search commands from multiple versions of Python at a time. This may be helpful to test across Python versions with
tox.
In contrast with pythonbrew and pythonz, pyenv does not...
- Depend on Python itself.
pyenvwas made from pure shell scripts. There is no bootstrap problem of Python. Iconic. - 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 createvirtualenvyourself, orpyenv-virtualenvto automate the process.
π§ 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. The way that works? Obsessed. π¦
πΈ 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:
/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. Order matters, bestie. π
π¦ Understanding Shims
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
PATHfor an executable file namedpip - Find the
pyenvshim namedpipat the beginning of yourPATH - Run the shim named
pip, which in turn passes the command along topyenv
π Choosing the Python Version
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,pyenvassumes you want to use the "system" Python. (In other words, whatever version would run ifpyenvweren't in yourPATH.)
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 liketox. For example, to set your path to first use your system Python and Python3 (set to2.7.9and3.4.2in this example), but also have Python3.3.6,3.2, and2.5available on yourPATH, one would firstpyenv installthe missing versions, then setpyenv 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 usingpyenv which, e.g.pyenv which python2.5(should display$(pyenv root)/versions/2.5/bin/python2.5), orpyenv which python3.4(should display path to system Python3). You can also specify multiple versions in a.python-versionfile, separated by newlines or any whitespace.hyπ
π§ Locating the Python Installation
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 the directories in $(pyenv root)/versions. Periodt. π
π Managing Virtual Environments
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. π«Ά
π Advanced Configuration
Skip this section unless you must know what every line in your shell profile is doing. (But if you're that girl, hi, welcome.) π
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:
- Sets up your shims path. This is the only requirement for
pyenvto function properly. You can do this by hand by prepending$(pyenv root)/shimsto your$PATH. - 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. You can disable this functionality by adding--no-rehashto the end of yourpyenv initcommand line. - Installs the sh dispatcher. This bit is also optional, but allows
pyenvand plugins to change variables in your current shell, making commands likepyenv shellpossible. 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.
To see exactly what happens under the hood for yourself, run pyenv init -. β¨
ποΈ Uninstalling Python Versions
okay so real talk bestie β over time you WILL accumulate Python versions in your $(pyenv root)/versions directory. it's giving hoarder closet but make it interpreters. π
to remove old Python versions, there's the pyenv uninstall command, which automates the removal process for you. slay.
alternatively, you can simply rm -rf the directory of the version you want to remove (iconic but scary π). you can find the directory of a particular Python version with the pyenv prefix command,
e.g. pyenv prefix 2.6.8.
π Command Reference
the most common subcommands are (brace yourself, we're going through ALL of them):
pyenv commands
Lists all available pyenv commands. that's it, that's the whole vibe β a little menu of everything you can do. β¨
pyenv local
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. it's a whole hierarchy, iykyk. π
$ 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. (the old file still gets read, it's just not the main character anymore π«Ά)
You can specify multiple versions as local Python at once. yes bestie, a whole squad.
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
see how the ORDER changes which one answers to plain python? first one listed wins, no cap. π
pyenv global
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. it's the default default, the fallback queen. π
$ pyenv global 2.7.6
The special version name system tells pyenv to use the system Python (detected by searching your $PATH). lowkey useful when you just want the OS to handle it.
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
pyenv shell
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. it's the top of the food chain for exactly as long as this shell lives. π
$ 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
pyenv install
Install a Python version. the main event, the moment, the reason we're all here. π₯
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)
pyenv uninstall
Uninstall Python versions. bye bestie, we hardly knew ye π¦
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.
the -f here is doing double duty: no confirmation prompt AND no error message if the version isn't there. so it's the "don't talk to me" flag basically. π
pyenv rehash
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. it's the "okay I changed stuff, please notice" button. β
$ pyenv rehash
pyenv version
Displays the currently active Python version, along with information on how it was set. receipts included, love that for us. π
$ pyenv version
2.7.6 (set by /home/yyuu/.pyenv/version)
pyenv versions
Lists all Python versions known to pyenv, and shows an asterisk next to the currently active version. a whole roster, with the star player marked. π
$ 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
pyenv which
Displays the full path to the executable that pyenv will invoke when you run the given command. so you can see exactly which file is about to get summoned. π§
$ pyenv which python3.3
/home/yyuu/.pyenv/versions/3.3.3/bin/python3.3
pyenv whence
Lists all Python versions with the given command installed. like a guest list check β who here has this binary? π
$ pyenv whence 2to3
2.6.8
2.7.6
3.3.3
π Environment variables
You can affect how pyenv operates with the following settings. here's the whole roster, name (default) first, then what it does:
PYENV_VERSIONβ Specifies the Python version to be used. Also seepyenv shell.PYENV_ROOT(~/.pyenv) β Defines the directory under which Python versions and shims reside. Also seepyenv root.PYENV_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-versionfiles.HTTP_PROXY,HTTPS_PROXYβ Proxy Variables.CONFIGURE_OPTSβ Pass configure options to build.PYTHON_BUILD_ARIA2_OPTSβ Used to pass additional parameters to aria2https://aria2.github.io/. If thearia2cbinary is available onPATH, pyenv usesaria2cinstead ofcurlorwgetto 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 1Mas value toPYTHON_BUILD_ARIA2_OPTSenvironment variable.
π License
The MIT License. short, sweet, legally iconic. periodt.
and that's the whole page bestie, you made it to the end!! π«Ά source/version check: PYENV, dated 24 Apr 2023, filed under PYENV(1) β go forth and manage those interpreters like the main character you are. π
π¦ where this page comes from
- debian trixie: pyenv 2.5.4-1 Β· MIT
- ubuntu questing: pyenv 2.5.4-1build1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms