What is requirements.txt & it’s advantage
When a person is working on a python project and added some python libraries and then shares the project in the team or when a new team member joins the team, it’s always difficult to tell what libraries used in the project, either you have to specify in a text document or write somewhere.
but during the time, if you upgrade the libraries, you have to manually update the version details as well in the documentation, so the next person who picks up the project can install specific version of the libraries.
so requirements.txt file will automatically updates the package details with version when someone installs a new package, also python provides a way to install those version packages directly from the requirements.txt file, so you need not to manually install each & every package.
How to create requirements.txt
Navigate to your project directory in terminal & type
pip freeze > requirements.txt
This will keep all the installed package names with the versions installed for the current project.
Important note –
Sometimes handcraft or relook into the requirements.txt is a good idea after doing a pip freeze
Scenario – Sometimes when developers try with different libraries for experiment, but they tend to forget to remove from the project, but those libraries left inside the requirements.txt file, so when other developer use the requirements.txt to install the packages might not have idea where it’s used inside the project.
Another scenario – few libraries when we install, requirements.txt add extra dependencies specific to the libraries, but when we upgrade the libraries during course of time, the dependencies might not be required, but doesn’t remove from the requirements.txt
so we need to remove those specific unnecessary dependencies manually so they don’t create any issues when we install using requirements.txt
python_package1=1.0 futures==1.0 # specific to python_package1
When we upgrade the package, the futures dependency is not needed, but stays in the file, so we need to remove manually.
python_package1=2.0 futures==1.0 # not required for the python_package1 now
How to install from requirements.txt
Navigate to the project folder where you want to install the packages from the requirements.txt file
Note – make sure requirements.txt file is present under the parent project directory
Open the terminal from the current directory and type
pip install -r requirements.txt
This will install all the libraries under the project directory [recommended to use the virtual environment]
You can use below command to know more install options –
pip install --help
Install Options: -r, --requirement <file> Install from the given requirements file. This option can be used multiple times. -c, --constraint <file> Constrain versions using the given constraints file. This option can be used multiple times. --no-deps Don't install package dependencies. --pre Include pre-release and development versions. By default, pip only finds stable versions. -e, --editable <path/url> Install a project in editable mode (i.e. setuptools "develop mode") from a local project path or a VCS url. -t, --target <dir> Install packages into <dir>. By default this will not replace existing files/folders in <dir>. Use --upgrade to replace existing packages in <dir> with new versions. --platform <platform> Only use wheels compatible with <platform>. Defaults to the platform of the running system.
If you are using pycharm editor, then you can directly create or install the packages from the requirements.txt
Open the project from the editor > Tools > Sync Python Requirements… > Ok
Will generate requirements.txt under the project directory
This file can be shipped with the project, so other person can install the libraries
To install from requirements.txt
Open the requirements.txt file using pycharm editor
You will get a notification to install the libraries on top of the file, click on the link to install!
Hope this helps!