DEV Community

Ilya
Ilya

Posted on

Automatically manage Python dependencies with requirements.txt

One of the most important steps in Python development that everybody go through is dependencies management. I remember, when I only started to make some projects for myself I didn’t even know that it’s possible in any automatic way. I thought that you should remember them or look at the code to find which modules are imported and install it manually. Obviously I was wrong.

Dependencies management options

There is basically two well-known ways to handle dependencies of your project:

  • Using requirements.txt file.
  • Using dependency managers like pipenv or poetry.

Dependencies with requirements.txt

One of the oldest, most popular and native option to manage dependencies of a Python project is requirements.txt. It imply storing all the packages and versions in a file with corresponding name. The format is simple:

appdata==2.1.1
celery==5.0.5
numpy==1.22.1
Enter fullscreen mode Exit fullscreen mode

And to install all required packages to your Python interpreter just use the command:

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

That’s it! Without need in some extra packages. Everything is native to Pip. It doesn’t require handling it manually, by the way. Just use:

pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

and it will automatically generate requirements.txt for you. But there is one drawback in this approach: it puts ALL the installed packages into the file. After that the requirements.txt may become really big and it becomes hard to understand what a project really use.

One of the ways to handle it is pipreqs library. It searches through your code and find all the dependencies that are related to the project. Just type in your terminal:

pipreqs
Enter fullscreen mode Exit fullscreen mode

and you’re done. But that’s not the only option to handle it. You should always remember to use those commands before push, and sometimes it become a headache.

Automatic requirements.txt update

It becomes clear in the end that it’d be great to have some tool to update requirements.txt automatically and stay always updated without extra steps. For that purpose the library called to-requirements.txt were developed. Basically it integrates auto-updating scripts into Pip. The major advantage is that is does not imply any changes in process for development. You does not need to use some extra steps or other commands to install dependencies. Just use Pip as you usually do.

To install this package use:

pip install to-requirement.txt
Enter fullscreen mode Exit fullscreen mode

If you read the command above you can clearly understand what package do. It installs to requirements.txt. Great wordplay as for me.

And to setup your Pip scripts use:

requirements-txt setup
Enter fullscreen mode Exit fullscreen mode

It may require admin rights, because your pip scripts might require root access rights. After that command your pip scripts are ready and you can install your dependencies in straightforward way and keep your requirements.txt file updated.

To disable to-requirements.txt just uninstall the package or type:

requirements-txt config disable 1
Enter fullscreen mode Exit fullscreen mode

You can find more configuration options here:
https://requirements-txt.readthedocs.io/en/latest/

Conclusion

In this tutorial I described how you can manage your dependencies and how to do it in the easier way with to-requirements.txt. I hope it will help you with your projects. Have a nice day!

Top comments (0)