Managing Python Dependencies with requirements.txt on macOS Tahoe
A requirements.txt file is a simple text file that lists the Python packages required for a project. It is the standard way to specify and share dependencies, ensuring that anyone can easily recreate the same environment.
Creating a requirements.txt File
You can manually create a requirements.txt file or, more commonly, generate it from your existing environment using pip.
1. Manually Creating
Open a text editor and list each package, one per line. You can optionally specify a version number:
requests
pandas==1.5.0
numpy>=1.20
2. Generating from Your Environment
If you already have the packages installed in your environment (e.g., a virtual environment), you can automatically generate the file:
pip freeze > requirements.txt
This command lists all installed packages and their versions and redirects the output to requirements.txt.
Installing from a requirements.txt File
To install all the packages listed in a requirements.txt file:
pip install -r requirements.txt
pip will read the file and install each package with the specified versions.
Updating Packages
To upgrade all packages listed in requirements.txt to the latest version:
pip install --upgrade -r requirements.txt
Related Guides
- Creating Python Virtual Environments with venv on macOS Tahoe
- Installing Python Packages with Pip Without Sudo on macOS Tahoe