How to Activate a Virtual Environment in Python

What you’ll build or solve

You’ll activate a Python virtual environment so your project uses the right interpreter and dependencies.

When this approach works best

Activating a virtual environment works best when you want to:

  • Keep each project’s packages separate, so installs in one project do not affect another.
  • Match the same dependency setup across a team or across machines.
  • Avoid permission issues that come from global installs on macOS and Linux.

This is a bad idea when you intentionally need a tool available for every project. For project code, use a venv.

Prerequisites

  • Python 3 installed
  • A project folder you can open in a terminal

If you do not have a virtual environment yet, create one first:

python-m venv .venv

Step-by-step instructions

1) Activate the environment

Activation depends on your shell and operating system.

Option A: macOS or Linux (bash/zsh)

source .venv/bin/activate

Option B: Windows (PowerShell)

.\.venv\Scripts\Activate.ps1

Option C: Windows (Command Prompt)

.\.venv\Scripts\activate.bat

What to look for: Your prompt usually changes to include the environment name, like (.venv).


2) Confirm activation worked

After activation, check that python points to the environment.

Option A: Check the resolved path

macOS/Linux:

which python

Windows:

wherepython

Option B: Check from inside Python

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

What to look for: The printed path should be inside your project’s .venv folder. If it points to a system location, activation did not apply to this terminal session.


Examples you can copy

Example 1: Create and activate in a new project (macOS/Linux)

mkdir my-project
cd my-project
python-m venv .venv
source .venv/bin/activate
python-c"import sys; print(sys.executable)"

Example 2: Create and activate in a new project (Windows PowerShell)

mkdirmy-project
cdmy-project
python-mvenv .venv
.\.venv\Scripts\Activate.ps1
python-c"import sys; print(sys.executable)"

Example 3: Fix “wrong folder” activation

If you run activation and nothing changes, you may be in the wrong directory.

macOS/Linux:

pwd
ls-a

Then cd into the folder that contains .venv, and activate again:

cd path/to/my-project
source .venv/bin/activate

Example 4: Use a different venv name

Some projects use venv instead of .venv.

source venv/bin/activate

Windows PowerShell:

.\venv\Scripts\Activate.ps1

Example 5: Install dependencies after activating

source .venv/bin/activate
python-m pip install-r requirements.txt

Common mistakes and how to fix them

Mistake 1: Activating in one terminal, running in another

What you might do

  • Activate in Terminal A
  • Run python in Terminal B and expect the same environment

Why it breaks

Activation only affects the current terminal session.

Corrected approach

Activate in the same terminal where you run your commands.

source .venv/bin/activate
python-c"import sys; print(sys.executable)"

Mistake 2: PowerShell blocks activation scripts

What you might do

.\.venv\Scripts\Activate.ps1

…and get a script execution error.

Why it breaks

PowerShell may block local scripts by policy.

Corrected approach

Allow scripts for your user, then try again:

JavaScript

Set-ExecutionPolicy-ExecutionPolicyRemoteSigned-ScopeCurrentUser

Then re-run activation.


Troubleshooting

If you see python: command not found, use python3 instead:

python3-m venv .venv
source .venv/bin/activate

If your prompt does not change, run the confirmation command:

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

Some shells do not show the venv name, but the environment may still be active.

If which python (or where python) points to system Python, confirm you activated the venv from the folder that actually contains .venv.

If you see Permission denied on macOS/Linux, make sure you use:

source .venv/bin/activate

If you see an execution policy error in PowerShell, run:

JavaScript

Set-ExecutionPolicy-ExecutionPolicyRemoteSigned-ScopeCurrentUser

Then activate again.


Quick recap

  • Create a venv if needed: python -m venv .venv
  • Activate it using the command for your OS and shell
  • Confirm python points inside .venv
  • Run deactivate when you’re finished