AllAboutOps

How to Install Python Using Pyenv on Windows

Author Avatar By Adarsh Mallandur on | 6 minute read | Category: Programming
How to Install Python Using Pyenv on Windows

Do you want to start coding Python on a Windows machine? I would not suggest installing python from the official website and installing packages there! Instead, the better way to install Python is through Pyenv. In this post, I will tell you why you should not install Python from the official website if you want to switch Python versions (which you would like to!) and if you plan to use a virtual environment. I will talk about how to install Pyenv on a Windows 10/11 machine to change and manage multiple python versions within the same project.

By the end of this post, you will set up python on windows by

  1. Understanding why we need multiple python versions each with its own virtual environment.
  2. Installing Pyenv in order to install and manage multiple python versions

Let’s find out the best way to set up python on windows ….

Do you already have python installed?

Before installing python, you need to make sure Python is already installed on your machine. You can do this by opening the Windows Command Prompt and typing the below command.

python --version

If you do not have it installed then you will see an error or you will end up in the Microsoft Store App. The Microsoft Store version of Python is a little restrictive and it does not allow you to do certain things. We shall not do it the Microsoft way. If you already have it installed, don’t worry. You can go ahead and uninstall because we will now do it the RIGHT WAY!

Different ways to install python

There are different ways to setup Python on windows.

  1. Visit the official website python.org and download the installer.

Here, you get the safest and most compatible distribution of Python called Cython. However, it becomes difficult to manage multiple versions of Python in this way. You need to tweak Path variables and at some point of time you will have a breakdown!

  1. Install Python from the Microsoft Store

However, it’s worth noting that the version of Python available in the Microsoft Store may not always be the latest version, and you may need to update it manually in the future.

  1. Use a package manager for Windows such as Chocolatey or NuGet

However, it does require some familiarity with the command line and may not be as straightforward as the other methods.

  1. Use a third-party distribution such as Anaconda or Miniconda

However, the installation file can be quite large and the installation process may take longer than the other methods. Also, conda and Anaconda cannot be used for commercial purposes anymore without a valid license!

  1. Using the Windows Subsytem for Linux (WSL)

You could do this, if you want to use Unix-like environment in Windows.

In order to choose one of the above options you need to consider two main things to note when developing python code on your machine

  1. How to create an isolated python environment?
  2. How to switch python version and create another virtual environment?

You could download python from the official website and use virtualenv/venv but then it becomes difficult to switch to different version of python to create another environment. This is where Pyenv comes to rescue!

What is PyEnv?

Pyenv is an open-source tool that enables you to manage multiple versions of Python on a single machine. It allows you to easily switch between different versions of Python and set the version of Python that your projects or applications will use. Together with virtual environments, Pyenv is one of the most important tools for everyone who uses Python regularly.

You could install multiple Python versions by downloading the corresponding executables from the official website. However, you will end up with multiple Python executables in your PATH, such as python, python2, python3.8, etc. This becomes quickly impossible to manage. Pyenv tool helps to install and run specific Python versions. However, Pyenv does not officially support Windows and is meant to be used on Unix-based operating systems like Linux and macOS. Nevertheless, Pyenv can be installed on Windows using a fork of Pyenv called pyenv-win.

How to install Pyenv on windows?

There are multiple ways to install Pyenv on a Windows machine. The easiest way to install Pyenv on windows is by using Windows PowerShell.

  1. Press Windows+R to open the Run dialog box, and then type “powershell” in the text box.
  2. Run the following command in 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"
  1. If the installation succeeds, then you will see the below message
pyenv-win is successfully installed. You may need to close and reopen your terminal before using it.
  1. If not, then you may need to start the Powershell with “Run as administrator” option and run below command in the administrator powershell
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
  1. Now rerun the installation command.

Validation

In order to check if the installation is successful, you can open a new command prompt by pressing Windows+R and typing cmd and then running

pyenv --version

You should see the text pyenv followed by the installed version.

Using Pyenv to Install Python

Now that you have pyenv installed, installing Python through pyenv is the next step. First, see if the version of Python you want is available (if the version you need is not available then you may need to run pyenv update to update the tool and make sure you have access to the latest versions)

pyenv install --list

This will show you a long output like this

... 
3.10.1 
3.10.2 
3.10.3 
3.10.4 
3.10.5 
3.10.6 
3.10.7 
3.11.0rc2 
3.11-dev 
3.12-dev 
...

So now you know, you can install from these versions. To install a Python version:

pyenv install 3.10.7 
:: [Info] :: Mirror: https://www.python.org/ftp/python 
:: [Downloading] :: 3.10.7 ... 
:: [Downloading] :: From https://www.python.org/ftp/python/3.10.7/python-3.10.7-amd64.exe 
:: [Downloading] :: To C:\Users\adarsh\.pyenv\pyenv-win\install_cache\python-3.10.7-amd64.exe 
:: [Installing] :: 3.10.7 ... 
:: [Info] :: completed! 3.10.7

This will download Python from the official website python.org and install it in .pyenv directory.

Pyenv Usage

You can see the currently-selected Python version with this command:

pyenv version 
No global/local python version has been set yet. Please set the global/local version by typing: 
pyenv global <python-version> 
pyenv global 3.7.4 
pyenv local <python-version> 
pyenv local 3.7.4

Since we have not run pyenv global, the python version has not been set yet. Let’s run pyenv global to set python to the downloaded version

pyenv global 3.10.7
pyenv version 
3.10.7 (set by C:\Users\admin\.pyenv\pyenv-win\version)

You can see all versions downloaded including the current selected one with

pyenv versions 
* 3.10.7 (set by C:\Users\adarsh\.pyenv\pyenv-win\version)

Note the * next to the selected python version.

You can have a globally selected Python version, and then locally selected Python versions. The global version is used if no local version is selected.

You can select a local version with:

pyenv local 3.10.7

This creates a .python-version file in your current folder, which tells pyenv to use that Python version.

For more information on how to use Pyenv refer https://realpython.com/intro-to-pyenv/

The great thing about pyenv is that it just works, and we only use it at the very beginning of a project, to create a virtual environment. You need it again only if you want to switch your python version.

Conclusion

In this post, we saw what Pyenv is and how to install Pyenv on a windows machine. Together with Pyenv and a virtual environment, we can have a starting point to develop python code on our machine. In the next post, we will see how to use Pyenv together with virtualenv to create a python virtual environment. Feel free to comment below if you need any help with the installation.