A virtual environment is a folder that holds its own copy of Python and its own installed packages. Every project gets one, so installing a library for one project can never break another. It is the habit that separates tidy Python projects from messy ones, and it takes two commands to start.
Why you need one
Suppose project A needs version 1 of a library and project B needs version 2. If you install packages globally, only one version can exist at a time. With a virtual environment per project, each project has the version it needs. It also keeps your computer's own Python clean: on recent Debian and Ubuntu systems, installing packages globally with pip is blocked with an "externally-managed-environment" error, and the fix is a virtual environment.
Create one
Go to your project folder and run:
python3 -m venv .venv
On Windows, use python -m venv .venv. This creates a folder called .venv inside your project. The name is a convention; many tools look for .venv automatically.
Activate it
Activating tells your terminal to use the Python and pip inside that folder:
| System | Command |
|---|---|
| macOS and Linux | source .venv/bin/activate |
| Windows (Command Prompt) | .venv\Scripts\activate |
| Windows (PowerShell) | .venv\Scripts\Activate.ps1 |
Your prompt will now start with (.venv). If PowerShell refuses to run the script, run Set-ExecutionPolicy -Scope CurrentUser RemoteSigned once and try again.
Install packages with pip
With the environment active, pip installs into it, not into the system:
pip install requests pip list
To check which Python you are using, run python -c "import sys; print(sys.prefix)". The path should point into your .venv folder.
Save and restore your dependencies
Record the exact packages your project uses in a file called requirements.txt:
pip freeze > requirements.txt
Anyone, including you on a new computer, can rebuild the same environment:
python3 -m venv .venv source .venv/bin/activate pip install -r requirements.txt
Commit requirements.txt to Git, but not the .venv folder. Add this line to your .gitignore:
.venv/
Leave the environment
deactivate
You can delete the whole environment at any time by deleting the .venv folder; nothing else is affected, and you can recreate it from requirements.txt.
Common mistakes
- Installing with pip before activating. The package goes into the system Python. Check for
(.venv)in your prompt first. - Moving or renaming the project folder. Virtual environments contain absolute paths and can break. Delete and recreate it.
- Committing .venv to Git. It is large and specific to one machine. Commit
requirements.txtinstead. - Forgetting to select it in your editor. In VS Code, choose the interpreter inside
.venvso the editor finds your installed packages.
New to Python? Start with installing Python and running your first program. Once your environment is ready, learn the built-in data structures or see how to call an API with requests.
Covering financial markets, commodity rates, and practical technology insights for India. Researched and fact-checked in accordance with our Editorial Policy.
