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.
Got a project in mind? Send me a quick message, and I'll get back to you within 24 hours!.
Recent Posts
- Disabling Payment Methods in WooCommerce Based on Conditions
- How to Update Product Quantity in WooCommerce Using Custom Code
- Dynamically Generating a Table of Contents in WordPress
- Direct Checkout in WooCommerce - Add Product to Cart from Checkout Page & Skip Shop, Product, and Cart Pages
- Understanding the Impact of git reset --hard Command
Your Questions / Comments
If you found this article interesting, found errors, or just want to discuss about it, please get in touch.