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:
- It ensures compatibility between development and production environments.
- Many use Linux-based packaging or installation tools.
- Python tutorials are mostly written for Linux environments.
1) Enable Windows SubSystem for Linux (WSL)
- You need to go to “start”.
- Then make search for “Turn Windows features on or off”.
- Next, click link to open “control panel”.
- After this open “Windows features pop-up menu”.
- Go down and in that list to find “windows subsystem for Linux” option and click on the checkbox”.
- 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:
- You need to open “start menu”.
- Once done, make search for “command prompt” and click right on the result which appears on the top, choose “run as administrator” option.
- 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
- You need to open “start menu”.
- Once done, make search for “turn windows on or off” and choose the result which appears on the top to open page.
- After this check “virtual machine platform” option.
- Hit “ok”.
- 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.
It also has python redistribution files that carry windows builds, this makes it easy to include Python in any other bundle of software.
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.
- You need to download get-pip.py and ensure that you save the file to the desktop.
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
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:
For activation on windows, you need to activate scripts in the scripts folder
The alternative Installation method for VirtualEnvWrapper-win
We are going to suggest you two main methods to install this batch script.
- 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
Python setup.py install
Other Python tutorials you will enjoy reading:
- 7 Python Malware Analysis Libraries for Forensics and Ethical Hacking.
- How to convert any piece of text into speech with Python code.
- How to create a Bluetooth jammer and jam Bluetooth speakers.
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.