Why they exist
Picture two projects. One needs version 1 of a library, the other version 3, and they're incompatible. Install everything system-wide and only one project works.
A virtual environment is a separate folder with its own set of libraries for one project. Each project lives in its own box and doesn't disturb its neighbours.
The second use matters more: without one you can't say clearly what your project needs to run. Which means you can neither deploy it nor hand it to anyone.
Creating and activating
In the project folder:
python -m venv .venv
A folder appears containing a separate interpreter and libraries.
Activate on Linux and macOS:
source .venv/bin/activate
Activate on Windows:
.venv\Scripts\activate
Once active, the environment name appears at the start of your terminal prompt — the sign that you're inside it.
Deactivate:
deactivate
Installing libraries
Inside an active environment:
pip install requests
It installs into this project only; the system is untouched.
Recording dependencies
So somebody else can run your project:
pip freeze > requirements.txt
The file listing libraries and versions goes into the repository.
Installing from it on another machine:
pip install -r requirements.txt
Without that file your project can't be run, and it's the first thing people check when opening an unfamiliar repository.
What not to commit
The environment folder itself. It weighs tens of megabytes, depends on the operating system, and is useless to anyone: the other person creates their own.
Add it to your ignore list. An environment folder in a repository is a reliable beginner's marker.
Common mistakes
Forgetting to activate. You install a library and it goes system-wide. The sign: no environment name in the prompt.
Different environments in the editor and the terminal. The editor runs the wrong interpreter and reports "library not found". Check which interpreter the project settings point at.
Forgetting to update the dependency list. You installed a library but didn't record it. Somebody else's copy won't start.
The environment ended up in the repository. Remove it and add it to the ignore list.
Is there anything more convenient
Yes — tools exist that manage environments and dependencies together and run faster. But start with the built-in method: it's everywhere, needs no installation, and understanding how it works pays off regardless.
Хватит читать — пора делать
На CohortX можно найти команду под пет-проект и получить тот самый опыт, о котором спрашивают на собеседовании.
Похожие статьи
- FastAPI: your first service in an eveningWhat a service for programs is, how to write your first one in an evening, and why automatic documentation saves time.
- FastAPI: первый сервис за вечерЧто такое сервис для программ, как написать первый за вечер и почему автоматическая документация экономит время.
- Django for beginners: a first project step by stepFrom installation to a working list with an admin panel — the steps, and what each one means.