How To Set Up a Python Virtual Environment on Windows 10

Reading Time: 5 minutes

A virtual environment, or venv, is a Python module that creates a unique environment for each task or project. It installs the necessary packages specific to that setting while neatly organizing your projects.

Venv never modifies the system’s default Python versions or system modules. Using it allows a unique working environment to avoid disruptions to other Python variants existing but unrelated to your project.

Prerequisites

We recommend enabling the Windows Subsystem for Linux (WSL) to take full advantage of all the functionality of venv on Windows 10. This feature allows you to run a complete Linux distribution within Windows to aid in the functionality of the new dev environment.

How To Set Up A Python Virtual Environment On Windows 10

What is a Python Virtual Environment?

A Python virtual environment is a Python utility for managing dependencies and isolating projects. They enable Python third-party libraries (site packages) to be deployed locally in an isolated directory for a specific project rather than globally (system-wide).

A Python virtual environment is a directory containing three key elements:

  • Symlinks to existing Python executables.
  • Site-package directory for third-party libraries.
  • Scripts that ensure Python code executes using the Python interpreter and libraries installed within the specified virtual environment.

Why Should I Use WSL?

Many of the tutorials for Python are written for Linux environments. In addition, a lot of developers use Linux-based packaging/installation tools. Using WSL ensures compatibility between development and production environments.

What Are the Benefits of a Virtual Environment on Windows 10/11?

While there’s certainly no lack of benefits regarding Python virtual environments, here are the three primary ones.

Organization

You can better arrange your packages and know which packages you require to run your code if someone else wants to run it on their machine.

Flexibility

You can use whichever version of Python you need for each environment without fear of conflicts.

Better Performance

Your primary Python package directory is clear of unused Python packages, increasing performance.

How to Create a Python Virtual Environment on Windows 10/11

There are a few steps to create a Python virtual environment on Windows. We will outline all of them below.

How Do I Enable WSL?

From the Start menu or search bar, search for Turn Windows features on or off.

Click on Windows Control Panel

Next, open the Windows features pop-up menu. 

Scroll down in that list to locate the Windows Subsystem for Linux option and select the checkbox to enable it.

Reboot your device.

Note:
WSL will require you to download a version of Linux noted here. If you’re using Windows (without WSL), simply install Python 3 from the Python website. The venv module is incorporated into that Windows installation.

Install Linux

There are multiple Linux distros that work with WSL. You can locate and install them from the Microsoft Store. We recommend starting off with a Ubuntu 20.04 distribution as it's up to date, has an excellent support community, and is well documented.

From the Microsoft app store, locate Ubuntu 20.04 and click Get.

From PowerShell, run the following command.

wsl --install -d ubuntu

Now type Ubuntu into your Start menu and initiate it.

Next, you'll be asked to create a username and password, as it will be your first time using this OS.

Subsequently, you will now be signed in automatically as the default user.

Lastly, we'll need to run an update on the new OS. Windows does not handle upgrades for this OS, so you will need to ensure Ubuntu stays up to date by running the update and upgrade commands manually. You can accomplish this by running the following command.

sudo apt update && sudo apt upgrade

If the Microsoft store app is unavailable to you for some reason, you can manually download and install a Linux distribution.

To install the downloaded distribution, navigate to the directory which contains the newly downloaded Linux distributions. Once in that directory, run the following command in PowerShell (where app_name.aspx is the name of the distribution file).

Add-AppxPackage .\app_name.appx

Next, we'll add the path to the distro into your Windows environment PATH using Powershell (e.g. C:\Users\Admin\Ubuntu).

$userenv = [System.Environment]::GetEnvironmentVariable("Path", "User")

[System.Environment]::SetEnvironmentVariable("PATH", $userenv + ";C:\Users\Admin\Ubuntu", "User")

Now, we can start the distro by typing in ubuntu.exe. Next, we should initialize the new instance.

Launch a Distro

To finish the initialization of your newly installed distro, we will need to launch a new instance. You can accomplish this by clicking on the Launch button in the Microsoft app store, or by launching the distro’s .exe file from the Start menu.

Additionally, if using a Windows Server, you can start the distro's launcher’s executable file (Ubuntu.exe) from the distro’s installation directory.

During the last stage of the installation, the distro's files will be decompressed and stored locally on your PC. This process may take a few minutes but is only required once. Later initializations should take less than a second.

Create Your Python Virtual Environment

There are four basic steps to create a virtual environment on windows:

  1. Install Python
  2. Install Pip
  3. Install VirtualEnv
  4. Install VirtualEnvWrapper-win
Note:
Remember, these commands should be run within the WSL Ubuntu environment. Additionally, the sudo command may be needed if not running as the root user.

Step 1: Install Python 

There is a Python installer for Windows. This installer will download the required software during the installation.

There are also Python redistributable files that contain the Windows builds, which makes it easier to include Python in another software bundle. 

If you installed Ubuntu, Python3 comes pre-installed. Use the following command to verify this.

which python3

The output shows the directory path where Python3 is installed.

Step 2: Install PIP

Python3 usually comes with pip preinstalled. However, some get the following error.

pip command not found

Should this occur, simply use the following method to install pip on Windows.

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

Download get-pip.py, and save the file. For this tutorial, the file is saved to the Desktop. 

Start a command prompt as an administrator, navigate to your Desktop, and run the get-pip.py script. After that, pip should work system-wide.

cd Desktop
python3 get-pip.py

Here is the same command using standard Python instead of Python 3.

cd Desktop
Python get-pip.py

Step 3: Install Virtualenv

Type the following command into your Windows command shell prompt to install Virtualenv.

pip install virtualenv

Start Virtualenv

To start Virtualenv, head to your project location on your windows command prompt. For this tutorial, the project name and location are my_project.

cd my_project

Once inside the project directory, run this command.

virtualenv env

Activate Virtualenv

On Windows, venv creates a batch file called activate.bat located in the following directory.

\venv\Scripts\activate.bat

To activate the Python virtual environment on Windows, run the script from the directory. Username will be the user’s name logged into the environment.

C:\Users\'Username'\venv\Scripts\activate.bat

Step 4: Install virtualenvwrapper-win

There are two main recommended methods to install the virtualenvwrapper-win script.

Install virtualenvwrapper-win With pip

You can install it using pip.

pip install virtualenvwrapper-win

Install virtualenvwrapper-win From Source

Alternatively, you can install it from the source.

git clone git://github.com/davidmarble/virtualenvwrapper-win.git

Finally, change directories to the virtualenvwrapper-win directory and run the following.

python setup.py install

Your Python venv is set up and ready to use.

Wrapping Up

Creating a Python virtual environment in Windows 10 gives developers another tool for isolating projects and getting things done. Using this simple method saves the hassle and cost of using multiple servers to separate projects.

Check out all that Liquid Web has to offer for your next Python project. They offer dedicated servers, cloud dedicated servers, bare metal cloud servers, and VPS hosting. Contact the sales team today to get started.

Avatar for Joseph Molloy

About the Author: Joseph Molloy

Liquid Web Security Operations tech Joseph spends his days cleaning up malware and doing his part to keep the Internet safe. He is interested in all things Linux and always looking for something new to learn. In addition, he enjoys cooking and playing guitar, is an avid horror and sci-fi reader, and is a fan of black metal music.

Latest Articles

Blocking IP or whitelisting IP addresses with UFW

Read Article

CentOS Linux 7 end of life migrations

Read Article

Use ChatGPT to diagnose and resolve server issues

Read Article

What is SDDC VMware?

Read Article

Best authentication practices for email senders

Read Article