Ayushi Garg
Setting the virtual environment in Python
3 min readMay 16, 2020

--

Before starting with the set up of Virtual Environment in Python, there is a question arises why one should use Virtual Environment, what is the need of it?

Here is the answer, the main purpose of virtual environments in Python is to create an isolated environment for projects. This means that each project can have its dependencies, regardless of what dependencies every other project has.

Example: If I installed an older version of Pandas and develop various projects in it but now I want the latest version of Pandas in my new Project, so if I update, all the older projects stopped working and if I don’t then this new project doesn’t work. So, here comes the use of a virtual environment, now for using the latest version of Pandas we create a virtual environment where we install a new version of Pandas in this way we could run our new project and older ones successfully.

Steps to create Virtual Environment in Python(Here I use Windows)-

  1. First, we need to install Virtual Environment, for that open cmd, go to the folder where u want to install it and type pip install virtualenv
Installing virtualenv

2. Now I create the virtualenv named as venv, you can name it anything. This creates a folder named venv, which will contain all the packages and requirements of a project. (You can see in the above picture)

3. If You want to check whether virtualenv is successfully installed or not, then type pip list and if it is installed then u will find its name in the list of packages.

virtualenv is successfully installed

4. The next step is to activate the virtual environment so that all the packages and requirements of the project will be installed in folder venv. For that, we type venv\Scripts\activate. Sometimes there is a problem occur in the activation so run cmd as administrator and install packages here.

Here we enter into the virtual environment and I install numpy

5. If You want to have the complete list of packages and dependencies used in this project for that we use pip freeze > requirements.txt, it creates a file requirements.txt file having all dependencies and packages used in this project.

Here it creates requirements.txt, have all dependencies and packages for this project

6. If You want to deactivate the virtual environment then type deactivate in cmd, in this way you come out of it, if I import any package suppose numpy there appears error not found because that package is installed not in system interpreter but in venv only.

Here comes error because numpy was there in venv but not in system interpreter.

These are some of the steps that are required to set up the virtual environment in Python.

Happy Learning!

--

--