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 (WSL). Even in ESL, the Pythons it installs are not native Windows versions but rather Linux versions running in a virtual machine – so you don’t get any Windows-specific functionality.
Fortunately, @kirankotari’s pyenv-win fork makes pyenv available for Windows.
Refer to the installation section for guidance on installing pyenv-win. I recommended using Chocolatey. If Chocolatey is not already installed on your system, follow the instructions here to install it.
Verifying installation
Once you have installed pyenv-win, you can verify the installation by running the following PowerShell command:
pyenv --version
If the installation is successful, you will see the version of pyenv-win installed on your system. For example:
pyenv 3.1.1
You can further confirm that pyenv-win is working correctly by running the pyenv command. This will display a list of all available commands:
pyenv
For example, you might 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 command displays all the available Python versions. For example, you might 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.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, such as 3.9.10, you can use the following command:
pyenv install 3.9.10
This will initiate the download and installation process, producing output similar to:
:: [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 a Global Python Version
Step 1: Understanding the Global Python Version
In pyenv, the global Python version is the default version used system-wide unless overridden by a local version.
Key Points:
- The global version serves as a fallback if no other version is specified.
- A project-specific version (set with
pyenv local
) will take precedence only within the project’s directory.
This hierarchy allows flexibility while maintaining a fallback/default when no other version is specified.
Step 2: Viewing or Setting the Global Python Version
You can use the pyenv global
command to set or view the global Python version for your system.
Example: Setting a Global Python Version
To set Python 3.9.10
as the default version, run:
pyenv global 3.9.10
Once set, Python 3.9.10
will be the default version used unless a local version is defined.
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. ↩︎