How To Set Up a Python Virtual Environment on Windows 10 (2024)

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 (1)

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 Desktoppython3 get-pip.py

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

cd DesktopPython 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.

Contact us

How To Set Up a Python Virtual Environment on Windows 10 (2024)

FAQs

How To Set Up a Python Virtual Environment on Windows 10? ›

This tells us that there is a venv folder directly in your home directory - in other words, a separate venv folder (meaning, a separate virtual environment) from the one that is in ~/Desktop/Directory/venv . Using the command source venv/bin/activate while in that directory, will activate that virtual environment.

How to create a virtual environment in Python in Windows 10? ›

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.
Jan 17, 2023

How to activate a virtual environment in Python? ›

This tells us that there is a venv folder directly in your home directory - in other words, a separate venv folder (meaning, a separate virtual environment) from the one that is in ~/Desktop/Directory/venv . Using the command source venv/bin/activate while in that directory, will activate that virtual environment.

How do I run a Python virtual environment? ›

To use the virtual environment you created to run Python scripts, simply invoke Python from the command line in the context where you activated it. For instance, to run a script, just run python myscript.py .

How to setup Python in Windows environment? ›

Python Installation on Windows
  1. Step 1: Select Python Version. ...
  2. Step 2: Download Python Executable Installer. ...
  3. Step 3: Run Executable Installer. ...
  4. Step 4: Add Python to Path (Optional) ...
  5. Step 5: Verify Python Was Installed on Windows. ...
  6. Step 6: Verify PIP Was Installed. ...
  7. Step 7: Install virtualenv (Optional)
Dec 5, 2023

How do I activate venv in Windows? ›

Open Any Terminal and run below command.
  1. python -m venv venv. Activate the Virtual Environment. ...
  2. source venv/bin/activate. For Windows With CMD.
  3. .\venv\Scripts\activate.bat. For Windows With Power shell.
  4. .\ venv\Scripts\activate.ps1. ...
  5. source venv/Scripts/activate.
Jan 19, 2022

Are virtualenv and venv the same? ›

venv is a built-in module in Python 3.3 and later versions that allows you to create isolated Python environments. It is similar to virtualenv , but it is installed by default with Python. pyvenv is a script that comes with Python 3.3 and later versions that allows you to create virtual environments.

How to check if Python virtual environment is activated? ›

Several ways to know if the Python interpreter is running inside a virtual environment:
  1. Solution 1: use sys. prefix that points to the Python directory.
  2. Solution 2 (the better way): VIRTUAL_ENV environment variable. When a virtual environment is activated, this is set to the venv's directory, otherwise it's None.
Jul 26, 2023

Do you need a virtual environment for Python? ›

Always use a Virtual Environment

You can have as many venvs as you want. For an additional layer of control over when you update to new versions of Python, you can compile your own Python interpreter and create a virtual environment based on it.

How do I find my virtual environment in Python? ›

To see a list of the Python virtual environments that you have created, you can use the 'conda env list' command. This command will give you the names as well as the filesystem paths for the location of your virtual environments.

How do I add Python to environment path in Windows 10? ›

For Windows 10/8/7:
  1. Open System Properties (Right click Computer in the start menu, or use the keyboard shortcut Win + Pause )
  2. Click Advanced system settings in the sidebar.
  3. Click Environment Variables...
  4. Select PATH in the System variables section.
  5. Click Edit.
May 19, 2010

How to setup Python local environment? ›

Install virtualenv and pip
  1. Install with Python 2.4. Run the following command: ...
  2. Install with Python 2.5-2.7. If your Python version is 2.5, 2.6 or 2.7, run easy_install with your Python version number, like this: ...
  3. Install with Python 3.4 or above. ...
  4. Create and activate your virtual environment.

How to run a Python program in Windows 10? ›

Navigate to the Script's Directory: Use the cd command to navigate to the directory where your Python script is located. Run the Script: Enter python scriptname.py, where scriptname.py is the name of your Python script. This command will execute your script.

