Python Virtual Environments Guide
Short answer: a virtual environment is an isolated folder that holds its own Python packages for one project. Create one with python -m venv .venv, activate it, and install packages with pip. This stops different projects from clashing over package versions.
Why you need virtual environments
Suppose project A needs Django 4.2 and project B needs Django 5.0. If you install packages globally, you can have only one version at a time. A virtual environment gives each project its own set of packages, and keeps your system Python clean.
Create a virtual environment
cd my-project
python -m venv .venv
On some systems the command is python3. This creates a .venv folder with a copy (or link) of the interpreter and an empty site-packages directory. The name .venv is a common convention.
Activate it
| System | Command |
|---|---|
| macOS / Linux | source .venv/bin/activate |
| Windows (Command Prompt) | .venv\Scripts\activate.bat |
| Windows (PowerShell) | .venv\Scripts\Activate.ps1 |
Your prompt shows (.venv) when active. If PowerShell blocks the script, you may need to allow local scripts for your user with Set-ExecutionPolicy -Scope CurrentUser RemoteSigned.
Leave the environment with deactivate.
Install and manage packages
pip install requests
pip install "django>=4.2,<5"
pip list
pip uninstall requests
Check you are using the environment's Python:
which python # macOS/Linux
where python # Windows
python -c "import sys; print(sys.prefix)"
Save and share dependencies
pip freeze > requirements.txt # save installed versions
pip install -r requirements.txt # recreate on another machine
Commit requirements.txt but not the .venv folder. Add this to .gitignore:
.venv/
__pycache__/
.env
Using different Python versions
python3.12 -m venv .venv
Install multiple Python versions with tools like pyenv (macOS/Linux) or the Python launcher py -3.12 on Windows. A virtual environment uses the Python version that created it.
Alternatives and tools
| Tool | What it adds |
|---|---|
venv | Built in, simple and enough for most projects |
virtualenv | Older third-party tool with extra features |
| pipenv, Poetry, uv | Dependency locking, project management and faster installs |
| conda | Environments and non-Python packages, popular in data science |
Editors and IDEs
In VS Code, open the Command Palette and choose Python: Select Interpreter, then pick the one inside .venv. See VS Code settings for more setup. Most IDEs detect a .venv folder automatically.
Common problems
- "pip installs to the wrong place": the environment is not activated. Use
python -m pip install ...to be sure pip belongs to the active Python. - "command not found: python": try
python3. - Moved or renamed project folder: virtual environments contain absolute paths; recreate it rather than copying it.
- Missing packages in a notebook or editor: select the right interpreter or kernel.
- Corrupt environment: delete
.venvand recreate fromrequirements.txt.
Best practices
- Create one environment per project.
- Pin versions for reproducible builds.
- Never install project packages with
sudo pipinto the system Python. - Keep secrets in environment variables, not in code or in
requirements.txt. - Rebuild the environment in CI from
requirements.txtso tests match production.
Once your environment works, practise with list comprehensions and exception handling. Background: what is Python.
FAQs
Do I need a virtual environment for every project?
It is strongly recommended. It avoids version conflicts and makes your project reproducible.
What is the difference between venv and virtualenv?
venv ships with Python 3. virtualenv is an older external tool that supports more options and older Python versions.
Can I copy a virtual environment to another computer?
No. Recreate it there from requirements.txt.
How big is a virtual environment?
Small when empty, then it grows with the packages you install, from a few megabytes to hundreds for data-science libraries.
