Create python virtual environment on windows and linux

article-featured-image

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
activate-virtual-environment

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:

(virtual_env)$
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:

(virtual_env)$
python -c "import requests; response = requests.get('https://google.com'); print(response.status_code)"
activate-virtual-environment

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:

(virtual_env)$
pip install requests

(virtual_env)$
python -c "import requests; response = requests.get('https://google.com'); print(response.status_code)"
activate-virtual-environment

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 (google.com) 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) $
deactivate

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:

activate-virtual-environment

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:

>
.\venv\Scripts\activate
activate-virtual-environment-on-windows

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

>
.\venv\Scripts\activate
setting-execution-policy

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:

(virtual_env)$
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:

(another_venv)$
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
protocolten-admin

Author: Harpreet Singh

Created: Mon 25 Sep 2023

Updated: 8 months, 1 week ago

POST CATEGORY
  1. Linux
  2. Windows
  3. Programming
Suggested Posts:
LINUX post image
Install Python 3.12 on Ubuntu 22 Linux

Choosing the right Python version for your Python application plays an important role in future-proofing …

CLOUD post image
Python Lambda function to convert S3 bucket images into PDFs

AWS Lambda is amazing. It's a serverless platform where you don't have to manually …

KNOWLEDGE post image
Read Binary Bits with these effective methods

You are most likely familiar with the concept of Binary Language. Yes, you guessed …

SECURITY post image
Large Data Encryption & Decryption using Cryptography

In the past few years, keeping your data safe and secure is challenging than …

SCRIPTS post image
Create your own personal Secure VPN on the Cloud

This article is about creating a secure personal VPN. Nowadays with all those privacy …

Sign up or Login to post comment.

Comments (0)