How do I add a environment variable in Python Windows 10? ›

For Windows 10/8/7:
  1. Open System Properties (Right click Computer in the start menu, or use the keyboard shortcut Win + Pause )
  2. Click Advanced system settings in the sidebar.
  3. Click Environment Variables...
  4. Select PATH in the System variables section.
  5. Click Edit.
May 19, 2010

How to set up a virtual environment with a different Python version on Windows? ›

  1. Step 1: Install Virtualenv. ...
  2. Step 2: Create a Virtual Environment. ...
  3. Step 3: Activate the Virtual Environment. ...
  4. Step 4: Install a Different Python Version. ...
  5. Step 5: Verify the Python Version. ...
  6. Step 6: Install Packages. ...
  7. Step 7: Deactivate the Virtual Environment.
Nov 2, 2023

Top Articles
Trading: ¿Qué es y cómo funciona? - GBM Media
LAS 15 MEJORES PÁGINAS PARA VER SERIES ONLINE — Steemit
Target Dummies 101 - The Dummy Research/Tutorial Thread
Your Blog - Sheri Blonde
Revolve 360 Extend Manual
Academic Calendar Pbsc
Gateway Login Georgia Client Id
Ap Psychology Unit 8 Vocab
Mashle: Magic And Muscles Gogoanime
Red Wing Boots Dartmouth Ma
A Comprehensive Guide to Redgif Downloader
Jeff Siegel Picks Santa Anita
29 Best Free Sports Streaming Sites | Sept. 2024 (No Ads!)
Julia Is A Doctor Who Treats Patients
Short Swords Resource Pack (1.21.1, 1.20.1) - Texture Pack
Wasmo Link Telegram
Wvtm 13 Schedule
Weldmotor Vehicle.com
Exploring the Northern Michigan Craigslist: Your Gateway to Community and Bargains - Derby Telegraph
Starfield PC, XSX | GRYOnline.pl
Aluminum Model Toys
Truist Drive Through Hours
Gay Cest Com
Wsbtv Traffic Map
Logisticare Transportation Provider Login
Advance Auto.parts Near Me
Top 10 Best OSRS Ranged Weapons (Bows + Crowssbows) – FandomSpot
Minor Additions To The Bill Crossword
1084 Sadie Ridge Road, Clermont, FL 34715 - MLS# O6240905 - Coldwell Banker
Ulta Pigeon Forge
Any Ups Stores Open Today
Walgreens Rufe Snow Hightower
Blue Beetle Showtimes Near Regal Independence Plaza & Rpx
Grupos De Cp Telegram
Orylieys
Mercantilism - Econlib
Cashflow Manager Avid
Huskersillustrated Husker Board
R/Moissanite
Famous Church Sermons
Uw Oshkosh Wrestling
Scotlynd Ryan Birth Chart
Kortni Floribama Shore Drugs
Sams Warehouse Jobs
Transactions on Computational Social Systems - IEEE SMC
Stock Hill Restaurant Week Menu
Melisa Mendini Wiki, Age, Boyfriend, Height, Career, Photos
Is Chanel West Coast Pregnant Due Date
Stephen Dilbeck Obituary
Toothio Login
3220 Nevada Terrace Ottawa Ks 66067
Latest Posts
Article information

Author: Ouida Strosin DO

Last Updated:

Views: 6166

Rating: 4.6 / 5 (76 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Ouida Strosin DO

Birthday: 1995-04-27

Address: Suite 927 930 Kilback Radial, Candidaville, TN 87795

Phone: +8561498978366

Job: Legacy Manufacturing Specialist

Hobby: Singing, Mountain biking, Water sports, Water sports, Taxidermy, Polo, Pet

Introduction: My name is Ouida Strosin DO, I am a precious, combative, spotless, modern, spotless, beautiful, precious person who loves writing and wants to share my knowledge and understanding with you.