1.3.12 · HinglishPython Intermediate

Virtual environments — venv, pip, requirements.txt

1,753 words8 min readRead in English

1.3.12 · Coding › Python Intermediate


Virtual environments KYUN exist karte hain?

Teen cheezein jo venv isolate karta hai:

  1. Packages — har env ka apna site-packages hota hai.
  2. Python interpreter link — har env ek specific Python version ki taraf point karta hai.
  3. 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:

Figure — Virtual environments — venv, pip, requirements.txt

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 pythonweather/.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?
Dependency conflicts ("dependency hell") — har project ko apne package versions doosron se aur system Python se isolated rakhne deta hai.
.venv folder mein venv create karne ka command kya hai?
python -m venv .venv
venv 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?
Shell 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.txt
Us file se env ko kaise rebuild karein?
pip install -r requirements.txt
Kya .venv/ ko git mein commit karna chahiye?
Nahi — ise gitignore karo; requirements.txt commit karo (env reproducible, OS-specific, aur bada hota hai).
Kya activation terminal windows ke across persist karta hai?
Nahi — yeh sirf current shell session ko affect karta hai; har naye terminal mein re-activate karo.
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?
Env ka naam parentheses mein, jaise (.venv).
site-packages kya hai?
Wo directory jahan installed third-party packages rehte hain; har venv ka apna hota hai.

Connections

  • pip and PyPI
  • Python import system and sys.path
  • Dependency management — poetry, pipenv
  • Reproducible builds and Docker
  • .gitignore best practices
  • Semantic versioning

Concept Map

solved by

creates

is just a

contains

contains empty

edits

redirects

installs into

freeze produces

rebuilds env via

Dependency hell

Virtual environment

python -m venv module

Isolated folder

pyvenv.cfg to base Python

Private site-packages

PATH manipulation

activate

pip installer

requirements.txt