Use pyenv for easier Python version management
Toc
Jump right to a section:
TL;DR
If you're just here for a quick reference, use the sections below for quick pyenv setup & usage instructions. Any sections linked here will have a button that returns you to this TL;DR section, so if you're curious about one of the steps and follow a link you can get right back to it when you're done reading.
This is what the button will look like: ⤴️ back to TL;DR
TLDR 1: Install pyenv
See the Install pyenv section for OS-specific installation instructions.
Warning
If you skip the installation instructions, make sure you add pyenv to your PATH. If you need help setting PATH variables, please read the Install pyenv section.
- Linux/WSL:
- Edit
~/.bashrc, add this to the bottom of the file (if in a terminal environment, use an editor like$ nano ~.bashrc):
- Edit
| Add pyenv to PATH | |
|---|---|
- Windows:
- Edit the user's
PATHvariable, add these 2 paths:
- Edit the user's
TLDR 2: Choose from available Python versions
Show versions available for install. Update the list with $ pyenv update if you don't see the version you want.
| list & install python versions | |
|---|---|
TLDR 3: Set your global, local, & shell Python versions
Warning
The 3.12.1 version string is used as an example throughout the documentation. Make sure you're using a more recent version, if one is available.
You can check by running $ pyenv update && pyenv install -l.
Examples commands
- Set
globalversion to3.12.1:$ pyenv global 3.12.1 - Set
globalto multiple versions,3.12.1+3.11.8:$ pyenv global 3.12.1 3.11.8-
Version-order
The order of the versions you specify does matter. For example, in the command above,3.12.1is the "primary" Python, but3.11.8will also be available for tools likepytest, which can run tests against multiple versions of Python, provided they are available in thePATH.
-
- Set
localversion to3.11.8, creating a.python-versionfile in the process:$ pyenv local 3.11.8- Setting a
localversion will override theglobalsetting
- Setting a
- Set
localto multiple versions,3.12.1+3.11.8:$ pyenv local 3.12.1 3.11.8 - Set
shellversion to `3.
Pyenv cheat sheet
| Command | Description | Notes |
|---|---|---|
pyenv commands |
List available pyenv commands | Useful as a quick reference if you forget a command name. Does not include help/man text. To see available help for a command, run it without any parameters, i.e. pyenv whence |
pyenv update |
Refresh pyenv's repository | Updates the pyenv utility, fetches new available Python versions, etc. Run this command occasionally to keep everything up to date. |
pyenv install 3.xx.xx |
Install a version of Python | If you don't see the version you want, run pyenv update |
pyenv uninstall 3.xx.xx |
Uninstall a version of Python that was installed with pyenv. |
You can uninstall multiple versions at a time with pyenv uninstall 3.11.2 3.11.4 |
pyenv global 3.xx.xx |
Set the global/default Python version to use. | You can set multiple versions like pyenv global 3.11.4 3.11.6 3.12.2. Run without any args to print the current global Python interpreter(s). |
pyenv local 3.xx.xx |
Set the local Python interpreter. | Creates a file in the current directory .python-version, which pyenv will detect and will set your interpreter to the version specified. Like pyenv global, you can set multiple versions with pyenv local 3.11.4 3.11.6 3.12.2. Run without any args to print the current global Python interpreter(s). |
pyenv shell 3.xx.xx |
Set the Python interpreter for the current shell session. | Resets when session exits. Like pyenv global, you can set multiple versions with pyenv shell 3.11.4 3.11.6 3.12.2. Run without any args to print the current global Python interpreter(s). |
pyenv versions |
List versions of Python installed with Pyenv & available for use. | Versions will be printed as a list of Python version numbers, and may include interpreter paths. |
pyenv which <executable> |
Print the path to a pyenv-installed executable, i.e. pip |
Helps with troubleshooting unexpected behavior. If you suspect pyenv is using the wrong interpreter, check the path with pyenv which python, for example. |
pyenv rehash |
Rehash pyenv shims. | Run this after switching Python versions with pyenv if you're getting unexpected outputs. |
What is pyenv
pyenv is a tool for managing versions of Python. It handles downloading the version archive, extracting & installing, and can isolate different versions of Python into their own individual environments.
pyenv can also handle running multiple versions of Python at the same time. For example, when using nox, you can declare a list of Python versions the session should run on, like @nox.session(python=["3.11.2", "3.11.4", "3.12.1"], name="session-name"). As long as one of the pyenv scopes (shell, local, or global) has all of these versions of Python, nox will run the session multiple times, once for each version declared in python=[].
Note
For more information on the problem pyenv solves, read the "Why use pyenv?" section below.
If you just want to see installation & setup instructions, you can skip to the "install pyenv" section, or to "using pyenv" to see notes on using the tool.
For more detailed (and likely more up-to-date) documentation on pyenv installation & usage, check the pyenv gihub's README.
Why use pyenv?
With pyenv, you can separate your user Python installation from the system Python. This is generally a good practice, specifically on Linux machines; the version of Python included in some distributions is very old, and you are not meant to install packages with pip using the version of Python that came installed on your machine (especially without at least using a .venv).
The system Python is there for system packages you install that have some form of Python scripting included. Packages built for specific Linux distributions, like Debian, Ubuntu, Fedora, OpenSuSE, etc, are able to target the system version of Python, ensuring a stable and predictable installation. When you add pip packages to your system's Python environment, it not only adds complexity for other packages on your system to work around, you also increase your chance of landing yourself in dependency hell, where 2 pip packages require the same package but different versions of that package.
For more reading on why it's a good idea to install a separate version of Python, and leave the version that came installed with your machine untouched, check this RealPython article.
You also have the option of installing Python yourself, either compiling it from source or downloading, building, and installing a package from python.org. This option is perfectly fine, but can be difficult for beginners, and involves more steps and room for error when trying to automate.
pyenv scopes
pyenv has 3 "scopes":
shell- Setting a version with
pyenv shell x.xx.xxsets the Python interpreter for the current shell - When that shell is exited or refreshed (i.e. with
exec $SHELLorsource ~/.bashrc), this value is also reset
- Setting a version with
local- Setting a version with
pyenv local x.xx.xxcreates a file called.python-versionat the current path pyenvuses this file to automatically set the version of Python while in this directory
- Setting a version with
global- Setting a version with
pyenv global x.xx.xxsets the default, global Python version - Any Python command (like
python -m pip install ...) will use thisglobalversion, if nolocalorshellversion is specified localandshelloverride this value- You generally don't need to change this value often. You are providing a version of Python you've installed with
pyenv, in the event you have not set apyenv localorpyenv shellversion of Python.
- Setting a version with
The global scope is essentially the "default" scope. When no other version is specified, the global Python version will be used.
Scope precedence
pyenv shell overrides > pyenv local overrides > pyenv global
Warning
Make sure to pay attention to your current pyenv version. If you are getting unexpected results when running Python scripts, check the version with python3 --version. This will help if you are expecting 3.11.4 to be the version of Python for your session, for example, but have set pyenv shell 3.12.1. Because shell overrides local, the Python version in .python-version will be ignored until the session is exited.
Install pyenv
Installing pyenv varies between OSes. On Windows, you use the pyenv-win package, for example. Below are installation instructions for Windows and Linux.
Install pyenv in Linux/WSL
- Install dependencies
| install pyenv dependencies (Debian/Ubuntu) | |
|---|---|
| install pyenv dependencies (RedHat/Fedora) | |
|---|---|
- Install
pyenvwith the convenience script
- Add
pyenvvariables to your~/.bashrc
| ~/.bashrc | |
|---|---|
Install pyenv in Windows
- Install
pyenv-winwith Powershell
Invoke-WebRequest -UseBasicParsing -Uri "https://raw.githubusercontent.com/pyenv-win/pyenv-win/master/pyenv-win/install-pyenv-win.ps1" -OutFile "./install-pyenv-win.ps1"; &"./install-pyenv-win.ps1"
- Add the following variables to your
PATH
| pyenv Windows PATH variables | |
|---|---|
- You can do this by opening the Control Panel > User Accounts and clicking "Change my environment variables":
- NOTE: Setting these env variables can also be accomplished with this Powershell script:
| set pyenv-win PATH variables | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | |
Using pyenv
The sections below will detail how to install a version (or multiple versions!) of Python using pyenv, switching between them, and configuring your shell to make multiple versions of Python available to tools like nox and pytest.
Note
To see a list of the commands you can run, execute $ pyenv without any commands.
Update pyenv
Updating pyenv is as simple as typing $ pyenv update in your terminal. The pyenv update command will update pyenv itself, as well as the listing of available Python distributions.
List all available Python versions
You can ask pyenv to show you a list of all the versions of Python available for you to install with the tool by running: $ pyenv install -l (or $ pyenv install --list). You will see a long list of version numbers, which you can install with pyenv.
Note
Some releases will indicate a specific CPU architecture, like -win32, -arm64, etc. Make sure you're installing the correct version for your CPU type!
To be safe, you can simply use a Python version string, omitting any CPU specification, like 3.12.1 instead of 3.12.1-arm.
Install Python version(s) with pyenv
Once you have decided on a version of Python to install (we will use 3.12.1, a recent release as of 2/14/2024), install it with: $ pyenv install 3.12.1 (or whatever version you want to install).
You can see which versions of Python are available to pyenv by running $ pyenv versions. The list will grow as you install more versions of Python with $ pyenv install x.x.x.
Set global, local, & shell Python with pyenv
Note
To learn more about the global, local, and shell scopes, check the pyenv scopes section.
Uninstall a version of Python installed with pyenv
To uninstall a version of Python that you installed with pyenv, use $ pyenv uninstall x.x.x. For example, to uninstall version 3.12.1: pyenv uninstall 3.12.1.