Pip — Requirements & Virtual Environment (Part 2)
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
To show it use — requirments.txt
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.
Now make a text file containing beautifulsoup4 and its version like this: -
To install this package use-
Pip install -r abc.txt
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?
See (virtuals) before C:\Users …. It shows that now your are in virtual environment.
Let’s check
I am glad that you read till the end!
Hope you’ve enjoyed it.