Single logo
  • Courses
  • Blog
  • About
Join for freeJoin free
Learning pathShip Your First Model: From Notebook to a FastAPI Service
  1. 1
    Saving and Loading Models with joblib: One Pipeline, One File
  2. 2
    FastAPI Hello World: Routes, uvicorn and the Free Swagger Page
  3. 3
    Deploy a Machine Learning Model with FastAPI: POST /predict for a Saved Model
  4. 4
    Project: Car Price Predictor End to End, from CSV to a FastAPI Price API
  5. 5
    Python Virtual Environments: One Project, One Toolbox
  6. 6
    Git and GitHub Fundamentals: Commits, Branches, Push and Pull
  7. 7
    Testing a FastAPI App with pytest: Three Small Tests
  8. 8
    Config and Logging Instead of Hard-Coded Values in ML Training
  9. 9
    SQL Essentials for Python: SELECT, WHERE, JOIN and GROUP BY
  1. Blog
  2. /
  3. Machine Learning
  4. /
  5. MLOps and Production ML

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.

Chaand Sheikh

·27 November 2026Engineering Basics

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_app needs requests version 2.31.0.
  • chat_bot needs requests version 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.

Shared Python versus one virtual environment per project One shared Python weather_app chat_bot one requests upgrade one, both change One toolbox each weather_app chat_bot .venv requests 2.31.0 .venv requests 2.32.3 nothing clashes
With one shared Python, both projects use the same copy of 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 install goes into the laptop's shared Python, which is exactly what we wanted to avoid.
  • Committing the .venv folder. It is large and tied to one machine. Share requirements.txt and rebuild instead.
  • One environment for every project. That brings the clash straight back. One project, one .venv.

Recap

StepCommandWhat it does
Createpython -m venv .venvMakes the project's own toolbox
Activate.venv\Scripts\activate (Windows), source .venv/bin/activateThis terminal now uses the project's Python
Installpip install requests==2.31.0Puts the package in this toolbox only
Sharepip freeze > requirements.txtWrites the packing list for Git
Rebuildpip install -r requirements.txtRecreates 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 →
← Previous
Project: Car Price Predictor End to End, from CSV to a FastAPI Price API
Next →
Git and GitHub Fundamentals: Commits, Branches, Push and Pull
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool
Go deeper with a course

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.

On this page
  • Two projects, one laptop
  • The small script we will run
  • Create it, then activate it
  • 1. Create the toolbox
  • 2. Activate it
  • 3. Install into the toolbox
  • Let PyCharm activate it for you
  • The packing list: requirements.txt
  • Common mistakes
  • Recap
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool
Go deeper with a course

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)

Single logo

Empowering Education, One Course at a Time

About
About usContact usFAQs
Legal
Terms of ServicePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.