Share this content:
In this guide we are going to show how to quickly switch python version using pyenv with some easy steps.
In this article:
Why different python versions
It can happen that in big projects or in projects where there is not a single repository of the code (i.e. microservices) there must be some dependency between the packages used and the python version. For some reason some repository belongs to legacy project that cannot be updated because X or Y, where X or Y could easily be one between:
- can cost too much effort
- will not imply big revenue
- it is a calculated risk
- will not bring any effect
- it can be too expensive to refactor based on the related libraries
- ….and so on…
This of course, bring into play the fact that a developer cannot switch laptop or remove/upgrade
/downgrade python version on the fly during his working day based on the task he is working on. This problem is solved by pyenv.
What is pyenv
From https://github.com/pyenv/pyenv
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.
And still from the same page
What pyenv does…
- 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.
In contrast with pythonbrew and pythonz, pyenv does not…
- 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.
Install pyenv
Installing pyenv is super easy! Based on the OS you have you can
Install pyenv Mac OS
Using homebrew
brew update
brew install pyenv
Install pyenv Windows
Windows is not officially supported but there is an independent port called pyenv-win https://github.com/pyenv-win/pyenv-win
From the powershell terminal
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"
Install pyenv Linux
Linux requires few more steps
Clone the repository
# clone the repository
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
Configure the environment
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
Then restart your shell
Use pyenv
If everything went in the right way you can now run from the command line of your OS
$> pyenv
pyenv 2.3.2
Usage: pyenv <command> [<args>]
Some useful pyenv commands are:
--version Display the version of pyenv
commands List all available pyenv commands
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 given pyenv command
init Configure the shell environment for pyenv
install Install a Python version using python-build
local Set or show the local application-specific Python version(s)
prefix Display prefixes for Python versions
rehash Rehash pyenv shims (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 existing pyenv shims
uninstall Uninstall a specific Python version
version Show the current Python version(s) and its origin
version-file Detect the file that sets the current pyenv version
version-name Show the current Python version
version-origin Explain how the current Python version is set
versions List all Python versions available to pyenv
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: https://github.com/pyenv/pyenv#readme
List all the python versions available
For listing all the available python versions we can use pyenv install -l command
$> pyenv install -l
Available versions:
2.1.3
2.2.3
2.3.7
2.4.0
2.4.1
2.4.2
2.4.3
2.4.4
2.4.5
...
stackless-3.4-dev
stackless-3.4.2
stackless-3.4.7
stackless-3.5.4
stackless-3.7.5
Install a specific python version
Let’s say we want to install python 3.10.5 all we have do to is to run the install command as follow
$> pyenv install 3.10.5
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.10.5.tar.xz...
-> https://www.python.org/ftp/python/3.10.5/Python-3.10.5.tar.xz
Installing Python-3.10.5...
python-build: use tcl-tk from homebrew
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.10.5 to /Users/d3/.pyenv/versions/3.10.5
now we can list the versions available on our system by doing
$> pyenv versions
system
3.10.5
* 3.8.10 (set by /Users/d3/.pyenv/version)
3.8.11
3.8.12
Activate a python version
Once we are able to list all the versions available and install a specific one, now we can activate the one we want by doing
$> pyenv global 3.10.5
$ python -V
Python 3.10.5
Uninstall a particular python version
If we want to remove a particular python version we can just run
$> pyenv uninstall 3.10.5
More commands
You can explore more commands by listing all of them and then add –help as additional argument
As example we can list the commands available
$> pyenv commands
--version
commands
completions
exec
global
help
hooks
init
install
local
prefix
realpath.dylib
rehash
root
shell
shims
uninstall
version
version-file
version-file-read
version-file-write
version-name
version-origin
versions
whence
which
Then we can for example see what shell is doing
$> pyenv shims --help
Usage: pyenv shims [--short]
List existing pyenv shims
[][/Users/afabiano] $> pyenv shell --help
Usage: pyenv shell <version>...
pyenv shell -
pyenv shell --unset
Sets a shell-specific Python version by setting the `PYENV_VERSION'
environment variable in your shell. This version overrides local
application-specific versions and the global version.
<version> should be a string matching a Python version known to pyenv.
The special version string `system' will use your default system Python.
Run `pyenv versions' for a list of available Python versions.
When `-` is passed instead of the version string, the previously set
version will be restored. With `--unset`, the `PYENV_VERSION`
environment variable gets unset, restoring the environment to the
state before the first `pyenv shell` call.
I hope that this small introduction was useful! Please share and help us grow!