Learning path: Ship Your First Model: From Notebook to a FastAPI Service · Step 5 of 9
Python Virtual Environments: One Project, One Toolbox
Why every Python project gets its own virtual environment: two projects that need different versions of requests, python -m venv .venv, activation, pip install and the PyCharm setting that activates it for you.
What you will learn
- Explain why two projects on one laptop clash over one shared package
- Create a .venv folder with python -m venv and activate it
- Install a package into the project's own toolbox and check which Python runs
- Keep a requirements.txt packing list and keep .venv out of Git
Before you start
- Basic Python
- Running a script from a terminal
A plumber and an electrician each carry their own toolbox. The plumber's wrench never ends up in the electrician's bag, and nobody fights over tools. A Python project needs the same thing: its own toolbox of packages. That toolbox is a virtual environment.
One project, one toolbox. Let's see why, and how to switch one on in PyCharm.
Two projects, one laptop
Say you have two projects on one laptop:
weather_appneedsrequestsversion 2.31.0.chat_botneedsrequestsversion 2.32.3.
If both use the one Python installed on the laptop, they share one copy of requests. You upgrade it for the chat bot, and the weather app changes too, even though you never touched it.
A virtual environment fixes this. Each project gets its own private folder with its own Python and its own packages, so each one keeps its own version.
requests. With a .venv per project, each one keeps the version it needs.The small script we will run
Each project has a tiny main.py that prints two things: the version of requests it sees, and the folder its Python lives in.
import pathlib
import sys
import requests
print(f"weather_app uses requests {requests.__version__}")
print("Python lives in:", "/".join(pathlib.Path(sys.prefix).parts[-2:]))
sys.prefix is the folder of the Python that is running the script. We keep only its last two parts so the output stays short.
Create it, then activate it
Open the weather_app project in PyCharm and open the Terminal. Then:
1. Create the toolbox
cd weather_app
python -m venv .venv
python -m venv .venv creates a folder called .venv inside the project. That folder is the toolbox: a private copy of Python plus room for packages.
2. Activate it
# Windows
.venv\Scripts\activate
# macOS / Linux
source .venv/bin/activate
Once it's active, (.venv) shows up at the start of the prompt. From now on this terminal uses the project's own Python, not the one installed on the laptop.
3. Install into the toolbox
pip install requests==2.31.0
python main.py
weather_app uses requests 2.31.0
Python lives in: weather_app/.venv
pip install put requests inside weather_app/.venv and nowhere else. The second line proves which Python ran the script: the one in the project's own .venv.
The chat_bot project gets its toolbox with uv, another tool for the same job (more on it below). Same idea, its own .venv, with requests==2.32.3:
cd chat_bot
uv init
uv add requests==2.32.3
uv run main.py
chat_bot uses requests 2.32.3
Python lives in: chat_bot/.venv
Two projects, two versions of the same package, on one laptop. Neither one knows the other exists.
Let PyCharm activate it for you
Typing the activate command in every new terminal gets old fast. Once PyCharm is using this .venv as the project's interpreter, every new terminal you open in that project activates it for you. That choice lives in the project's Python interpreter setting.
Quick check: if you are ever unsure which Python is running, print sys.prefix like main.py does. If the path doesn't end in your project's .venv, the environment isn't active.
The packing list: requirements.txt
The .venv folder belongs to one laptop. What you share is the list of packages in it, so anyone can rebuild the same toolbox:
# write the packing list
pip freeze > requirements.txt
# rebuild from it anywhere, inside a fresh, active .venv
pip install -r requirements.txt
For weather_app, the list holds requests and the packages it pulled in:
certifi==2026.10.23
charset-normalizer==3.5.2
idna==3.20
requests==2.31.0
urllib3==2.8.0
Commit requirements.txt to Git, never the .venv folder. A .gitignore with one line keeps it out:
.venv/
With uv, as in chat_bot, the idea is the same: uv add requests==2.32.3 records the package in pyproject.toml and pins every version in uv.lock, and uv run main.py rebuilds the .venv for you when it is missing.
Common mistakes
- Installing before activating. If the prompt has no
(.venv),pip installgoes into the laptop's shared Python, which is exactly what we wanted to avoid. - Committing the
.venvfolder. It is large and tied to one machine. Sharerequirements.txtand rebuild instead. - One environment for every project. That brings the clash straight back. One project, one
.venv.
Recap
| Step | Command | What it does |
|---|---|---|
| Create | python -m venv .venv | Makes the project's own toolbox |
| Activate | .venv\Scripts\activate (Windows), source .venv/bin/activate | This terminal now uses the project's Python |
| Install | pip install requests==2.31.0 | Puts the package in this toolbox only |
| Share | pip freeze > requirements.txt | Writes the packing list for Git |
| Rebuild | pip install -r requirements.txt | Recreates the same toolbox anywhere |
Try it yourself: make a third project that needs another version of requests, freeze it to a requirements.txt, then rebuild it in a fresh folder from that list and run main.py to check the version.
Built this? Share your solution in the StudyEasy AI Community.
Join free on Skool →Project: Car Price Predictor End to End, from CSV to a FastAPI Price API
Next →Git and GitHub Fundamentals: Commits, Branches, Push and Pull
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Machine Learning Engineer: From First Model to ML Engineer
Learn machine learning in Python, then ship it: data analysis, regression, classification, tuning, deployment and MLOps.View on Udemy ↗: Machine Learning Engineer: From First Model to ML Engineer (opens in a new tab)Related articles
Git and GitHub Fundamentals: Commits, Branches, Push and Pull
The Git you use every day, on a tiny hello-ml project: commits as save points, the staging area, a branch merged with a fast-forward, then push, clone, pull and pull requests with a teammate.
Testing a FastAPI App with pytest: Three Small Tests
A tiny FastAPI app with a Hello World route and a name route that refuses numbers, three pytest tests that check it, and what happens when the validation is deleted by mistake.
Config and Logging Instead of Hard-Coded Values in ML Training
Take the hard-coded values out of a car price training script: one config.toml read with tomllib, logging with levels and a run id on every line, and secrets read from the environment. Same RMSE, 2195, before and after.
SQL Essentials for Python: SELECT, WHERE, JOIN and GROUP BY
The SQL a Python developer needs, on a tiny shop with four customers and eight orders: CREATE TABLE, INSERT with placeholders, SELECT with WHERE, JOIN, GROUP BY, pandas.read_sql, and why values never go in f-strings.