Virtual Environment for Python based application

Python virtual environment: Here, we are going to learn what is python virtual environment, why do we need a virtual environment, and how to create a virtual environment in Python3? By Sapna Deraje Radhakrishna Last updated : December 16, 2023

What is Virtual Environment in Python?

Virtual environments are very useful when the application requires a separate environment, each using its version of python and libraries for execution. Similar to virtual environment, there are packaging tools like "Anaconda" and "Miniconda".

The virtual environment is the easiest and recommended way to configure a custom python environment.

Why do we need a virtual environment?

The virtual environment is a clever way to keep the python setup isolated with other projects. As a user, one can have multiple virtual environments in a single machine and each virtual environment can have different libraries as per the requirements. We can also package a virtual environment like a zip file with all dependencies needed for the application and deploy it on serverless service like AWS lambda.

How to create a virtual environment using Python3?

Step 1: Install virtual environment

Below are the commands to Install Virtual Environment:

pip3 install virtualenv
Collecting virtualenv
  Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)
    100% |████████████████████████████████| 1.8MB 367kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-15.1.0

Step 2: Create the virtual environment

Using python3, the command to create the virtual environment and the syntax is 'python3 –m venv /path/to/create/the/virtual/env'.

-bash-4.2$ python3 -m venv test_venv
-bash-4.2$ ls
test_venv
-bash-4.2$

Step 3: Activate virtual environment

Below is the command to activate virtual environment:

-bash-4.2$ source test_venv/bin/activate
(test_venv) -bash-4.2$

Step 4: Install the required libraries

Install the required libraries, using pip (for the example below, we have used 'flask' library)

(test_venv) -bash-4.2$ pip3 install flask
Collecting flask
  Downloading https://files.pythonhosted.org/packages/9b/93/628509b8d5dc749656a9641f4caf13540e2cdec85276964ff8f43bbb1d3b/Flask-1.1.1-py2.py3-none-any.whl (94kB)
    100% |████████████████████████████████| 102kB 3.8MB/s
Collecting Jinja2>=2.10.1 (from flask)
  Using cached https://files.pythonhosted.org/packages/1d/e7/fd8b501e7a6dfe492a433deb7b9d833d39ca74916fa8bc63dd1a4947a671/Jinja2-2.10.1-py2.py3-none-any.whl
Collecting click>=5.1 (from flask)
  Using cached https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl
Collecting itsdangerous>=0.24 (from flask)
  Using cached https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting Werkzeug>=0.15 (from flask)
  Using cached https://files.pythonhosted.org/packages/ce/42/3aeda98f96e85fd26180534d36570e4d18108d62ae36f87694b476b83d6f/Werkzeug-0.16.0-py2.py3-none-any.whl
Collecting MarkupSafe>=0.23 (from Jinja2>=2.10.1->flask)
  Using cached https://files.pythonhosted.org/packages/b2/5f/23e0023be6bb885d00ffbefad2942bc51a620328ee910f64abe5a8d18dd1/MarkupSafe-1.1.1-cp36-cp36m-manylinux1_x86_64.whl
Installing collected packages: MarkupSafe, Jinja2, click, itsdangerous, Werkzeug, flask
Successfully installed Jinja2-2.10.1 MarkupSafe-1.1.1 Werkzeug-0.16.0 click-7.0 flask-1.1.1 itsdangerous-1.1.0
 (test_venv) -bash-4.2$

Python Tutorial

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.