Skip to content

Module 16 · External Libraries and APIs — topic 4 of 12

Virtual environments

Day-to-day virtual environment workflow: one .venv per project, activating it in terminals and editors, recreating it from requirements, running tools without activation, and checking that the right interpreter is used.

Beginner 8 min read·Lesson 238 of 306 in Python Programming

Learning objectives

  • Follow a reliable per-project environment workflow
  • Run a virtual environment's Python without activating it
  • Recreate an environment from requirements

Definition

In practice, a virtual environment is the .venv folder that holds a project's interpreter link and packages. The workflow is: create it once, activate (or call its python directly) whenever you work, install into it, record requirements, and recreate it whenever needed.

Introduction

The Modules and Packages lesson explained what virtual environments are. This lesson is about using them every day without friction.

Why it matters

A consistent workflow means every project runs with the right packages, and a broken environment can be rebuilt in a minute.

Detailed explanation

Create one environment per project, in the project folder, named .venv: python -m venv .venv.

Activate it in every new terminal before working. Your prompt shows (.venv) while it is active.

Scripts and schedulers can skip activation by calling the environment's interpreter directly: .venv\Scripts\python.exe on Windows or .venv/bin/python elsewhere.

Editors such as VS Code and PyCharm detect .venv and use it for running, debugging and code completion; select it as the interpreter if they don't.

If an environment breaks or you upgrade Python, delete .venv and recreate it: python -m venv .venv, then python -m pip install -r requirements.txt.

Keep .venv out of version control with a .gitignore entry; commit requirements.txt instead.

Check which interpreter is running with sys.executable, and whether it is a virtual environment with sys.prefix != sys.base_prefix.

Syntax

Terminal
python -m venv .venv
.venv\Scripts\activate # Windows
source .venv/bin/activate # macOS / Linux
python -m pip install -r requirements.txt
# without activating
.venv/bin/python main.py
  • One .venv per project.
  • Activate, or call its python directly.
  • Recreate from requirements.

Code examples

Fresh start for a project

Terminal
git clone https://github.com/example/project.git
cd project
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
python -m pip install -r requirements.txt
python main.py

Rebuilding a broken environment

Terminal
deactivate
rm -rf .venv # Windows PowerShell: Remove-Item -Recurse .venv
python -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt

Deleting .venv is safe because requirements.txt describes how to rebuild it.

More examples

Running an environment's interpreter directly

Python
import os
import subprocess
import venv
from pathlib import Path
venv.create("env", with_pip=False)
py = Path("env") / ("Scripts/python.exe" if os.name == "nt" else "bin/python")
result = subprocess.run([str(py), "-c", "import sys; print(sys.prefix != sys.base_prefix)"],
capture_output=True, text=True)
print(py.exists(), result.stdout.strip())
Output
True True

Step-by-step explanation

  • venv.create builds a fresh environment in the env folder.
  • The interpreter path differs between Windows and other systems.
  • Running that interpreter directly confirms it is a virtual environment, with no activation step involved.

Expected output

Running “Running an environment's interpreter directly” prints exactly:

TextOutput
True True

Practical applications

  • Scheduled jobs that call /path/to/project/.venv/bin/python script.py.
  • Teams onboarding new members with a three-command setup.

Best practices

  • Create .venv inside each project.
  • Use the venv's interpreter in scripts and schedulers.
  • Treat environments as disposable.

Common mistakes

Sharing one environment across projects

Avoid

~/envs/everything # used by every project

Prefer

project-a/.venv and project-b/.venv

Projects end up depending on each other's package versions.

Important notes

  • On Windows, if activation scripts are blocked, run Set-ExecutionPolicy -Scope CurrentUser RemoteSigned in PowerShell, or call the interpreter directly.

Key takeaways

  • One .venv per project, ignored by git.
  • Activate or call .venv's python directly.
  • Rebuild from requirements whenever needed.

Try it yourself

  1. 1
    Set up a new project folder with .venv, a requirements.txt and a .gitignore.
  2. 2
    Write a script that refuses to run unless it is inside a virtual environment.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

Test yourself across the whole course in the Python practice bank.