1. What is venv? (The Concept)
Maven/Gradle project scope → venv in Python
Local Maven repository → venv's site-packages
pom.xml dependencies → requirements.txt
Java version in project → Python version in pyvenv.cfg
The Problem venv Solves
Without venv (danger):
- All Python packages install to
C:\Python313\Lib\site-packages\ - All projects share the same packages
- Package version conflicts break your code
- Hard to know what packages a project actually needs
With venv (safe):
- Each project gets its own
site-packagesfolder - No version conflicts between projects
requirements.txtdocuments exactly what the project needs- Easy to replicate on another machine (like cloning a Maven project)
What venv Actually Does
venv creates a lightweight copy of Python that points to your project folder:
2. The venv Workflow
Create venv
python -m venv myenvActivate venv
myenv\Scripts\activateInstall packages
pip install requestsSave versions
pip freeze > requirements.txtmyenv\Scripts\activatepip install -r requirements.txtAll packages available
3. Windows Step-by-Step Setup
Step 1: Open Command Prompt or PowerShell
In VS Code / Cursor: Ctrl + ` → Terminal → PowerShell or cmd
Or press Win + R, type cmd, press Enter
Step 2: Navigate to Your Project Folder
Step 3: Create Virtual Environment
Takes a few seconds. A new myenv/ folder appears.
Step 4: Activate It
Command Prompt:
PowerShell (if execution policy blocks scripts):
Then:
(myenv) at the start.
Step 5: Verify Activation
Should show a path containing myenv\Scripts\python.exe
Step 6: Install Packages
Step 7: Freeze Dependencies
Lists packages and versions — share this with teammates.
Step 8: Deactivate
Back to system Python. The (myenv) prefix disappears.
4. VS Code & Cursor Integration
VS Code Setup
Option 1: Auto-Detection
- Create and activate venv (steps 3–4 above)
- Open your project folder in VS Code
- If prompted to select a Python environment, choose
./myenv/Scripts/python.exe - VS Code remembers this per workspace
Option 2: Manual Selection
- Bottom-right: click the Python version indicator
- Choose or type
./myenv/Scripts/python.exe
Option 3: settings.json
Ctrl + Shift + P→ “Preferences: Open Workspace Settings (JSON)”- Add:
Cursor Setup
Cursor uses the same VS Code Python extension, so the steps match:
Step 1: Create venv in Terminal
Step 2: Open Project in Cursor
File → Open Folder → select your project
Step 3: Select Python Interpreter
Bottom-right: click Python version → choose ./myenv/Scripts/python.exe
Step 4: Use Cursor's Terminal
Ctrl + ` — the terminal should auto-activate your venv once the interpreter is set.
5. Common Windows Problems & Fixes
Problem 1: python: command not found
Solution:
- Python not installed or not on PATH
- Test with
python --version - If it fails, reinstall from python.org
- During install, check “Add Python to PATH”
Problem 2: (myenv) does not appear after activation
Diagnosis:
If it shows C:\Python313\python.exe (not your myenv folder), activation failed.
Fixes:
- Command Prompt:
myenv\Scripts\activate.bat - PowerShell: set execution policy, then
myenv\Scripts\Activate.ps1 - Git Bash:
source myenv/Scripts/activate(forward slashes)
Problem 3: pip installs to the wrong place
Check whether venv is active:
Should include myenv\Lib\site-packages. If it shows system Python, activate first.
Problem 4: ModuleNotFoundError but pip list shows the package
Python is running from the wrong interpreter (system, not venv).
- Activate:
myenv\Scripts\activate - In the IDE: select
./myenv/Scripts/python.exe - Restart the IDE terminal
Problem 5: Works in terminal, fails in IDE
- Click Python version (bottom-right)
- Confirm it points at
./myenv/Scripts/python.exe - Close and reopen the Python file
Problem 6: PowerShell execution policy
Problem 7: No module named venv
- Reinstall Python from python.org
- During install, keep
tcl/tk and IDLEandpy launcherchecked
6. FAQ
Do I need to activate venv every time?
Yes for a raw terminal. In VS Code/Cursor, configure the interpreter once and the IDE can auto-activate.
Can I rename the venv folder?
Yes. env, .venv, or any name — just update the activation path.
Should I commit venv to Git?
No. Add to .gitignore:
Commit requirements.txt. Teammates run pip install -r requirements.txt.
One venv per project or one for all?
One per project. Shared venvs become version hell. Two folders can even use different Python versions at once — see section 10.
venv vs conda?
venv: built-in, lightweight, pip. Best for Python-only work.
conda: separate tool; handles non-Python deps. Common in data science/ML.
Start with venv — enough for most projects.
Multiple venvs in one project?
Technically possible; avoid it. One venv = one environment.
How do I update packages?
I accidentally installed into system Python
- Activate your venv
- Reinstall:
pip install package-name - Inside the venv, that copy takes priority
venv vs virtualenv?
Similar idea. venv is built into Python 3.3+. Prefer venv.
What about uv?
uv is a fast modern tool that can create venvs and install deps. For pinning a Python version (e.g. 3.10), use uv python install 3.10 then uv venv --python 3.10 .venv — see section 9.
7. Complete Java → Python Translation
| Java / Maven | What it does | Python / venv | What it does |
|---|---|---|---|
pom.xml |
Declares dependencies | requirements.txt |
Lists installed packages |
mvn install |
Downloads to local repo | pip install -r requirements.txt |
Installs from file |
| Local Maven repo (~/.m2) | JAR cache | venv/Lib/site-packages/ |
Package cache for this env |
mvn clean |
Removes build artifacts | deactivate then delete myenv |
Removes the environment |
CLASSPATH |
Where .class files live | PYTHONPATH |
Where .py modules live |
| Java version in pom | Project Java version | pyvenv.cfg |
Project Python version |
| Project-scoped dependency | Only this project | Packages in venv | Only when activated |
mvn dependency:tree |
Shows dependency tree | pip list |
Shows installed packages |
Mental model
Activate venv = enter the project’s Maven scope
pip install X = add X and download it locally
requirements.txt = pom.xml equivalent
site-packages/ = this project’s local repo
Deactivate = leave project scope, back to system
8. Real Example: Web Scraper Project
Scenario: build a scraper with BeautifulSoup.
9. Real Example: project1 — Pin Python 3.10 with uv
From Week-02 AI Learning: this project targets Python 3.10. A venv cannot change its Python version in place — if you need a different version, recreate it. Prefer .venv and uv for a modern workflow.
1. Set version constraints
In pyproject.toml:
Or pin to 3.10 only:
In .python-version:
2. Install Python 3.10 (if needed)
If that fails:
3. Remove any existing venv
Recreate when switching Python versions:
4. Create a new venv with 3.10
5. Activate and verify
Python 3.10.x.
6. Install dependencies
or:
Notes (gotchas)
- Prefer
>=3.10,>=3.10,<3.11, or==3.10.*inrequires-python— not a bare= "3.10". pip install python 3.10does not install Python; useuv python install 3.10(or the official installer).uv initcreates a project folder, not a virtual environment. Useuv venvfor the venv.
requires-python / .python-version ≈ pinning the JDK in pom.xml / toolchain.uv venv --python 3.10 ≈ creating a project scoped to that JDK — if you change the JDK, you rebuild the environment, you don’t “upgrade in place.”
10. Demo: Two Projects, Two venvs, Two Python Versions
Each project folder owns its own .venv. Those venvs can point at different Python versions at the same time — like two Java projects using JDK 17 and JDK 21 side by side. Activating one never changes the other.
Folder layout
Create both (PowerShell)
Install both interpreters once (if needed), then create a venv inside each project folder:
Side-by-side: activate and prove the versions differ
Open a terminal in project-a:
Open a second terminal in project-b:
Packages installed in project-a\.venv are invisible to project-b, and vice versa. Same rule as two Maven projects with different JDKs and different ~/.m2-scoped deps.
What must stay true
- One venv per project folder — never share
.venvacross projects. - Activate after
cdinto that project —pythonthen resolves to that folder’s interpreter. - Changing Python version = recreate the venv — do not try to “upgrade” an existing
.venvin place. - In the IDE, pick
project-a\.venv\Scripts\python.exefor A andproject-b\.venv\Scripts\python.exefor B (separate windows/workspaces).
.venv folders → can run Python 3.10 and 3.13 at the same time without fighting over system Python.
You've got this
venv is simple once the mental switch flips:
venv = Maven-style dependency isolation for Python
Activate → install → freeze to requirements.txt → done. With uv: pin the version → uv venv → uv sync. Each project folder keeps its own .venv — and its own Python version.