Questions And Answers

More Tutorials

Python Virtual Environment & Requirements.txt

First, we will create a new folder. After opening the folder we will now open our PowerShell window using shift + mouse right-click.

Our PowerShell window will appear like this:

Note that the path it is showing is of our current folder. That is actually the benefit of using shift + mouse right-click.

Now let us move on to some theoretical concept.

A virtual environment is a tool or an aid provided to us by Python to keep the dependencies that we have utilized earlier in a few projects, constant. In simple terms, after some duration, Python keeps on launching and upgrading its versions. The new versions yet being better can have certain disadvantages for few users, because, in every new update, new functions are added to the modules, and previous ones may be upgraded. So, there is a chance that a function that used to work earlier will not work as it used to.

To save ourselves from such situations, Python has allowed us to use of a virtual environment.

What virtual environment does?

Virtual Environment saves the current state of our compiler along with the state of their modules and libraries. So in this way even if Python has made certain changes in its module, our virtual environment can still work as before even after years. We can also install different packages and “dataframes” in our virtual environment.

To be more clear, the virtual environment works exactly the same way as the Python we have installed on our windows/mac/Linux currently because a virtual environment is just a clone of the original product.

Using Virtual Environments

To get started, install the virtualenv tool with pip:
$ pip install virtualenv

virtualenv is a tool. It is used to create isolated Python environments.

To assign a name to your virtual environment, use command
$ virtualenv virtualenv_name

After running this command, a directory named folder_name will be created. This directory will contain all the necessary executables to use the packages that the Python project would need. Python packages will be installed in this directory.

Now, after creating a virtual environment, you need to activate it. When you open the directory, it contains folders like include, lib, script, and tcl. When you open the script folder, you'll find a file activate.bat. You can activate the virtual environment by simply clicking on this file or using the command prompt and writing the following command.
$ .\virtualenv_name\Scripts\activate

Once the virtual environment is activated, the name of your virtual environment will appear on the terminal's left side.

When you are done working in the virtual environment for the moment, you can deactivate it:
(virtualenv_name)$ deactivate

Now moving onto requirement.txt. When we run a certain command pip freeze > requirement.txt, a file will be generated in the directory where our virtual environment is based. The file will contain all the details related to the external packages that we have installed along with their versions. By having the requirement.txt file, we can create our virtual machine again easily by downloading all the same libraries, having the same versions by a simple command.
freeze > requirement.txt

The requirement.txt folder will contain data like this:

We can install all the packages one by one by a command:
pip install package_name == version

But in case we have a large number of libraries installed, this will take a massive amount of time as we have to install each one by one so we have another method by which we can install all the packages at once by using the requirement.txt file. The syntax would be:
pip install -r .\requirements.txt


In this page (written and validated by ) you learned about Python Virtual Environment & Requirements txt . What's Next? If you are interested in completing Python tutorial, your next topic will be learning about: Python Enumerate Function.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.