How to Create a Virtual Environment in Python
python -m venv .venv in your project folder, then activate it with source .venv/bin/activate (macOS and Linux) or .venv\Scripts\activate (Windows). Packages you install afterwards stay inside that project.A virtual environment is a private folder holding its own Python interpreter and its own installed packages. Each project gets its own, so one project can use requests 2.31 while another uses 2.32 without conflicts, and your system Python stays clean.
The venv module is part of the standard library, so there is nothing to install. The commands below go in a terminal, not in a Python script. For the theory behind it, see pip and virtual environments.
| Create | python -m venv .venv |
|---|---|
| Activate (macOS/Linux) | source .venv/bin/activate |
| Activate (Windows) | .venv\Scripts\activate |
| Deactivate | deactivate |
| Module | venv (standard library since Python 3.3) |
Create and activate a virtual environment (step by step)
- Open a terminal and move into your project folder.
- Create the environment. The last word is the folder name;
.venvis a common choice. - Activate it. Your prompt changes to show
(.venv). - Install packages with pip. They go into
.venvonly. - Run your script with the environment active, see how to run a Python script.
# 1. go to your project
cd my_project
# 2. create the environment
python -m venv .venv
# 3. activate it
source .venv/bin/activate # macOS / Linux
.venv\Scripts\activate # Windows (Command Prompt)
.venv\Scripts\Activate.ps1 # Windows (PowerShell)
# 4. install packages
python -m pip install requests
# 5. leave the environment when finished
deactivate
Activation commands for each operating system
| System / shell | Activate command | Create command |
|---|---|---|
| macOS, Linux (bash, zsh) | source .venv/bin/activate | python3 -m venv .venv |
| Windows Command Prompt | .venv\Scripts\activate | python -m venv .venv |
| Windows PowerShell | .venv\Scripts\Activate.ps1 | python -m venv .venv |
| Linux fish shell | source .venv/bin/activate.fish | python3 -m venv .venv |
| Any system, no activation | .venv/bin/python script.py (Windows: .venv\Scripts\python.exe) | python -m venv .venv |
Save and restore a project's packages with requirements.txt
Never copy the .venv folder to share a project. Instead record the packages and let others rebuild the environment. Add .venv/ to your .gitignore file so it is not committed.
# save exact versions
python -m pip freeze > requirements.txt
# on another machine: create, activate, then
python -m pip install -r requirements.txt
Check from Python that the venv module is available
On some Linux distributions venv is split into a separate system package (for example python3-venv on Debian and Ubuntu). If python -m venv fails, confirm the module exists:
import venv
print(hasattr(venv, "EnvBuilder"))If that import fails, install your distribution's venv package, then try again.
How to tell whether a virtual environment is active
Inside an active environment sys.prefix (the environment folder) differs from sys.base_prefix (the Python it was created from). The helper below shows the comparison with fixed strings so the result is predictable; in a real script pass sys.prefix and sys.base_prefix.
def in_virtualenv(prefix, base_prefix):
return prefix != base_prefix
print(in_virtualenv("/home/asha/app/.venv", "/usr"))
print(in_virtualenv("/usr", "/usr"))Examples you can run
Press Run, then edit the code and run it again.
Detect a virtual environment from Python
In a real script call in_virtualenv(sys.prefix, sys.base_prefix); here fixed paths keep the output predictable.
def in_virtualenv(prefix, base_prefix):
return prefix != base_prefix
print(in_virtualenv("/home/asha/app/.venv", "/usr"))
print(in_virtualenv("/usr", "/usr"))Expected outputTrue
False
Confirm the venv module exists
Importing venv and checking for EnvBuilder proves your Python can create environments.
import venv
print(hasattr(venv, "EnvBuilder"))
print(callable(venv.create))Expected outputTrue
True
Check the Python version of the interpreter
A new environment uses the Python version that created it, so check before you create one.
import sys
version = sys.version_info
print(version >= (3, 3))
print(f"{version.major}.{version.minor}".startswith("3."))Expected outputTrue
True
Common mistakes
Forgetting to activate the environment
✗ Wrong
python -m venv .venv
python -m pip install requests✓ Right
python -m venv .venv
source .venv/bin/activate
python -m pip install requestsActivate first (source .venv/bin/activate), or call the environment's Python directly: .venv/bin/python -m pip install requests.
Running activate as a program instead of sourcing it
✗ Wrong
bash .venv/bin/activate✓ Right
source .venv/bin/activateOn macOS and Linux the script must be sourced into your current shell: source .venv/bin/activate.
PowerShell blocks the activation script
✗ Wrong
.venv\Scripts\Activate.ps1✓ Right
Set-ExecutionPolicy -Scope CurrentUser RemoteSigned
.venv\Scripts\Activate.ps1Allow scripts for your user once: Set-ExecutionPolicy -Scope CurrentUser RemoteSigned, or use Command Prompt and .venv\Scripts\activate.
How to Create a Virtual Environment in Python: questions people ask
How do I create a virtual environment in Python?
Run python -m venv .venv in your project folder, then activate it with source .venv/bin/activate on macOS and Linux or .venv\Scripts\activate on Windows.
How do I deactivate a virtual environment?
Type deactivate in the terminal. The (.venv) prefix disappears from your prompt and commands use your normal Python again.
Should I commit the virtual environment folder to Git?
No. Add it to .gitignore and commit a requirements.txt instead, so anyone can rebuild the environment with python -m pip install -r requirements.txt.
Keep learning
Step-by-step lessons:
Python pip and Virtual EnvironmentsPython Modules and ImportsWhat Is Python?
Related:
How to Install a Package with pip in PythonHow to Run a Python Script from the Command LinePython Module vs Package: What Is the Difference?
More in setup and tooling:
How to Install a Package with pip in PythonHow to Run a Python Script from the Command Line See all →