Coding, Python, Virtual Environment

Perks of coding with the virtual environment

Sayan Das
4 min readJul 26, 2022

Today we will talk about the difficulties of using the base environment in python coding and another environment called “virtual environments” to solve the problems. Also, we are sharing how to manage and use these virtual environments.

Problems arise from using the base interpreter

Let’s go back to the past when we started to learn python programming. We just follow our general guidelines to install python from the official website of python which will create a python environment for us to learn code and we started learning the coding using this installed python environment.

We all know that in python language we have to install a lot of modules that are available on the internet. Sometimes we also update our python modules to a newer version. In doing these we just used the python interpreter that we initially installed on our device. Soon after a couple of days of coding, we find that some of our previous programs are not working.

This happens because the previously written code is using some other version of modules and as we have updated our modules those newer modules are not working on the older version of the code.

Also, we find, in our base environment that several modules are used sometimes by us to do a specific job and once it is done that module will be of no use.

For these reasons, we ran into a mess handling our base environment. To sort out these problems we come for help from something that is called a virtual environment.

What is a Virtual Environment?

The virtual environment is just a clone that is created inside from the original base python environment that is installed into our device, which can be maintained independently from the base environment.

Suppose we are working on a project that requires some specific kind of python module(s), especially for the project. Once the project is over that specific model will be of no use. Under these circumstances, we can create a virtual environment inside the main project directory. Then we can install that specific module(s) in the newly created environment and build our entire project based on it. Once the project is done we can deactivate the environment. Let's see, how we can create a virtual environment using the command prompt (cmd).

How to create a virtual environment in windows cmd?

Suppose we have our base python interpreter installed in the folder C:/python/python_version1 and we want to use this interpreter to create a virtual environment in a project folder D:/user/project1. Suppose the name of the virtual environment is project1venv then the cmd command to create the virtual environment is -

C:\user>D:
D:\>cd D:/user/project1
D:\user\project1>C:/python/python_version1 -m venv project1venv

After this, a virtual environment folder will be created inside D:/user/project1. Now to use this environment we need to access this. When we write codes we use some IDE. We have to add the path of this newly created virtual environment in the “Select interpreter” option. If we want to access it from the cmd then we need to write the following line of codes.

D:\user\project1>cd project1venv/Scripts
D:\user\project1\project1venv\Scripts>activate
(project1venv) D:\user\project1\project1venv\Scripts>

Once we activate the environment we can see that the name of the environment appeared in the front of the prompt. Now if we want to install some module inside the environment then we should write-

(project1venv) D:\user\project1>pip install numpy

This will install the numpy package inside the project1venv environment. To deactivate the environment we write

(project1venv) D:\user\project1>deactivate
D:\user\project1>

Advantages of using a virtual environment

We get a lot of advantages when we use a virtual environment setup.

  • Using the virtual environment we can maintain project-specific modules independently of our base environments.
  • There are several specific modules that we use for specific projects and we hardly ever use them again. In that case, it will be unnecessary to store this module inside the base environment. We can install them inside the virtual environment, which we can deactivate, once our project is done.
  • Here we don’t have to worry about the version of the packages that are particularly used in the project. We can always check inside the environment for the available packages and their versions and we can also keep a record of these in a “requirement.txt”. So, if we transfer our code to someone else’s device then even if his module and version in his base environment do not match, he can always create a virtual environment and use this “requirement.txt” to download and install all the required modules with specified version, without disturbing his base environment.

--

--

Sayan Das

Stat Graduate, Studying Masters in Data Science, Data Science Aspirant, Python & R Coder,