How To Create Python Virtual Environment in Windows 11 (virtualenv)

Virtualenv is a Python module that is used to create a unique environment for every task and project. Now, what happens when we use such environments; we have a unique environment to work in, there are no disruptions to other variants of Python used, keep the project neatly organized and much more. In this tutorial, you will learn to configure, create and activate a Python virtual environment in Windows 11.

We will use methods such as Command Prompt, PowerShell and Terminal to install virtualenv and also setup WSL2 for the needs of this tutorial.

Create and Configure a Python Virtual Environment in Windows 11

To take full advantage, enable Windows Subsystem for Linux (WSL). This shall help you run full Linux distribution inside windows to help in functions of the new environment.

Why WSL is your best friend:

It is for the following 3 main reasons:

  1. It ensures compatibility between development and production environments.
  2. Many use Linux-based packaging or installation tools.
  3. Python tutorials are mostly written for Linux environments.

ALSO SEE: How To Create, Delete, Activate and Deactivate Venv in Windows and Mac.

1) Enable Windows SubSystem for Linux (WSL) 

  1. You need to go to “start”.
  2. Then make search for “Turn Windows features on or off”.
  3. Next, click link to open “control panel”.
  4. After this open “Windows features pop-up menu”.
  5. Go down and in that list to find “windows subsystem for Linux” option and click on the checkbox”.
  6. Once done, simply reboot.

2) Install WSL 2 Manually via CMD

For this you need to follow the steps below:

  • On your new version of Microsoft open “start”.
  • Then search for “command prompt”, click right on the result which appears on the top and choose “run as administrator” option.
  • To install this type the following command and hit “enter”.
C:\Users\Paul> wsl --install
  • Once done, restart your system and finish the installation.
  • Then continue with the Linux distro setup as required. I would recommend Ubuntu.

Once you have installed it make sure it is up-to-date:

C:\Users\Paul> Wsl --update

As you complete automatically required Linux components shall be installed on windows 11 and Ubuntu’s Linux distribution’s latest version.

If you are into mixing some flavors of Linux then the WSL2 command has an option to both view and choose a different distro:

  1. You need to open “start menu”.
  2. Once done, make search for “command prompt” and click right on the result which appears on the top, choose “run as administrator” option.
  3. If you want to view a list of available WSL distros which can be installed on version 11 then just type the following commands and hit “enter”.
C:\Users\Paul> Wsl --list –online

4. To install WSL with specific distro on new version type the following command and hit “enter”

C:\Users\Paul> Wsl --install –d Ubuntu

5. Make sure that you replace DISTRO-NAME with the name you wish to install like Debian.

6. To finish the process of installation, simply restart your system.

Once done all the components shall be installed alongside distro of Linux which has been specified.

Keep in mind only if update is available only then it shall download and install on your system.

Alternatively, Install WSL2 from Microsoft Store!

The best thing about Microsoft is that we can download all the components from this wrapped into an application. Now, what’s the advantage? it shall run isolated from version 11 and all updates shall be available via the Microsoft store without the need to wait for the Operating System’s next version to install the newest version available.

3) Enable Virtual Machine from Platform

  1. You need to open “start menu”.
  2. Once done, make search for “turn windows on or off” and choose the result which appears on the top to open page.
  3. After this check “virtual machine platform” option.
  4. Hit “ok”.
  5. Restart your system.

Once all these steps have been completed, you can download the application from the store.

4) Install Pip and Python for Virtual Environment

Fact: Pip is pre-installed in version 3 of Python.

Now Windows has a web-based installer that is able to download required software during the process of installation.

  1. It also has python redistribution files that carry windows builds, this makes it easy to include Python in any other bundle of software.

  2. If you install Ubuntu version 20.04 then version 3 of Python comes pre-installed, if you don’t have it then please install it. It is a core requirement.

  3. You need to download get-pip.py and ensure that you save the file to the desktop.
  4. At cmd, go to desktop and run the get-pip.py script, it shall be able to would system-wide after this.

Paul@ninja-ide:~#  Cd \paul\desktop\desktop
Paul@ninja-ide:~#  ./Python get-pip.py

5) Install virtualenv 

Type the following command in the windows commands shell prompt:

Paul@ninja-ide:~# apt-get install virtualenv

ALSO SEE: Python Venv Commands Cheat Sheet (10 Helpful Command Examples).

How To Activate and Start virtualenv on Windows 11

Type following command in windows command prompt and go to location of your project:

C:\Users\Paul>  Cd ninja_project

Once within the project run the following command:

C:\Users\Paul> Virtualenv env

Virtualenv on windows creates a batch file called:

\env\Scripts\activate.bat

For activation on windows, you need to activate scripts in the scripts folder

C:\Users\Paul>C:\Users\Paul\venv\Scripts\activate.bat

The alternative Installation method for VirtualEnvWrapper-win

We are going to suggest you two main methods to install this batch script.

  1. With pip
Paul@ninja-ide:~# pip install virtualenvwrapper-win
  • From the git clone
Paul@ninja-ide:~# Git clone git://github.com/davidmarble/virtualenvwrapper-win.git

Then run:

Python setup.py install

Other Python tutorials you will enjoy reading:

Conclusion

Python has various uses and it certainly is still very active on Windows! The virtual environments allow you to work and isolate apps in different directories so they don’t affect others. It is important to know how to create a Python virtual environment using virtualenv in Windows 11 as not having one configured can cause data loss and damage to other work stored on the machine.

Previous articleBazaar integration by beuno
Next articleHow To Build Pacman Game in C – Simple Pacman Game Project for Beginners
Paul Carruthers
Paul is an avid programmer who specializes in Python and Java with over 16 years of experience in the field. He loves automating complex tasks and creating useful scripts to streamline work and make life easier. He is also a massive fan of Linux and currently uses it as his main desktop OS. When he is not staring at code, he loves hiking and swimming in different parts of the world.

LEAVE A REPLY

Please enter your comment!
Please enter your name here