Virtual environments — venv, pip, requirements.txt
1.3.12· Coding › Python Intermediate
Virtual environments KYUN exist karte hain?
Teen cheezein jo venv isolate karta hai:
- Packages — har env ka apna
site-packageshota hai. - Python interpreter link — har env ek specific Python version ki taraf point karta hai.
pip/scripts — installed command-line tools env ke andar rehte hain.
VENV exactly kya hai? (First principles se derivation)
Jab Python start hota hai, wo imports search karne ke liye ek folder list banata hai, jise sys.path kehte hain. Key insight yeh hai:

KAISE use karein — poora workflow
Worked Example 1 — Ek fresh project banao
mkdir weather && cd weather
python -m venv .venv
source .venv/bin/activate
pip install requests
pip freeze > requirements.txt| Step | Yeh step kyun? |
|---|---|
mkdir weather |
Ek folder = ek project = ek env. Cheezein clean rehti hain. |
python -m venv .venv |
Isolated interpreter create karta hai. .venv conventional hidden naam hai. |
source .venv/bin/activate |
Ab which python → weather/.venv/bin/python, isliye installs yahan land karti hain. |
pip install requests |
.venv/lib/.../site-packages mein jaata hai, system Python mein nahi. |
pip freeze > requirements.txt |
requests==2.32.3 (+ uski dependencies) record karta hai taaki teammate reproduce kar sake. |
Worked Example 2 — Teammate ka project reproduce karo
git clone https://example/cool-app && cd cool-app
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt| Step | Yeh step kyun? |
|---|---|
git clone |
Tumhe code aur requirements.txt milta hai, lekin env nahi (wo gitignored hai). |
python -m venv .venv |
Env local & disposable hai — ise rebuild karo, ship mat karo. |
pip install -r requirements.txt |
Author ne jo exact package set use kiya tha usse re-create karta hai. Same versions = same behaviour. |
Worked Example 3 — freeze vs hand-written requirements
# pip freeze output (fully pinned, machine-generated)
flask==3.0.3
requests==2.32.3
certifi==2024.7.4 # ← transitive dependency, auto-included
# hand-written (loose, human-curated)
flask>=3.0
requests| Style | Kyun use karein? |
|---|---|
== pins (from freeze) |
Reproducible builds — production, exact state share karna. |
>= / unpinned |
Libraries, jahan tum chahte ho ki users compatible newer versions lein. |
Common mistakes (Steel-manned)
Recall Feynman: ek 12-saal ke bachche ko explain karo
Socho tumhare computer ka Python ek shared toy box jaisa hai poore ghar ke liye. Agar tumhara bada bhai usme race-car parts bhardega aur tumhe doll parts chahiye, toh tum dono ek hi box ke liye ladte ho. Ek virtual environment aise hai jaise har bachche ko apna chhota box de do sirf unke toys ke liye. python -m venv ek naya khaali box banata hai. pip install toys sirf tumhare box mein rakhta hai. requirements.txt ek shopping list hai taaki agar tumhara box kho jaaye, tum exactly wahi toys phir se kharid sako. Aur activate ka bas matlab hai "abhi mera box kholo aur use karo".
Active-recall flashcards
Virtual environments kaunsi problem solve karte hain?
.venv folder mein venv create karne ka command kya hai?
python -m venv .venvvenv ki jagah python -m venv kyun use karein?
-m us specific Python ke saath venv module run karta hai, fix karta hai ki env kaun sa interpreter version use karegi.Venv activate karne se under the hood kya hota hai?
PATH edit hoti hai taaki python aur pip env ki copies ki taraf point karein, installs ko env ke site-packages mein redirect karte hue.Env ke exact packages ko file mein kaise save karein?
pip freeze > requirements.txtUs file se env ko kaise rebuild karein?
pip install -r requirements.txtKya .venv/ ko git mein commit karna chahiye?
requirements.txt commit karo (env reproducible, OS-specific, aur bada hota hai).Kya activation terminal windows ke across persist karta hai?
Requirements mein flask==3.0.3 aur flask>=3.0 mein kya difference hai?
== ek exact version pin karta hai (reproducible builds); >= compatible newer versions allow karta hai (libraries).Active environment se kaise bahar niklen?
deactivate run karo.Jab env active hota hai toh prompt mein kya dikhta hai?
(.venv).site-packages kya hai?
Connections
- pip and PyPI
- Python import system and sys.path
- Dependency management — poetry, pipenv
- Reproducible builds and Docker
- .gitignore best practices
- Semantic versioning