
Have you ever moved a Python project to another computer and discovered that some libraries were missing? Or shared your code with someone who could not run it because they did not have the same packages installed? A requirements.txt file helps solve this problem by recording the Python packages a project needs.
In this beginner-friendly guide, you will learn what requirements.txt is, how to create it with pip, how to install packages from it, and how to keep dependencies organised in a virtual environment. The commands are suitable for Windows, macOS and Linux, including the integrated terminal in Visual Studio Code.
What Is requirements.txt in Python?
requirements.txt is a plain-text file commonly used to list the Python packages required by a project. pip can read this file and install the listed requirements into the currently selected Python environment.
A simple file might look like this:
requests
numpy
pandas
You can also specify exact versions to make an environment more reproducible:
requests==2.32.3
numpy==1.26.4
pandas==2.2.3
The version numbers above are examples to demonstrate the format; use versions that are compatible with your project and Python version.
Why Should You Use requirements.txt?
- Recreate your setup: Install the project’s listed dependencies on another computer.
- Share projects more easily: Give teammates or learners a clear list of required packages.
- Keep dependencies visible: Document the libraries your application uses.
- Support deployment: Many development and deployment workflows can install dependencies from a requirements file.
- Reduce setup mistakes: Avoid relying on someone to remember every package installed manually.
Before You Start: Use a Virtual Environment
It is best to create a separate virtual environment for each Python project. This keeps one project’s packages isolated from other projects and from the operating system’s Python installation.
Open your project folder in VS Code and open Terminal → New Terminal. Create a virtual environment with one of these commands.
Windows
py -m venv .venv
.\.venv\Scripts\Activate.ps1
If you use Windows Command Prompt instead of PowerShell, activate it with:
.venv\Scripts\activate.bat
macOS and Linux
python3 -m venv .venv
source .venv/bin/activate
After activation, the terminal prompt usually shows (.venv). In VS Code, select this environment using Python: Select Interpreter from the Command Palette.
Need the full walkthrough? Read How to Create a Python Virtual Environment in VS Code (Windows, macOS & Linux).
Method 1: Create requirements.txt Using pip freeze
If you have already installed the packages your project needs in its activated virtual environment, you can create a requirements file automatically.
Step 1: Activate the project’s virtual environment.
Step 2: Run this command from the project directory:
python -m pip freeze > requirements.txt
pip writes the installed distributions and their versions to a file named requirements.txt. Open the file in VS Code to review its contents.
Important: pip freeze reports packages installed in the active environment. If that environment contains unrelated tools or packages, they may also appear in the output. This is why a clean, project-specific virtual environment is recommended.
Method 2: Create requirements.txt Manually
You do not have to use pip freeze. You can create the file yourself when you know which direct dependencies your project needs.
- Open your project folder in VS Code.
- Create a new file named
requirements.txtin the project root. - Add one package requirement per line.
- Optionally specify versions using version operators.
- Save the file.
For example:
requests>=2.31
flask>=3.0
python-dotenv>=1.0
This example uses minimum-version constraints rather than exact pins. Choose constraints that you have tested with your application. Package names in requirement files are distribution names; they are not always identical to the names used in Python import statements.
Understanding Version Specifiers
| Specifier | Meaning | Example |
|---|---|---|
== | Match an exact version | requests==2.32.3 |
>= | Use this version or newer | requests>=2.31 |
< | Use a version lower than the specified version | requests<3 |
!= | Exclude a particular version | requests!=2.32.0 |
~= | Compatible release constraint | requests~=2.32 |
For more advanced version rules, refer to the Python Packaging User Guide’s documentation on dependency specifiers.
How to Install Packages From requirements.txt
When you receive a Python project that contains a requirements.txt file, you can install its dependencies with one command.
- Open the project folder in your terminal or VS Code.
- Create and activate a virtual environment if one does not already exist.
- Make sure the terminal is using the intended Python interpreter.
- Run the installation command below.
python -m pip install -r requirements.txt
pip reads the file and installs the listed requirements and their dependencies. On Windows, if your Python command is the launcher, you can use py -m pip install -r requirements.txt. On macOS or Linux, use python3 -m pip install -r requirements.txt when appropriate.
Complete Practical Example: Share a Python Project
Let’s create a small project and prepare its dependencies so another person can set it up.
Step 1: Create a project folder
mkdir pip-demo
cd pip-demo
Step 2: Create and activate a virtual environment
On Windows PowerShell:
py -m venv .venv
.\.venv\Scripts\Activate.ps1
On macOS or Linux:
python3 -m venv .venv
source .venv/bin/activate
Step 3: Install a package
python -m pip install requests
Step 4: Create a Python file
Create main.py with this simple example:
import requests
response = requests.get("https://example.com", timeout=10)
print("Status code:", response.status_code)
print("Request completed!")
Run the program:
python main.py
The request should print an HTTP status code if the computer can reach the website. Network access is required for this example.
Step 5: Generate requirements.txt
python -m pip freeze > requirements.txt
Open the generated file and check that the installed dependency is listed.
Step 6: Recreate the project environment
On another computer, copy the project files, create a new virtual environment, activate it, and run:
python -m pip install -r requirements.txt
The new environment will install the requirements recorded in the file, subject to package availability and compatibility with that computer’s Python version and platform.
How to Update requirements.txt
Whenever you add or remove dependencies, review the requirements file so it reflects the setup you intend to share.
After installing a new package, you can regenerate the file with:
python -m pip freeze > requirements.txt
To remove an unneeded package, uninstall it first:
python -m pip uninstall package_name
Then regenerate and review the file. Be aware that pip freeze records the environment’s installed distributions, including transitive dependencies, not just the packages your code imports directly.
requirements.txt vs pyproject.toml
Both files can be part of Python dependency management, but they serve different roles in common workflows.
| Feature | requirements.txt | pyproject.toml |
|---|---|---|
| Typical purpose | List requirements for pip to install | Project configuration and packaging metadata; can also declare dependencies |
| Format | pip requirement lines | TOML configuration |
| Common use | Reproducing an environment or deploying an app | Configuring a package or modern Python project |
| Package metadata | Not a complete project metadata format | Supports standard project metadata through the [project] table |
For a beginner project, requirements.txt is a straightforward way to share pip dependencies. As projects grow, learn about pyproject.toml and the packaging tools that use it.
Common requirements.txt Errors and Fixes
| Problem | Possible solution |
|---|---|
Could not open requirements file | Check the filename and current working directory. Run the command from the folder containing the file or provide its correct path. |
No matching distribution found | Check the package name, version constraint, Python version and operating-system compatibility. |
| Packages install but imports fail | Confirm that pip installed into the same environment used to run the script. Use python -m pip show package_name and check the VS Code interpreter. |
| Unexpected packages appear in the file | Regenerate the file from a clean project virtual environment rather than a global or shared environment. |
| Installation fails with a permission error | Activate a project virtual environment and install dependencies there instead of forcing a global installation. |
| Different computers get different results | Check Python version, platform-specific dependencies and version constraints. Exact pins can improve repeatability but do not guarantee identical environments across every platform. |
Best Practices for Using requirements.txt
- Use a separate virtual environment for each project.
- Keep
requirements.txtin the project root so it is easy to find. - Review the file before committing it to version control.
- Choose version constraints deliberately and test dependency upgrades.
- Do not include secrets, passwords or private access tokens in a requirements file.
- Add your virtual environment directory, such as
.venv/, to.gitignore; share the dependency file rather than the environment folder. - Document the Python version your project expects, because requirements alone do not fully describe the runtime environment.
Frequently Asked Questions
What is the purpose of requirements.txt in Python?
It lists Python package requirements so pip can install the dependencies needed by a project. It makes project setup easier to share and repeat.
How do I generate requirements.txt?
Activate the project’s virtual environment, open a terminal in the project folder, and run python -m pip freeze > requirements.txt.
How do I install all packages from requirements.txt?
Activate the correct environment and run python -m pip install -r requirements.txt.
Should I include virtual environment folders in Git?
No. Normally, add .venv/ to .gitignore and commit the requirements file instead. Each developer can create their own environment and install the listed packages.
Does pip freeze include all dependencies?
pip freeze lists the installed distributions in the active environment, including dependencies installed alongside your packages. It does not determine which packages your source code directly imports.
Can I name the file something other than requirements.txt?
Yes. The file can have another name or path. Pass that path to pip with -r, for example python -m pip install -r config/dev-requirements.txt.
Is requirements.txt enough to recreate an identical Python environment?
It records package requirements, but exact reproduction can also depend on the Python version, operating system, CPU architecture, external system libraries and package availability. For more controlled workflows, document those details as well.
Conclusion
A requirements.txt file is a simple and useful way to document and share the Python packages a project needs. You can create one manually or generate it with pip freeze, then install the listed dependencies on another machine with pip install -r requirements.txt.
For a reliable workflow, keep dependencies inside a project-specific virtual environment, review the requirements file, and make sure VS Code uses the same interpreter. This makes your Python projects easier to share, maintain and set up on Windows, macOS and Linux.
Official References
- pip documentation: Requirements File Format
- pip documentation: pip freeze
- pip documentation: pip install
- Python Packaging User Guide: Installing Packages Using pip and Virtual Environments
- Python Packaging User Guide: Writing your pyproject.toml
