How to Create a Virtual Environment in Python

Short answer
Run 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.

Quick facts
Createpython -m venv .venv
Activate (macOS/Linux)source .venv/bin/activate
Activate (Windows).venv\Scripts\activate
Deactivatedeactivate
Modulevenv (standard library since Python 3.3)

Create and activate a virtual environment (step by step)

  1. Open a terminal and move into your project folder.
  2. Create the environment. The last word is the folder name; .venv is a common choice.
  3. Activate it. Your prompt changes to show (.venv).
  4. Install packages with pip. They go into .venv only.
  5. 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 / shellActivate commandCreate command
macOS, Linux (bash, zsh)source .venv/bin/activatepython3 -m venv .venv
Windows Command Prompt.venv\Scripts\activatepython -m venv .venv
Windows PowerShell.venv\Scripts\Activate.ps1python -m venv .venv
Linux fish shellsource .venv/bin/activate.fishpython3 -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:

Python
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.

Python
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.

Detect a virtual environment from Python
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.

Confirm the venv module exists
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.

Check the Python version of the interpreter
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
The package is installed into the global Python, not the project's environment.

✓ Right

python -m venv .venv
source .venv/bin/activate
python -m pip install requests

Activate 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
No visible error, but the environment is not active afterwards.

✓ Right

source .venv/bin/activate

On 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
cannot be loaded because running scripts is disabled on this system

✓ Right

Set-ExecutionPolicy -Scope CurrentUser RemoteSigned
.venv\Scripts\Activate.ps1

Allow 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 →