How to create Python Virtual Environments
This tutorial is for How to create Python Virtual Environments. A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating. the virtual environment uses specific projects in the required packages installed so it can not affect another project. the virtual environment is one of the most important tools.
You are working on two web-based python projects one uses request 2.7.0 and the other uses request 2.25.1 and so on. In this situation, the virtual environment can be really useful to maintain dependencies of both projects using requires packages.
Check your Python installation has to pip
pip -V
If you see something like this: pip 20.3.3 from /usr/local/lib/python3.8/dist-packages/pip (python 3.8)
text so you have pip installed, otherwise download and install pip.
Installing virtualenv
pip install virtualenv
Check your installation:
virtualenv --version Output : virtualenv 20.2.2 from /usr/local/lib/python3.8/dist-packages/virtualenv/__init__.py
Create the virtualenv
To create a virtual environment, you must specify a folder path.
virtualenv devnote
Run this command to create a directory named devnote will be created. Go to devnote directory and activate the python environment by running the following command:
# Linux / Mac OS
source bin/activate
# Windows
devnote\Scripts\activate
The virtual environment is activated, and the name of your virtual environment will appear on the left side of the terminal e.g. (devnote) root@ubuntu:/home/dev/Desktop/devnote#. Now work with your virtual environment.
The requests 2.7.0 package will be placed in devnote folder and will be isolated from the complete system.
pip install requests==2.25.1
Check install packages
Check how many packages are installed in your environment.
pip freeze
Deactivate the virtualenv
Once done with the work, you can deactivate the virtual environment and use your original Python environment by the following command:
deactivate