Pip — Requirements & Virtual Environment (Part 2)

Nidhi Jakhad
3 min readDec 21, 2021

--

In the last part, we saw that how can we use pip basic commands.

Let’s dive into installing packages using requirements.txt. But, wait — what is requirements.txt?

Simplifying with an example: — Let’s say you wrote some code using Pandas library. And, in your machine(pc), it was the latest version. But, in your friend’s machine, it is still an older version available which doesn’t have the same functionalities as the latest has. Hence, your friend won’t be able to run that code. Your friend needs the same version of pandas as yours in order to run your code.

Similarly, in bigger projects before running the code which is written on one machine to another. We should check whether the requirements are up to date. This can be done using requirements text file where your friend (from example) can store the requirements (package versions) of the code and send it to you along with the code so that you can download the same requirements before you run it.

But, how to make a requirements text file?

As from part 1, pip list command shows all the packages installed on your pc. So, let’s first make the file that contains all the packages.

- Pip list freeze > requirements.txt

The above command freezes(stores) all the requirements in the text file name as requirements.txt. You can give any name to instead of requirements but the extension should be only.txt

It won’t give you any output but your requirements file is ready!

To show it use — requirments.txt

The file will pop up on your screen wilt all the installed packages along with their versions.

And now, you can go to your directory to find the requirements file. In my case, I will find it in the C drive then the Users folder then Nidhi folder.

Now, in another case where you will take the requirements.txt file from someone then how to download packages using this.

For example- First I’ll uninstall beautifulsoup4 package then I will install it using requirements file.

Hence uninstalled

Now make a text file containing beautifulsoup4 and its version like this: -

abc text file containing package name and its version. This file should be in the same folder that is in my case it should be in folder Nidhi.

To install this package use-

Pip install -r abc.txt

That is how you can download packages with the specified versions from requirements file

Virtual Environment

What is a virtual environment and why do we need it?

You have 2 projects and in both the projects different versions of pandas is needed but as we see we can have only one version at a time in a machine. So to accomplish both the projects we create a virtual environment — basically another light-weighted python in your machine. Now, you can interact with two pythons. As a result, you can do your 1 project in system’s python and another in the virtual environment.

How to create a virtual environment?

python -m venv path(where you want to create a virtual environment). I found my virtual environment in Virtuals folder.
Note: still your virtual environment is only created but not activated. To activate it go to scripts and type activate

See (virtuals) before C:\Users …. It shows that now your are in virtual environment.

Let’s check

Pandas is not installed in virtual environment.
But it is installed in the system’s python.

I am glad that you read till the end!

Hope you’ve enjoyed it.

--

--

Nidhi Jakhad
Nidhi Jakhad

Written by Nidhi Jakhad

Loves to write on Research, Values, Leadership, Python, Finance-Markets(coming soon) | Eager to Acquire Consulting skills| Photographer- addicted to aesthetics

No responses yet