Tue Jul 25 2023
2 min readJust add direct dependencies to requirements.txt, automatically.
🙏Namaste Pythonistas! Let me ask you something — how do you usually maintain your requirements.txt
file? If you’re like me, you must be using good ol’ pip freeze > requirements.txt
command. Sure, it’s quick and easy, but have you ever found yourself looking at the file later and wondering what on earth cffi==1.15.1
refers to? Have you struggled to figure out if it was installed directly or as a dependency of another package? The ideal way would be to “hand write” it, but who has the time for it!?
Well, let me tell you about pip-chill
. It’s like a smarter version of pip freeze
. Instead of simply dumping all installed packages into the requirements file, it shows only the ones you directly installed.
To get started, just install pip-chill
using pip
itself:
pip install pip-chill
After that here is how I generate the requirements.txt
file now:
pip-chill --no-chill -v > requirements.txt
--no-chill
: does not include pip-chill
itself in the output-v
: outputs the indirect dependencies as well, but commented. Therefore it is easy to keep track of them.It generates the following for a basic Django project.
django==4.2.3
psycopg2-binary==2.9.6
requests==2.31.0
# asgiref==3.7.2 # Installed as dependency for django
# certifi==2023.5.7 # Installed as dependency for requests
# charset-normalizer==3.2.0 # Installed as dependency for requests
# idna==3.4 # Installed as dependency for requests
# sqlparse==0.4.4 # Installed as dependency for django
# typing-extensions==4.7.1 # Installed as dependency for asgiref
# urllib3==2.0.4 # Installed as dependency for requests
# urllib3==1.26.16 # Installed as dependency for botocore, requests
See how pretty it looks. It just works and solves my problem of properly maintaining requirements.txt
. Now, I can focus on coding and building cool stuff, leaving the hassle of maintaining the requirements file to pip-chill
.
So just, take a chill pill with pip-chill and chillax!
Happy coding 👋.
Disclaimer: Assistance from generative AI tools was used for this post.