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.

Recent Posts

  1. Automating Release Generation with GitHub Actions
  2. WP CLI Commands to Bulk Delete Entries in WordPress Database
  3. Split a Single CSV File into Multiple Files Using the Split Command - Bash
  4. Migrating code repo from BitBucket to GitHub
  5. Streamlining Development - Our Journey with Git, Bitbucket, and Jira

Your Questions / Comments

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