📋 requirements.txt file for Python
Therequirements.txt file is the industry standard for specifying and managing dependencies in Python projects. This guide shows how to create, configure and use requirements.txt to ensure consistency between development and production environments.
What is a requirements.txt file?
Arequirements.txt file lists all external Python packages your project needs, with their specific versions. This allows:
- You and your team to maintain identical environments
- New collaborators to reproduce the environment exactly
- Deploy on Square Cloud to work with the correct dependencies
1
Activate the virtual environment
Working inside a virtual environment is essential to keep your project dependencies isolated from the global Python system. This avoids version conflicts and ensures reproducibility.Activate your virtual environment with one of the commands below, depending on your system:Windows:macOS/Linux:
2
List project dependencies
Option A: Create manually (Recommended)
Create arequirements.txt file in the root of your project and list all external packages with their versions:requirements.txt
Option B: Generate automatically with pip freeze
After installing dependencies in your virtual environment, you can automatically generate the file. There are two main ways:Option B1: Usepip freeze (includes everything)pipdeptree or pip freeze with filtering (Recommended)To capture ONLY your project’s dependencies (without auxiliary environment packages), use:pipdeptree:If you are working in a virtual environment (recommended),
pip freeze will work perfectly as the environment will be isolated and contain only your project’s dependencies.3
Review and validate dependencies
Open the
requirements.txt file in your text editor and:- Review the package list
- Remove unnecessary dependencies
- Update versions as needed
- Ensure you are using exact versions (with
==)
4
Install dependencies
To install all listed dependencies:This command guarantees that all collaborators and production environments use exactly the same versions.
5
Version and share
With
requirements.txt ready, you can:- Share it with your team
- Put it in version control (git)
- Ensure everyone works with the same versions
- Simplify deployment on Square Cloud
requirements.txt file is automatically detected and all dependencies are installed during build.💡 Best Practices and Tips:
- Keep updated: Review and update
requirements.txtregularly as you add new dependencies - Use exact versions: Always use
==instead of>,>=or no version to avoid surprises in production - Review pip freeze: If using
pip freeze, review the file before committing, as it may include unnecessary auxiliary packages - Versioning: Commit
requirements.txtto git to track dependency changes - Isolated environment: Maintain one virtual environment per project to avoid global conflicts
- Test locally: Before deploying, test the file with
pip install -r requirements.txtin a new environment
🚀 Next Steps
With yourrequirements.txt file configured:
- Commit to your Git repository.
- Host your project on Square Cloud.
- Configure automatic deploy via GitHub.
- Welcome to professional Python application hosting!
requirements.txt file is the foundation for any successful Python application, ensuring your application works perfectly on Square Cloud.
