Home » Python

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?
Submitted by Sapna Deraje Radhakrishna, on September 25, 2019

What is Virtual Environment?

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?

Step1: 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

Step2: 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$

Step3: Activate virtual environment

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

Step4: 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$





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.