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.
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
- One .venv per project.
- Activate, or call its python directly.
- Recreate from requirements.
Code examples
Fresh start for a project
Rebuilding a broken environment
Deleting .venv is safe because requirements.txt describes how to rebuild it.
More examples
Running an environment's interpreter directly
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:
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
Prefer
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
- 1Set up a new project folder with .venv, a requirements.txt and a .gitignore.
- 2Write 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.