DEV Community

Hafidh Fikri
Hafidh Fikri

Posted on

Create Python Virtual Environment with Various Version of Python on Windows

As someone who's not too familiar with using Python, I am confused about creating multiple virtual environments with different Python versions.

To solve this issue, I found a helpful library that can help me create multiple virtual environments with different Python versions. I use pyenv and pyenv-venv to tackle this problem, and many tutorials explain the installation process in Linux and MacOS, so I will share how to install it on Windows.

Step 1. Install pyenv and pyenv-venv

Firstly, I need to install both the library by following the instructions on the git page pyenv-win and pyenv-win-venv. For the first step, I need to install pyenv first by executing these commands:

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
Enter fullscreen mode Exit fullscreen mode

To check that the installation was successful, I will check the version of pyenv.

pyenv --version
#Output: pyenv 3.1.1
Enter fullscreen mode Exit fullscreen mode

After that, I need to install pyenv-env.

Invoke-WebRequest -UseBasicParsing -Uri "https://raw.githubusercontent.com/pyenv-win/pyenv-win-venv/main/bin/install-pyenv-win-venv.ps1" -OutFile "$HOME\install-pyenv-win-venv.ps1"; &"$HOME\install-pyenv-win-venv.ps1"
Enter fullscreen mode Exit fullscreen mode

And check again if the installation was successful.

pyenv-win-venv
#Output: pyenv-win-venv v0.6.5
Enter fullscreen mode Exit fullscreen mode

Step 2. Install Python

I will plan to install two different versions of Python, which are version 3.8.8 and 3.9.4. Before I do the installation, I need to check if the Python version that I will install are available to download with this command:

pyenv install -l
#Output:
#3.0.1
#3.1.0
#3.1.1
#3.8.8
#3.9.4
#.....
Enter fullscreen mode Exit fullscreen mode

So, if I have found the version of Python that I want to install, the next step is to install it with the following command:

#Command structure
#pyenv install <python_version>

pyenv install 3.8.8
pyenv install 3.9.4
Enter fullscreen mode Exit fullscreen mode

Now we need to make sure both Python versions have been installed and can be used to create the virtual environment by checking availability both on pyenv and pyenv-venv

pyenv versions
#Output:
#3.8.8
#3.9.4

pyenv-venv list python
#Output:
#3.8.8
#3.9.4
Enter fullscreen mode Exit fullscreen mode

Well, I can see both Python versions have been installed, so now I can create the virtual environment.

Step 3. Create Virtual Environment

To create a new virtual environment by defining a specific Python version, I will execute the command below:

#Command structure
#pyenv-venv install <python_version> <env_name>
#pyenv-venv activate <env_name>

pyenv-venv install 3.8.8 env_a
pyenv-venv activate env_a
python -V 
#Output : Python 3.8.8
pyenv-venv deactivate

pyenv-venv install 3.9.4 env_b
pyenv-venv activate env_b
python -V 
#Output : Python 3.9.4
pyenv-venv deactivate
Enter fullscreen mode Exit fullscreen mode

Finally, if you want to find out what virtual environment has been created, just follow this command you can see the available virtual environment on the computer

pyenv-venv list envs

#Output:
#Envs installed:
#env_a
#env_b
Enter fullscreen mode Exit fullscreen mode

This is one of the solutions I use. I have tried using the virtualenv plugin from pyenv, and it turned out to be constrained in the installation, so I looked for other alternatives. I hope this article can be an alternative solution for installing various versions of Python in various virtual environments on Windows.

Top comments (0)