Generate Requirement File for Python App for Meilix-Generator
Meilix-Generator is based upon Flask (a Python framework) which has several dependencies to fulfill before actually running the app properly. This article will guide you through the way I used it to automatically generate the requirement file for Meilix Generator app so that one doesn’t have to manually type all the requirements.
An app powered by Python always has several dependencies to fulfill to run the app successfully. The app root directory contains a file named as requirements.txt which contains the name of the dependency and their version. There are features ways to generate the requirement file for an app but the one which I will demonstrate is the best one. So I used this idea to generate the requirement file for webapp Meilix Generator.
Ways to get the requirement.txt
The internet has a featured way through which one has just to run a command to get a list of all the different dependencies within an app.
pip freeze > requirements.txt
This way will generate a bunch of dependencies that we not even required.
Why do we really require to generate a requirement file?
Yes, one may even ask that we can even write the dependency in the requirements.txt file. Why do we need a command to generate it?
Since because it will take care of two important things:
1. It will ensure that all the dependencies have been included, from user input one may forget to find some of the dependency and to include that.
- It will also take care of the Python Package Version Pinning which is really important. People use to version pinning for Python requirements as “>=” style. It’s important to follow “==” style because If we want to install the program in one year in the future, the required packages should be pinned to assure that the API changes in the installed packages do not break the program. Please read here for more info.
The way mentioned below will ensure to provide both these features.
How I generated it for Meilix Generator?
Meilix Generator run on Flask that require a requirement.txt file to fulfill the dependencies. Let’s get straight to the way to generate it for the project.
First we will simply create a requirements.in file in which we will simply mention all the dependencies in a simple way:
Flask gunicorn Werkzeug
Now we will use a command to latest packages:
pip install --upgrade -r requirements.in
#Note that if you would like to change the requirements, please edit the requirements.in file and run this command to update the dependencies
Then type this command to generate the requirements.txt file from requirements.in
pip-compile --output-file requirements.txt requirements.in
#fix the versions that definitely work for an eternity.
This will generate a file something as:
click==6.7 # via flask Flask==0.12.2 gunicorn==19.7.1 itsdangerous==0.24 # via flask Jinja2==2.9.6 # via flask MarkupSafe==1.0 # via jinja2 Werkzeug==0.12.2 # via flask
Now you generated a perfect requirements.txt file with all the dependencies satisfied with proper python package pinning.
The meilix-generator repo which uses this: