How to setup Python VirtualEnv - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorials we are going to see what is Python Virtual Environment and how to setup Python VirtualEnv in different operating systems.
Virtual Environment (virtual env or venv) is a tool that helps to keep the libraries and dependencies required for a project separated from others. Using this, one can install the requirements specific to a python project and prevent them from interfering with other projects.
Consider you are working on a project that uses the Django version 3.1 and at the same time, you want to run a Django application that uses the older 3.0 version. With time, new features are added to any package and some features become deprecated, thus, both the different versions of the applications need that particular version of Django installed to work properly, else there might be dependency issues.
Using virtual environments, one can install the packages separately for each application and keep them along with that project confined to a directory. Also, multiple environments can be activated simultaneously in different terminals.
It is a general practice to begin any python project in a venv to prevent any issues in future.
Ensure pip is installed and added to the PATH variable and then install virtualenv using the command:
pip install virtualenv
Open the terminal or Command Prompt and navigate to the directory where the project needs to be created. Run this command to create a virtualenv with the name env.
virtualenv env
You can name the virtualenv as you wish for eg. something like env, venv or project-specific: testappenv, blogappenv. 
This will create a folder with that name with all the required scripts within that.
Windows
env\Scripts\activate
Linux
source ./env/bin/activate
Once the virtualenv is activated, (env) will be visible on the starting of the command line.
After the work for the python environment is done, you can deactivate it using the command:
deactivate
When working on a shared project or when you want to run your project on some other machine, it is essential to install all the required version of the dependencies first. For this, pip has a feature called freeze to make a list of all the installed packages which can then be used to make a clone of the venv in some other machine.
Once in an activated virtualenv run the command:
pip freeze > requirements.txt
This will list all the dependencies for that venv and store them in a file named requirements.txt.
requirements.txt file is used for specifying what python packages are required to run the project and is supposed to be present in its root directory.
The requirements.txt file is used to replicate any venv. Once this file (or list of dependencies) is available:
Create and activate a virtualenv
Run this command
pip install -r requirements.txt
This will install all the packages as per the version number given in requirements.txt in the newly created venv.
pip install -r requirements.txt
This will install all the packages as per the version number given in requirements.txt in the newly created venv.
Project PyEnv
Happy Learning 🙂

Python Django Helloworld Example
How to upgrade Python PIP version on Windows
Where can I find Python PIP in windows ?
Python Selenium HelloWorld Example
How to connect MySQL DB with Python
STS Gradle Setup Tutorials
How to pass Command line Arguments in Python
How to check whether a file exists python ?
Modes of Python Program
Python raw_input read input from keyboard
How install Python on Windows 10
Python Selenium Automate the Login Form
Python – Selenium Download a File in Headless Mode
How to get Words Count in Python from a File
What are Python default function parameters ?

Python Django Helloworld Example
How to upgrade Python PIP version on Windows
Where can I find Python PIP in windows ?
Python Selenium HelloWorld Example
How to connect MySQL DB with Python
STS Gradle Setup Tutorials
How to pass Command line Arguments in Python
How to check whether a file exists python ?
Modes of Python Program
Python raw_input read input from keyboard
How install Python on Windows 10
Python Selenium Automate the Login Form
Python – Selenium Download a File in Headless Mode
How to get Words Count in Python from a File
What are Python default function parameters ?