lricardo.space

Manage Python versions with `pyenv`

Managing multiple Python versions can be challenging, even more if we are using MacOS or Windows. When we install Python from the official website, most probably we will end up with multiple versions installed, not knowing which version will be chosen by the system as default. Another problem that we can potential face is having to develop code on legacy systems, usually, using older Python versions. Therefore, what if we need to develop some code in an older version?

That’s where pyven (or pyenv-win if you are using Windows) comes into action. This tool allows installation and management of different Python versions.

Below I show some of the most important commands, on Windows (but the commands are the same for UNIX-like systems):

# List all versions available for install
PS C:\Users\Leandro Ricardo> pyenv install -l
:: [Info] ::  Mirror: https://www.python.org/ftp/python
2.4-win32
2.4.1-win32
2.4.2-win32
2.4.3c1-win32
...
3.11.3
3.11.4-arm
3.11.4-win32
3.11.4

# Install some specific version
PS C:\Users\Leandro Ricardo> pyenv install 3.11.2
:: [Info] ::  Mirror: https://www.python.org/ftp/python
:: [Downloading] ::  3.11.2 ...
:: [Downloading] ::  From https://www.python.org/ftp/python/3.11.2/python-3.11.2-amd64.exe
:: [Downloading] ::  To   C:\Users\Leandro Ricardo\.pyenv\pyenv-win\install_cache\python-3.11.2-amd64.exe
:: [Installing] ::  3.11.2 ...
:: [Info] :: completed! 3.11.2

# Check installed versions
PS C:\Users\Leandro Ricardo> pyenv versions
  3.11.2
  3.11.4 (set by C:\Users\Leandro Ricardo\.pyenv\pyenv-win\version)

# Set default version (system-wide)
PS C:\Users\Leandro Ricardo> pyenv global 3.11.4
  3.11.2
* 3.11.4 (set by C:\Users\Leandro Ricardo\.pyenv\pyenv-win\version)

As you can see, very simple and yet, powerful!