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
- Automating Code Linting with GitHub Actions for WordPress Plugins
- Comprehensive Guide to Linting PHP, JavaScript, and CSS in WordPress Plugins Using Composer
- The Ultimate Guide to Indexing in Database Design
- Understanding 'update_meta_cache' in WordPress - When to Use It, When Not to, and Its Impact on Database Queries
- A Guide to Configuring JavaScript and SCSS Paths in WordPress Plugins with @wordpress/scripts
Your Questions / Comments
If you found this article interesting, found errors, or just want to discuss about it, please get in touch.