Sarathlal N

Manage package dependencies in Python project

If we share a python project with others, use a build system or restore an environment, we need to specify the external packages that the project requires.

The recommended approach is to use a requirements.txt file that contains a list of commands for pip that installs the required versions of dependent packages.

Note: My assumption is that you are using a virtual environment to set up your project. Developing python projects in a virtual environment is the recommended method.

There are multiple methods to generate requirements.txt.

1) pip freeze command

pip freeze > requirements.txt

This command records an environment’s current package list into requirements.txt. We can ship this file with our project & later every one can easily install dependent packages.

The drawback of this method is freeze command records all packages in the current environment to requirements.txt. There is a chance for unused packages.

2) Using pipreqs package

pip install pipreqs

If we are in the same path with our file, we can just write ./. Otherwise we need to give path of our file.

pipreqs ./

or

pipreqs /home/project/location

That will create a requirements.txt file for our project. The important advantage is pipreqs package only records packages that are imported in project files.

Looking for a skilled WordPress/WooCommerce developer? I'm currently available for freelance, contract, or full-time remote opportunities! Let's create something amazing together. Send me a quick message, and I'll respond within 24 hours!

Recent Posts

  1. Understanding the Singleton Pattern and Using Traits to Achieve Singleton in WordPress Plugin Development
  2. REST API Methods Explained with Best Practices for Building Clean and Secure APIs
  3. My 28-Day Plan to Master Modern WordPress Development Using AI Tools
  4. Scaling WordPress - How Custom Database Tables Solve the Post Meta Bottleneck
  5. WordPress Transients Explained - A Developer's Guide to Site Performance

Your Questions / Comments

If you found this article interesting, found errors, or just want to discuss about it, please get in touch.