Why code on Python using Virtual Environments?

Madhuresh Gupta
Quick Code
Published in
5 min readApr 25, 2020

Python has now been widely adopted for everything ranging from first language to introduce a student to the world of coding to develop the logic for real-time deep learning software for self-driving cars.

But still many of them don’t have a clear idea how to actually start with working on a Python project. Some use PyCharm IDE for first project which confuses many rookie coders as they get overwhelmed by the huge amount of advanced options and settings available so as to get started with a project. Let me try to make those section of people clear on how to proceed.

First off: Kindly install Micrsoft VSCode editor which is currently world’s most used lightweight editor.

So you might be knowing every Python project requires some additional libraries to be imported like numpy, pytorch, scipy, etc. But the biggest mistake we often do is perform a pip install in the global installation of python in our laptop.

Every python project should be started with creation of a Virtual Environment (more commonly venv) which would maintain the requirements of project

Next what we usually do is that just “pip install” bunch of libraries for our next project and loose track of what all we need once the project needs to scale up. That’s because the project was not initialized in a virtual environment!

Why we need a virtual environment?

The purpose of a virtual environment is to have a space we can install the packages that are specific to a certain project. Like for example — let’s say you have lot of Django sites which use Django ver 1, but now for newer projects you want to make use of Django ver 2.

Well then if you are just using a single global environment for all projects then when you update the Django package, it might break many of the old projects which would become incompatible with the newer version. Hence we don’t want all our Django projects pointing towards a single package. So each project should have it’s own packages seperated from each other such that we don’t end up breaking old code base.

Also having a separate environment for each project is easier to manage and maintain it’s dependencies. So lets get started!

First let me show you the list of modules that I have in the global installation of python:

Now I will navigate to my destop where I have my Project folder inside which we will make our virtual environment.

We will use the following command to create a virtual environment:

python -m venv <your virtual env name>

I have created environment with folder name “.venv”. This is a common nomenclature used by developers (some even go with simply “venv”).

Remember to be able to execute this command, you need to first “cd” to the root directory of your project folder.

Now to activate this newly created environment we will use the following command in powershell:

.venv\Scripts\activate.bat

So now we can see the environment name (.venv) appears in the beginning. This means you have successfully activated you first virtual environment. So now when you install some packages (pip install) here, you will only be installing for this particular environment.

For example I want to install 2 python packages name numpy and scipy, as I will be doing a machine learning project here then I will simply use the command:

pip install numpy pip install scipy

Thus we installed numpy and scipy only to our environment, isolated from rest of my projects and even the global installation of python.

Moreover if we want to export the project, using virtual environment has another feature where you can auto generate a requirements.txt file which other developers can use while cloning your project and install all of the same packages and dependencies that you were using. So to do that, first we will use:

pip freeze

This will produce a list of installed packages in your evironment, but uses the output which pip install expects. A common developer convention is to put the output in a requirements.txt file using the following the command:

pip freeze > requirements.txt

Now you will see a new txt file is created in your project’s root directory.

Lets see how this virtual environment is selected inside VS Code

By default, the Python extension in VSCode looks for and uses the first Python interpreter it finds in the system path.

To select a specific environment, use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P).

Here we can see multiple global instances of Python alongside the “env” folder which the VSCode automatically found inside the project root directory.

The requirements.txt can then be committed to version control and shipped as part of an application. Users can then install all the necessary packages with install -r

Now we know — how to create virtual environment, how to export it to a requirements text file, let’s see how we would use this requirements.txt file to install all the dependencies while cloning someone’s project.

So the initial steps remain the same i.e, after cloning the project to your machine, you create a virtual environment in the root folder of project after which you will activate the virtual environment. Next you execute the following command:

pip install -r requirements.txt

So finally we have learnt how to create virtual environments, export the packages to requirements.txt and finally how to install someone’s project dependencies too.

Just one last tip: Kindly have a .gitignore file in your project repo and add the name of your virtual environment folder name so that you inadvertently don’t commit to version control.

Originally published at http://madhureshgupta.home.blog on April 25, 2020.

--

--

Madhuresh Gupta
Quick Code

Follows Microsoft, Tesla Motors, SpaceX, Google, Elon Musk. Aviation Admirer!