How to Create a Python Virtual Environment

You’ll create a Python virtual environment and use it to install project dependencies without affecting other projects.

What you’ll build or solve

You’ll create a virtual environment folder, commonly named .venv, activate it, and install packages inside it. Done looks like your terminal uses the venv’s Python instead of your system Python.

When this approach works best

This approach works best when you:

  • Work on more than one Python project and each needs different package versions.
  • Follow a tutorial that requires installing packages and you don’t want to pollute your system Python.
  • Run scripts in a team where everyone should use the same dependency set.

Skip virtual environments when:

  • You run Python only inside containers and never install packages directly on your machine.

Prerequisites

  • Python installed
  • A terminal
  • Basic terminal familiarity

Step-by-step instructions

1) Go to your project folder

Create a folder for your project or use an existing one, then move into it.

macOS/Linux:

mkdir my_project
cd my_project

Windows:

mkdirmy_project
cdmy_project

What to look for

If you’re not sure where you are, run:

  • macOS/Linux:
pwd
  • Windows (PowerShell):
Get-Location

2) Create the virtual environment

Create a venv named .venv in the current folder.

macOS/Linux:

python3-m venv .venv

Windows:

py-mvenv .venv

This creates a folder named .venv/ with its own Python interpreter and pip.

What to look for

If you see No module named venv, your Python install is missing venv support. See Troubleshooting.


3) Activate the virtual environment

Activation updates your terminal session so python and pip point to the venv.

macOS/Linux:

source .venv/bin/activate

Windows (PowerShell):

.\.venv\Scripts\Activate.ps1

What to look for

  • Your prompt often shows (.venv) after activation.
  • If activation fails in PowerShell, you may need to adjust the execution policy. See Troubleshooting.

4) Confirm the venv interpreter is active

Verify that python points to the environment you just created.

macOS/Linux:

which python
python--version

Windows:

python-c"import sys; print(sys.executable)"
python--version

What to look for

  • On macOS/Linux, the path from which python should include .venv.
  • On Windows, sys.executable should include \.venv\.

5) Install packages in the venv, then deactivate when done

Use python -m pip so pip matches the active interpreter.

Install one package:

python-m pip install requests

Install from a requirements.txt file:

python-m pip install-r requirements.txt

When you’re finished working in that project:

deactivate

What to look for

  • If pip installs succeed but imports fail, the venv may not be active in your current terminal window.
  • After deactivate, your prompt usually loses the (.venv) marker.

Examples you can copy

Example 1: Create and activate a venv, then install one package

macOS/Linux:

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

Windows (PowerShell):

py-mvenv .venv
.\.venv\Scripts\Activate.ps1
python-mpipinstallrequests

Example 2: Install dependencies for a project using requirements.txt

Create requirements.txt:

requests

Install:

python-m pip install-r requirements.txt

Then verify in Python:

importrequests
print(requests.__version__)

Example 3: Run a script inside the venv

Create main.py:

importrequests

resp=requests.get("https://example.com",timeout=5)
print(resp.status_code)

Run:

python main.py

Common mistakes and how to fix them

Mistake 1: Installing packages without activating the venv

What you might do

Run pip install ... from a terminal where the venv is not active.

Why it breaks

Packages install into a different Python, so your project can’t import them.

Fix

Activate the venv, then install with the interpreter you’ll run:

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

On Windows:

.\.venv\Scripts\Activate.ps1
python-mpipinstallrequests

Mistake 2: Creating the venv with one Python, then running with another

What you might do

Create a venv, then run python3 main.py and get version or import issues.

Why it breaks

You bypassed the venv by calling a different interpreter directly.

Fix

Activate the venv and run python:

source .venv/bin/activate
python main.py

Troubleshooting

If you see: No module named venv

On Ubuntu or Debian, install venv support:

sudo apt install python3-venv

Then recreate the virtual environment.


If you see (PowerShell): running scripts is disabled on this system

Allow scripts for your user, then retry:

JavaScript

Set-ExecutionPolicy-ScopeCurrentUser-ExecutionPolicyRemoteSigned

If you see: command not found: python3 (macOS/Linux)

Install Python 3, then rerun the venv command.


If pip installs succeed but imports still fail

Confirm the interpreter points to .venv, then reinstall inside the active environment:

which python
python-m pip install-r requirements.txt

If your prompt doesn’t change after activation

The prompt change can be disabled. Verify with:

  • macOS/Linux:
which python
  • Windows:
python-c"import sys; print(sys.executable)"

Quick recap

  • Create a venv: python3 -m venv .venv or py -m venv .venv.
  • Activate it: source .venv/bin/activate or .\.venv\Scripts\Activate.ps1.
  • Confirm python points to .venv.
  • Install packages with python -m pip install ....
  • Deactivate with deactivate when you’re done.