In this blog we will be exploring what are virtual environments in python and how to setup virtual environments.
Virtual environments are kind of wrappers of python whenever we create a virtual environment, it creates a python environment having no third-party package installed even if your global python has a lot of packages installed. then you can activate this environment and install your desired packages and the scope of these packages will be limited to this specific virtual environment so you can install dependencies of 1 project in 1 virtual env and activate this specific environment whenever you wish to work on this project so that your all of dependencies are there and neither global nor other virtual env packages can interfere with it.
say in a scenario you have 2 projects and you are not using virtual environments or anything you installed python started working on a project which was based on Django==1.11 so you simply did pip install django==1.11 and continued work on it. In parallel you want to start another project which is based on a different Django version say django==2.0 so you did pip install django==2.0 what this will do is uninstall the existing version i.e 1.11 and install newer 2 so essentially your first project which is expecting version 1.11 now getting 2.0 so, you can run into problems like this. This is exactly what we need virtual environments for.
In the virtual environment scenario, we would create 2 virtual environments for both projects which will be empty while creating and then we activate first and install dependencies of project 1 and likewise second for project 2 we activate the 2nd environment and install dependencies of the project 2 and whenever I want to work on project 1 I activate virtual environment 1 and whenever I want to work on project 2 activate virtual environment 2 which comes with all of the dependencies present in it.
This is all about why we would need a virtual environment and in which way we use them.
Now coming to the technical part how to actually create virtual environments.
there exist a package called virtualenv which handles this for us so we will dopip install virtualenv
this will install the virtualenv package in the global installation of python.
The next question arises is where to store these virtual environments there are a couple of standards used in the community:
Finally, we come to the creation of a virtual environment we will do something like:
go to the directory where you want to create itvirtualenv Venv
this will create virtualenv named Venv now we have to activate it
To activate virtualenv in linux and Macsource Venv/bin/activate
To activate virtualenv in WindowsVenv\Scripts\activate.bat
NOTICE: The slashes are the other way around for windows '\' backslash (for cmd)
Now you can install python dependencies using pip and every time you want to work on a project whose dependencies are in this Venv we need to activate it using this activate command.
so what If you want to deactivate it?
Its similar there exists a deactivate command
To deactivate virtualenv in linux and Mac
you can just saydeactivate
To deactivate virtualenv in WindowsVenv\Scripts\deactivate.bat
If you have a file that contains all of the requirements in it you can do something like.pip install -r requirements.txt
assuming requirements.txt here has your requirements.
If you want to create a file which has all the dependencies which is a must to have in your project.
with exact versions or you can generate automaticallypip freeze > requirements.txt
again assuming you want to extract all your requirements to requirements.txt file.