The quickest way to generate your Requirements.txt file
Introduction
When you try to execute code from another developer, you may encounter errors due to missing dependencies.
Setting up your environment and installing these dependencies is the first step in ensuring smooth execution of the code. However, managing these dependencies can become cumbersome due to the multitude of libraries used and the need to specify specific versions to avoid compatibility issues.
In this article, we’ll explore a simple solution to this problem !
Generate a requirements file
What is a requirements file
One effective solution is to generate a file named requirements.txt
. This file is a text document that lists all external libraries used in a specific script or library, along with their respective versions.
Example :
pandas==1.5.2
uvicorn==0.20.0
fastapi==0.89.1
Once you have this file, you can effortlessly install all external libraries by executing the following command :
pip install -r requirements.txt
How to generate your requirements.txt file
To generate this file, we’ll utilize a library called pipreqs
. First, you need to install it by running:
pip install pipreqs
Upon installation, you may encounter a warning message similar to :
WARNING: The scripts jupyter-dejavu and jupyter-nbconvert are installed in ‘/Path/to/bin’ which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use — no-warn-script-location.
WARNING: The script pipreqs is installed in ‘/Path/to/bin’ which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use — no-warn-script-location.
To utilize pipreqs
, you'll need to either specify the path to the pipreqs
folder when invoking it, or add the pipreqs
path to your PATH environment variable.
Now, you can generate your requirements.txt
file by executing:
/Path/to/bin/pipreqs <path to your script/folder>
Congratulations! You’ve successfully created your requirements.txt
file ! 👏
Conclusion
With this file, you can ensure that all collaborators work with the same dependencies. This becomes especially beneficial when working within a virtual environment or when setting up a Docker image.