What is python virtual environment and how to use it? in 2021

Python virtual environment

Python virtual environment, what is it, and do we need this? Hi, guys today in this article we will discussing everything about the python virtual environment. With the end of this article, you will know, what is python’s virtual environment, why we need it, and how to use it. So let’s get started

What is a python virtual environment?

Python virtual environment or venv in python is nothing but an isolated environment, where the version of certain libraries, packages, or python itself is kept frozen. It is generally created inside a folder that is completely disconnected from its outer world. So, any new changes or update of those libraries, packages or python in the outer world doesn’t affect the things installed in the virtual environment. It is believed to be a good practice to create a venv for any project.

Why do we need a virtual environment?

There are a lot of reasons for this but the popular ones are dependencies, and version (of python packages) issues. 

Suppose you are working on a project, you installed required libraries and packages of certain versions and started working on it. After a few days/weeks of completing it, you tried to test it again before sending it to your client, but guess what? It didn’t work because XYZ libraries got updated and some functionality was changed, this is the most annoying problem one can face.

Generally the libraries and packages we use to keep getting improved constantly, and many times some of the functionality of certain libraries, packages, or python itself gets updated, removed, or replaced by other functionality. Due to that we often face this kind of issue

To deal with this kind of issue we can take help from two things docker (read: What is docker and why do we need it?) or a python virtual environment. As we discussed earlier, a virtual environment or venv creates an isolated environment and freezes the version of all libraries and packages installed in it and we never run into these kinds of problems. 

How virtual environment works?

A virtual environment is created with the help of a module named virtualenv. You first need to create a separate folder and then virtualenv creates an isolated environment and installs all the required libraries and packages you would need for your project. The libraries and packages inside the environment are kept separate and frozen so its version is not affected by any new updates. 

When you first create a new virtual environment you just have python and pip and you don’t have any libraries and packages installed. Also while creating an environment you can specify to copy all the libraries and packages installed in your main environment into your virtual environment if you don’t have any specific requirement.

Benefits of Virtual environments 

  • You can have multiple environments with a different version of libraries and packages
  • You don’t need to worry about updates and changes
  • You can easily share your environment with your client with all the dependencies and versions
  • Easy to delete the environment, you can just delete the folder

Requirement for creating virtual environments

  • Obviously you will need to have python installed in your system
  • You must have pip installed, generally, pip is pre-installed with python 3.4 and later version

How can we create Virtual environment?

To create virtual environment we first need to install python virtual environment module, that you can install by this command

pip install virtualenv

After installing it, we will create a new separate folder where we will create our isolated virtual environment, you can do it manually for you can do it by code. Here I will create a new folder on the desktop, so first I will change my directory (by “cd” command) and then create a new folder named “venv” by the following code

mkdir venv
python virtual environment
Creating new folder

Now, we have our new folder, so we will go inside that folder by changing our directory (‘cd venv’) and then we will create our virtual environment named “virtual” there, by this command. And one thing to note here is, there will no libraries and packages installed in this new environment.
Also read -> Twitter sentiment analysis with Elon Musk’s tweets python project

virtualenv virtual
python virtual environment
Creating Virtual environment

And Viola! we have created our virtual environment, now let’s see what’s inside.
So, As you can we have two folder and two files, which are ofcourse python files.

python virtual environment
Inside Virtual environment

Activate and setup enviroment

So, what now? Okay we have just created our environment, but in order to work in this environment we need to activate it by the following code.

.\virtual\scripts\activate

Also I am putting my virtual environment, so use this code only if you named your virtual environment as mine else put your virtual environment inplace of virtual. Now in order to check that our environment is activated or not you can find the environment name at starting of every line, if that is present then it is activated else not.

python virtual environment
Activating virtual

Now you can install here any libraries if want like pandas, numpy etc and it will be remain isolated.

pip install pandas
pip install numpy

Check all installed libraries

If you want to send the library and packages you used in a certain project, let’s say this project with their version then you can send it to him by generating a requirement.txt file. You can do that by the following code

pip freeze > requirement.txt

Let’s see how the text file looks like, by the way I have just installed pandas and rest are installed because they are dependencies of pandas

python virtual environment
Requirements file

you can install all these at once if you want to by the following code

pip install -r .\requirement.txt

Deactivate and Delete environment

Now to deactivate your environment you can simply type “deactivate” and your environment will be deactivated and you will return to the main python environment. You can reactivate it back again when you need it by the above code

deactivate

And incase you want to delete this environment, then you can simply delete the folder “venv” as a normal file.

Cloning our existing environment

Till now we talked about creating an environment with nothing installed in it, but what if we want to create our environment with everything installed in it as in your main environment. So, for this, we can use this code
Here we are creating new environment named ‘newV’

virtualenv --system-site-packages newV

so, how do we know if it installed everything from our main environment or not, you can do this by checking all installed libraries by the code which we generated our requirements.txt.
Here we are generating our file with the name of ‘newVreq.txt’

pip freeze> newVreq.txt

Let’s see newVreq.txt

python virtual environment
Cloning main environment

The list is pretty long so it was hard to capture the full notepad, well I hope you get the idea. If you want to see other functionality of the virtual environment then you can run this command

virtualenv -h

So, this was all about the virtual environment if you want to learn more about it then you can read it here, hope you liked the article and if you did then pls comment down below and share your experience.

Share this post
Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on email
If you love this article, You can support me on Patreon 🙂

Your support will help me on growing this site, sharing great articles, projects, and cheatsheets. 

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments