Introduction
Managing multiple Python installations on Windows can be a challenging task. With the help of pyenv, it becomes much easier and efficient. In this article, we will explore how to set up and use pyenv to effectively manage multiple Python versions on your Windows system.
Why have multiple Python installations?
Here is a non-exhaustive list of reasons why you might want to have multiple Python installations on your system:
Open-source contributions: Contributing to open-source projects often requires running specific Python versions that the project supports. By having multiple Python installations, you can easily switch to the required version for contribution purposes.
Legacy projects: Legacy projects may rely on older versions of Python that are no longer actively supported. If you need to maintain or work on such projects, having multiple Python installations enables you to keep the necessary older versions while using newer versions for other projects.
Testing and development: As a developer, you may need to test your code on different Python versions to ensure its compatibility across various environments. Having multiple Python installations allows you to test your code on different versions without affecting your main development environment. If you are seriously considering testing your code on different Python versions, you should consider using tox, which aims to automate testing in various Python versions.
Overall, having multiple Python installations provides flexibility and the ability to work on a variety of projects with different requirements. Pyenv enables developers (like you) to efficiently manage various Python versions in a easy to use cli.
What is pyenv?
Pyenv is a tool that allows you to manage multiple Python versions concurrently and isolate them from the system Python 1. It provides a straightforward and flexible way to switch between different Python versions.
Installing pyenv
A quick search on ‘pyenv’ will lead you to https://github.com/pyenv/pyenv. However, upon going through the README, you will find that pyenv is not supported on Windows. Here are some relevant excerpts from the README:
Pyenv does not officially support Windows and does not work in Windows outside the Windows Subsystem for Linux. Moreover, even there, the Pythons it installs are not native Windows versions but rather Linux versions running in a virtual machine – so you won’t get Windows-specific functionality.
If you’re in Windows, we recommend using @kirankotari’s pyenv-win fork – which does install native Windows Python versions.
So, if you are on Windows, you should navigate to pyenv-win. From the installation section, the best way (in my opinion) to install pyenv-win is using Chocolatey. If you don’t have Chocolatey installed, you can follow the instructions here to install it.
If you don’t prefer the Chocolatey way, you can try the Powershell way.
Verifying the installation
Once you have installed pyenv-win, you can verify the installation by reopening Powershell and running the following command:
pyenv --version
If the installation was successful, you should see the version of pyenv-win installed on your system. For example, I see the following output:
pyenv 3.1.1
Further, you can verify that pyenv-win is working correctly by running the pyenv command, which will list all the available commands.
pyenv
For example, I see the following output:
pyenv 3.1.1
Usage: pyenv <command> [<args>]
Some useful pyenv commands are:
commands List all available pyenv commands
duplicate Creates a duplicate python environment
local Set or show the local application-specific Python version
global Set or show the global Python version
shell Set or show the shell-specific Python version
install Install a Python version using python-build
uninstall Uninstall a specific Python version
update Update the cached version DB
rehash Rehash pyenv shims (run this after installing executables)
vname Show the current Python version
version Show the current Python version and its origin
version-name Show the current Python version
versions List all Python versions available to pyenv
exec Runs an executable by first preparing PATH so that the selected Python
which Display the full path to an executable
whence List all Python versions that contain the given executable
See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv-win/pyenv-win#readme
Installing Python
To see the list of Python versions available for installation, you can run the following command:
pyenv install -l
This will list all the available Python versions. For example, I see the following output (truncated for brevity):
:: [Info] :: Mirror: https://www.python.org/ftp/python
3.9.0a1-win32
3.9.0a1
3.9.0a2-win32
3.9.0a2
3.9.0a3-win32
3.9.0a3
3.9.0a4-win32
3.9.0a4
3.9.0a5-win32
3.9.0a5
3.9.0a6-win32
3.9.0a6
3.9.0b1-win32
3.9.0b1
3.9.0b2-win32
3.9.0b2
3.9.0b3-win32
3.9.0b3
3.9.0b4-win32
3.9.0b4
3.9.0b5-win32
3.9.0b5
3.9.0rc1-win32
3.9.0rc1
3.9.0rc2-win32
3.9.0rc2
3.9.0-win32
3.9.0
3.9.1rc1-win32
3.9.1rc1
3.9.1-win32
3.9.1
3.9.2rc1-win32
3.9.2rc1
3.9.2-win32
3.9.2
3.9.3-win32
3.9.3
3.9.4-win32
3.9.4
3.9.5-win32
3.9.5
3.9.6-win32
3.9.6
3.9.7-win32
3.9.7
3.9.8-win32
3.9.8
3.9.9-win32
3.9.9
3.9.10-win32
3.9.10
3.9.11-win32
3.9.11
3.9.12-win32
3.9.12
3.9.13-win32
3.9.13
To install a specific Python version (say 3.9.10), you can run the following command:
pyenv install 3.9.10
This will result in the following output:
:: [Info] :: Mirror: https://www.python.org/ftp/python
:: [Downloading] :: 3.9.10 ...
:: [Downloading] :: From https://www.python.org/ftp/python/3.9.10/python-3.9.10-amd64.exe
:: [Downloading] :: To C:\Users\<username>\.pyenv\pyenv-win\install_cache\python-3.9.10-amd64.exe
:: [Installing] :: 3.9.10 ...
:: [Info] :: completed! 3.9.10
Setting the global Python version
By setting the global Python version, you can set the Python version that will be used by default in all shells. To do this, you can run the following command:
pyenv global 3.9.10
This is the version of python that will be used by default if a local version (see below) isn’t set.
System Python is the Python version that comes pre-installed with your operating system. It is not recommended to modify or remove the system Python as it may break your system. ↩︎