Generate requirements.txt
Use pip freeze to generate a requirements.txt file:
pip freeze > requirements.txt
📓 Note:
pip freezeexports the packages installed in the current Python environment, including their version information. It does not inspect project source code to identify dependencies. Run it in the project's virtual environment to avoid including packages used by other projects. The default output omits some packaging tools; usepython -m pip freeze --allto include them.
The exported file records the environment's installation state. It is not a lockfile generated by a dependency solver and does not guarantee identical installation results across platforms. See the official pip freeze documentation.
- 梦想橡皮擦. (March 29, 2022). A detailed guide to Python requirements.txt [in Chinese]. 51CTO. Blog post.
Install packages from a requirements file
pip install -r requirements.txt installs Python packages listed in a text file named requirements.txt. Project maintainers typically use this file to specify the packages and versions required by their project.
pip install is pip's package installation command, and -r requirements.txt tells it to read the requirements from the specified file. The command installs the listed packages and their dependencies. Packages that are already installed and satisfy the requirements do not need to be installed again.
If downloads are slow, a regional package index mirror, such as the Alibaba Cloud mirror in China, may improve download speeds. Specify the index for an installation with:
pip install -i https://mirrors.aliyun.com/pypi/simple/ [package_name]
For an installable project with a pyproject.toml, pip can install the project and its dependencies from the project root. See why pip install -e src can point to the wrong directory.
Virtual environments with venv
Each project can install its dependencies in its own virtual environment. Activating the environment makes the shell find its python first, so python -m pip install -r requirements.txt uses that interpreter to install the packages.
Stephen Sanwo's tutorial uses two projects that need different versions of the same package to explain why each project gets its own environment. It also shows how to reinstall the dependencies in a fresh environment from requirements.txt.1
The following steps show how to use a virtual environment on Windows.
Create an environment:
python -m venv myenv
Here, myenv is the environment directory name; you can choose a different name.
Activate the environment in cmd.exe:
myenv\Scripts\activate.bat
The environment name appears in the command prompt after activation.
Change to the project directory and install its dependencies:
cd project_directory
# Replace project_directory with the path to your project.
# Install the project's dependencies.
pip install -r requirements.txt
# requirements.txt lists the required packages and versions.
This downloads and installs the required packages and their dependencies. Once installation is complete, the project is ready to run.
To leave the environment:
deactivate
# Return to the environment used before activation.
The workflow on Unix-like systems, such as macOS and Linux, is similar, but the activation command differs:
# Activate the virtual environment.
source myenv/bin/activate
# Run deactivate to leave the environment.
How do I install from requirements.txt without activating the venv?
If the environment is in the project's .venv directory, you can call its interpreter directly. In each command below, -m pip runs pip with the interpreter you selected. This also works in automation scripts.2
| Platform | Run from the project directory |
|---|---|
| macOS or Linux | .venv/bin/python -m pip install -r requirements.txt |
| Windows | .\.venv\Scripts\python.exe -m pip install -r requirements.txt |
Locate the current virtual environment
After activation, inspect the current Python interpreter path and environment directory:
python -c "import sys; print(sys.executable); print(sys.prefix)"
The first line shows the path to the running interpreter. The second shows its environment directory.
For example, an environment named .venv typically uses /path/to/project/.venv/bin/python on Linux or macOS and C:\path\to\project\.venv\Scripts\python.exe on Windows. The directory name depends on the path chosen when creating the environment; it does not have to be .venv.
Why does VS Code raise ModuleNotFoundError after I installed the package?
Run this before the failing import in the script, or in a cell in the notebook that raised the error:
import sys
print(sys.executable)
Compare the printed path with the interpreter you used to install the dependencies.2 If you installed the package in the project's .venv but the script uses another Python, choose the project interpreter in VS Code's Python: Select Interpreter. For a notebook, choose the corresponding environment in the kernel picker at the top right. Run the code again.3 If the printed path already points to the interpreter you intend to use, run that interpreter with -m pip install -r requirements.txt as shown above.
How can I list the venvs I created?
python -m venv creates an environment in the directory you specify. It has no command to list every environment on the machine. Each environment has a pyvenv.cfg file at its root. On Linux or macOS, open the directory where you keep your projects and run find . -type f -name pyvenv.cfg. The parent directory of each match is an environment. The search covers only the directory tree you started in.
Another freeCodeCamp tutorial shows two listing commands.4 lsvirtualenv lists environments that virtualenvwrapper manages under WORKON_HOME. The Conda command conda info --envs lists environments known to Conda.5
How do I remove an environment I no longer use?
For an environment created with python -m venv, check that the directory containing pyvenv.cfg is the environment you intend to remove. Keep the requirements file you need to rebuild it outside that directory. If the environment is active in your shell, run deactivate, then delete the environment directory.2
For example, once you confirm that the project’s .venv is the target, run rm -r .venv on macOS/Linux or Remove-Item -Recurse -Force .\.venv in Windows PowerShell. This also removes packages installed inside that directory, so you can skip uninstalling them individually.6 For an environment managed by virtualenvwrapper, deactivate it first, then use rmvirtualenv ENVNAME to remove it from WORKON_HOME.7
For a Poetry project, run poetry env list from the project directory to see that project's environments. Use poetry env remove <name> to remove one from the list. For a Pipenv project, run pipenv --rm from the project directory to remove its environment.2
How do I remove all installed dependencies from a venv?
To return .venv to its initial state, recreate it in place. First check that your source files and requirements.txt are outside .venv, because --clear deletes everything in the target directory. If the environment is active, run deactivate. Then, from the project root, use the base Python that created the environment to run python -m venv --clear .venv. The fresh environment includes pip by default. Python venv documentation
One reason to keep the directory is a custom script that runs after activation. As niceguydave explained, “I may have made changes to e.g. postactivate which will remain.”8
To keep other files in the environment directory, use pip to uninstall packages in bulk. On macOS or Linux, run the first command below from the project root, review the generated list, then run the second command. The --local option lists packages installed in this environment, and --exclude pip keeps pip itself. This adapts Harshad Kavathiya’s list-based approach. A separate uninstall list preserves the project's requirements.txt.9 pip list documentation · pip uninstall documentation
.venv/bin/python -m pip list --local --format=freeze --exclude pip > venv-uninstall.txt
.venv/bin/python -m pip uninstall -r venv-uninstall.txt
Move or rename a virtual environment
Copying an existing virtual environment may seem convenient for projects with many dependencies. However, scripts inside the environment can contain absolute paths to the Python interpreter. Moving or renaming the directory can invalidate these paths, and copying an environment between operating systems can also introduce binary compatibility problems.
To migrate an environment, export its dependencies and recreate it at the destination. Run this command in the original environment:
python -m pip freeze > requirements.txt
Copy the requirements file to the destination machine, then create an environment using a Python version compatible with the project:
python -m venv .venv
Activate the new environment using the appropriate command below, then run python -m pip install -r requirements.txt. Recreating the environment is also the recommended approach when renaming it. See the official Python venv documentation.
Activate an environment in different shells
The activation script adjusts the current shell's PATH so that commands such as python resolve to programs inside the virtual environment first. Assuming the environment directory is .venv:
| Operating system and shell | Activation command |
|---|---|
| Bash/Zsh on Linux, macOS, or WSL | source .venv/bin/activate |
| Windows Git Bash, for an environment created by Windows Python | source .venv/Scripts/activate |
| Windows cmd.exe | .venv\Scripts\activate.bat |
| Windows PowerShell | .\.venv\Scripts\Activate.ps1 |
Environments created inside WSL use the Linux directory structure and should not be mixed with native Windows environments. Run deactivate to leave the environment.
- czhust. (May 30, 2023). Installing requirements.txt with pip [in Chinese]. Zhihu. Blog post.
Use virtual environments in VS Code
Global environments: By default, each installed Python interpreter runs in its own global environment. For example, running python, python3, or py in a new terminal, depending on the installation, uses that interpreter's global environment. Installing or uninstalling packages there affects the environment and the programs that use it.
💡 Tip: Create an environment specific to the workspace, such as a local virtual environment.
Local environments: Virtual environments and conda environments let you install packages for a workspace without affecting other environments or the system-wide Python installation.
Original English excerpt
Global environments: By default, any Python interpreter installed runs in its own global environment. For example, if you just run python, python3, or py at a new terminal (depending on how you installed Python), you're running in that interpreter's global environment. Any packages that you install or uninstall affect the global environment and all programs that you run within it.
💡 Tip: In Python, it is best practice to create a workspace-specific environment, for example, by using a local environment.
Local environments: There are two types of environments that you can create for your workspace: virtual and conda. These environments allow you to install packages without affecting other environments, isolating your workspace's package installations.
Python environment tools
The following table lists the tools used to manage Python packages and environments:
| Tool | Definition and purpose |
|---|---|
| pip | The Python package manager for installing and updating packages. It is included with Python 3.9 and later, except on distributions such as Debian-based systems, where you may need to install python3-pip. |
| venv | Creates separate package environments for projects. It is included with Python 3, although Debian-based systems may require the python3-venv package. |
| conda | Installed with Miniconda. Manages packages and virtual environments and is commonly used in data science projects. |
If a project needs a Python version you have yet to install, consider how you will get the interpreter. In a comparison of venv, Pipenv, Poetry, and Conda, happylittle describes a limit of venv when choosing the Python version (“不能指定系统不存在的python环境版本”): the interpreter must already be installed. The January 2025 update adds uv, which can download the required interpreter and install the project dependencies with uv sync.10
Create a virtual environment in the terminal
To create an environment manually, use the following commands, where .venv is the environment directory name:
# macOS/Linux
# On Debian-based systems, you may first need:
# sudo apt-get install python3-venv
python3 -m venv .venv
# Windows
# You can also use: py -3 -m venv .venv
python -m venv .venv
📓 Note: Virtual environments are usually created inside the current workspace directory. The VS Code Python extension gives priority to environments located there.
How the extension chooses an environment automatically
If an interpreter has not been specified, the Python extension automatically selects the interpreter with the highest version within the following priority order:
- Virtual environments located directly under the workspace folder.
- Environments associated with the workspace but stored elsewhere, such as Pipenv or Poetry environments outside the workspace folder.
- Globally installed interpreters, such as those under
/usr/local/binorC:\python38.
The selected interpreter may differ from the one that python resolves to in the terminal.
If VS Code does not locate the interpreter automatically, you can specify one manually.
- Python environments in VS Code. (December 6, 2023). VS Code documentation. Retrieved February 28, 2024, from Python environments.
Footnotes
-
Sanwo, S. (2022, April 11). How to Set Up a Virtual Environment in Python – And Why It's Useful. freeCodeCamp. ↩
-
van Baaren, E. (2020, December 13). Python venv: How To Create, Activate, Deactivate, And Delete. Python Land. See “Which Python is my venv using?”, “How a Python venv works”, and “Deleting a Python venv” in the November 7, 2026 update. ↩ ↩2 ↩3 ↩4
-
Microsoft. (n.d.). Manage Jupyter Kernels in VS Code. Visual Studio Code. ↩
-
freeCodeCamp. (2020, February 1). Python Virtual Environments Explained with Examples. ↩
-
virtualenvwrapper. (n.d.). Command Reference, “lsvirtualenv”. ↩
-
Anthony, T. (2012, June 12). Answer to “How do I remove/delete a virtualenv?”. Stack Overflow. ↩
-
Antoniazzi. (2016, October 24). Answer to “How do I remove/delete a virtualenv?”. Stack Overflow. virtualenvwrapper command reference. ↩
-
niceguydave. (2013, July 4). Comment on blueberryfields’s answer to “How do I remove all packages installed by pip?”. Stack Overflow. ↩
-
Kavathiya, H. (2016, November 12). Answer to “How do I remove all packages installed by pip?”. Stack Overflow. ↩
-
happylittle. (2025, January 30). [Python] 虚拟环境venv、pipenv、poetry、conda、uv如何选择?1分钟搞懂. Zhihu. See the first item under “venv” for the quotation and items 3–4 under “uv” for interpreter and dependency installation. ↩