Feel free to contact.
Shahraiz Ali
12 Oct 2019
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.
Explaining why we need it with example:
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 do
pip install virtualenvthis 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:
- Create a directory say all-envs somewhere near the root maybe close to Documents and store all the environments for all the projects there so whenever you want to look for them go to that folder look for the project name and you will have your env there.
- Second and my personal preferrable is to create in the project directory itself and make sure the .gitignore file has its directory name defined so it's not pushed to git.
Create Environment
Finally, we come to the creation of a virtual environment we will do something like:
go to the directory where you want to create it
virtualenv Venvthis will create virtualenv named Venv now we have to activate it
Activate Environment
To activate virtualenv in linux and Mac
source Venv/bin/activateTo activate virtualenv in Windows
Venv\Scripts\activate.batNOTICE: 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?
Good question...
Deactivate Environment
Its similar there exists a deactivate command
To deactivate virtualenv in linux and Mac
you can just say
deactivateOR
source Venv/bin/deactivateTo deactivate virtualenv in Windows
Venv\Scripts\deactivate.batInstall multiple requirements at a time
If you have a file that contains all of the requirements in it you can do something like.
pip install -r requirements.txtassuming 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.
- One way is that you keep track manually and add to a txt file like
django==2.0.2
django-rest-framework==1.9
with exact versions or you can generate automatically
pip freeze > requirements.txtagain assuming you want to extract all your requirements to requirements.txt file.