Create python virtual environment on windows and linux

Created: Mon 25 Sep 2023 Updated: 4 months ago


Creating and managing a Python virtual environment is very crucial part of any project. Suppose you are working on multiple projects on a single system. Every project has different package requirements and its own Python version dependencies. To work on multiple projects while utilizing the same system, virtual environment plays an important role in isolating the projects.

Create python virtual environment in Linux

To demonstrate this process on Linux-based systems, I'm using Ubuntu 22.04 LTS distribution but the same method applies on all the other distributions. Along with python3, there are some Linux packages required for creating a new python virtual environment. Use the below command to install these packages:

sudo apt update

sudo apt install python3 python3-pip python3-venv

After the packages installation, run python3 --version to verify and check your python version. python3-pip is required to install python packages and python3-venv is used to create virtual environment itself.

Now run this command to create new virtual environment:
python3 -m venv virtual_env

It will create a new python virtual environment named virtual_venv in your current directory. To use this environment for a project, you need to activate it first. Run this command:

source virtual_env/bin/activate

Your virtual environment name, in this case, is virtual_venv, appearing before your username in the terminal is a sign that this environment is activated. Any python script you execute from now on will be executed inside this virtual environment. As explained earlier, every virtual environment has its own packages. To check the list of installed packages, run this command:

pip freeze

As this is a newly created virtual environment, the packages list will be empty because we haven't installed any packages yet. If you execute a python script that requires external packages, then the script will give you no module named <package-name> error. To test it, use the following command and check the output:

python -c "import requests; response = requests.get(''); print(response.status_code)"

Output produced by the above-mentioned command is simply an error stating that there is no requests package installed in the current virtual environment. Now install the required package and then check the output of same command:

pip install requests

python -c "import requests; response = requests.get(''); print(response.status_code)"

Now this time the output is different. After installing the required package, the python command returned code 200 which means the domain used in the command ( is live and this acknowledgment is the proof that our virtual environment is working. Now use pip freeze command again and you'll see some packages installed instead of an empty list.

To deactivate the virtual environment, use the below command:
(virtual_env) $

Create python virtual environment in Windows

Creating and setting the Python virtual environment for Windows system is different from Linux systems. The first requirement is to install the python software. Go and Download Python from their official website. While installing python, make sure to select Add pyhton.exe to PATH option as described in below image:


After installtion is completed, verify the installation by running the below command in Powershell:

python --version

Above PowerShell command will print the version of the installed Python. Now to create a new virtual environment, unlike Linux, you don't need to install extra system packages or software. Just use the following command in PowerShell:

python -m venv venv

This command will create venv named virtual environment in current folder. Now in order to activate this virtual environment, we need to set the execution policy for running scripts in powershell. Without setting the execution policy, you cannot activate the virtual environment. To test, use the following command in Powershell and check the output:


The output is an error message stating that scripts cannot be executed in Powershell because the execution policy does not allow it. In our case to activate a virtual environment, first, we need to set the execution policy. Run powershell and use the following commands to set policy rule and activate virtual environment:

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass


As you can see in the image above, after changing the execution policy, we are able to activate the virtual environment. The rest of the pip commands are same as in Linux. Use pip install <package-name> to install required package and pip freeze to get list of installed packages.

Generating pip requirement file

One of many benefits of using virtual environment is that we can import a requirements.txt file which is a list of all installed packages in the current environment. (requirements.txt is just name mostly used by developers to name the import file, you can name it anything). To import the packages list, below command can be used for both Linux and Windows systems:

pip freeze > requirements.txt

This command will generate a new file containing the list of all installed packages and their versions. To install these packages in another virtual environment, use the below command after switching to the appropriate environment:

pip install -r requirements.txt

Installing packages from requirements.txt file might take some time depending upon the number of packages to be installed. Just make sure both virtual environments are running the same Python version.

Setup virtual environment

Author: Harpreet Singh
Server Administrator

  1. Linux
  2. Windows
  3. Programming
Suggested Posts:
LINUX post image
How to deploy Django application with Nginx, MySQL, and Gunicorn on Ubuntu

Django is an open-source web framework based on Python. It can be very efficient …

LINUX post image
Top text processing and filtering commands in Linux

In this article you'll get to know about some of the best text processing and …

LINUX post image
Configure Django with Apache, MySQL and WSGi on Ubuntu

In this article, I'll be demonstrating how we can deploy Django website on an …

SECURITY post image
How to hide your public IP Address

Since the boom of the internet, securing online presence has been one of the toughest …

LINUX post image
Install Latest Version of PHP on CentOS 8 and 7

CentOS is great. I have to admit that all those SELinux enforcement and other …

Sign up or Login to post comment.

Sign up Login

Comments (0)