6 Things I Wish I Knew Earlier About requirements.txt
# Learnt from working with enterprise Python apps

1) How to install everything inside requirements.txt
requirements.txt contains the required libraries for a project that needs to be installed (usually using pip). A simple example:
library-one==1.0.1
library-two==3.1.4
library-three==100.1.0To install all 3 libraries, we can run the command:
pip install -r requirements.txtwhich will install every single line inside requirements.txt
2) How to generate requirements.txt
pip freeze
This command prints out in the terminal 1) every external module you have installed in whatever Python you are using 2) their exact versions. Something like this will print in the terminal (truncated):
absl-py==1.4.0
altair==5.0.1
anyio==3.6.2
appdirs==1.4.4
appnope==0.1.3
argon2-cffi==21.3.0This is essentially the stuff inside a requirements.txt file. Since we want these lines in a text file, but don’t want to manually copy paste it, let’s use the following command to write it to a file.
pip freeze > requirements.txt^ whatever output from pip freeze will be written to a text file with the name requirements.txt. And there we have it — how to generate a requirements.txt file!
3) It is better to use a Python virtual environment to create a requirements.txt
When we pip freeze, EVERY SINGLE installed library will appear in your requirements.txt. Which can get annoying — what if your project simply needs numpy and pandas, but requirements.txt will install another 100 unnecessary libraries.
When we use a Python virtual environment, we create a new instance of Python that is separate from your main Python.
python -m venv env # create a virtual environment called 'env'# activating our Python virtual environment
env\Scripts\activate.bat # Windows
source env\bin\activate # MacOS/LinuxAfter we create a new virtual environment, it has NOTHING installed. So we need to install everything we need for our project (only once). And when we pip freeze, the stuff that ends up in our requirements.txt will contain only the stuff we installed for the project.
4) We can leave out the version in requirements.txt
Here’s a normal requirements.txt
numpy==1.23.5
pandas==1.5.2numpywith a specific version1.23.5will be installedpandaswith a specific version1.5.2will be installed
Now, here’s a requirements.txt without the versions
numpy pandas
numpywith the latest version will be installedpandaswith the latest veersion will be installed
We don’t necessarily need to include the version all the time.
5) It doesn’t have to be named requirements.txt
pip install -r requirements.txt-rmeans toreadrequirements.txtis just an arbitrary text file named out of convention
We can actually name it whatever as long as it’s a valid text file name.
pip install -r applepie.txt^ pip will then look for a applepie.txt, and install every single line inside.
Note — if you’re working on an enterprise-level app, please ensure that you name your requirements.txt files properly eg. requirements_local.txt
6) requirements.txt can contain other requirements.txt files
Let’s say we have a 2 requirements.txt files
requirements.txt requirements_windows.txt
requirements.txtcontains the main stuffrequirements_windows.txtcontains specialized windows libraries
Since we want requirements_windows.txt to also contain the stuff inside requirements.txt, we can do this. In requirements_windows.txt:
library-one
library-two
-r requirements.txtlibrary-oneandlibrary-twowill be installed- everything in
requirements.txtwill also be installed
Conclusion
Hope you learnt at least one new thing here today!
Some Final words
If this story was helpful and you wish to show a little support, you could:
- Clap 50 times for this story
- Leave a comment telling me what you think
- Highlight the parts in this story that resonate with you
- Sign up for a Medium membership using my link ($5/month to read unlimited Medium stories)
These actions really really help me out, and are much appreciated!
My Ebooks: https://zlliu.co/ebooks
My LinkedIn: https://www.linkedin.com/in/zlliu/
