How to set up a Python virtual environment (venv) — in five commands, no library mess

You install a library for one project — and another one that worked yesterday suddenly breaks. Classic Python: packages all land in one shared pile, versions fight each other, and someone loses. A virtual environment (venv) is a separate box of libraries for a single project. Do it once and your projects stop stepping on each other. Here's the walkthrough.
What venv is and why (30 seconds of theory)
Short version: a venv is a folder inside your project where only this project's libraries live. Project A has its own set and versions, project B has its own, and your system stays clean. Without a venv, pip install dumps everything into one shared pile, and sooner or later project A wants one version of a package and project B wants another — and one of them breaks.
Step 1. Check Python is there
Open a terminal and type:
python3 --version
If a version prints (e.g. Python 3.12.4), you're set. If it says "command not found", install Python from python.org first. On Windows the command is often just python, no 3.
Step 2. Create the environment
Go into your project folder and run:
python3 -m venv .venv
A .venv folder appears — that's your box. The name .venv is a convention: the leading dot hides it from listings, and code editors recognize it automatically.
Step 3. Activate it
macOS / Linux:
source .venv/bin/activate
Windows (PowerShell):
.venv\Scripts\Activate.ps1
The sign it worked: (.venv) appears at the start of your terminal line. Now python and pip point inside the box, not at the system.
Step 4. Install libraries as usual
pip install requests
Now requests lands in .venv, not the system. It's the exact same pip — it just works inside an isolated box. This is a lifesaver with heavy AI libraries: if you run a model locally, keep its dependencies in a dedicated venv so you don't pollute the whole system.
Step 5. Freeze the list and exit
So someone else (or you tomorrow on another machine) gets the same versions:
pip freeze > requirements.txt
The requirements.txt file is a list of "what and which version". Later, install it all in one command: pip install -r requirements.txt. To leave the environment:
deactivate
What you get
A .venv folder with libraries for this project only, and a requirements.txt listing the versions. Projects no longer fight over packages, and anyone can reproduce your environment in one command.
Should I add .venv to .gitignore?
Yes, always. You don't commit the .venv folder to Git: it's heavy and specific to each machine. Only requirements.txt goes into the repo — from it, anyone recreates the environment locally. Add a .venv/ line to .gitignore and forget about it.
venv, virtualenv, conda — what's the difference?
venv is built into Python and covers 90% of cases — start there. virtualenv is an older external tool with the same ideas. conda is a separate manager (popular in data science): it installs not just Python packages but system dependencies too. Beginner rule: use the built-in venv, and move to the rest only when you hit a concrete need.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





