Trick ye hai: Jab tum virtual environment activate karte ho, to ye env ki site-packages/ ko sys.path mein prepend kar deta hai. Ab Python pehle env packages dhundta hai, system packages ko ignore karta hai.
Jab tum pip install -r requirements.txt chalate ho:
Step 1: pip har line padhta hai (format: package==version ya package>=version)
Step 2: Har package ke liye, pip:
PyPI se available versions query karta hai
Python version aur installed packages se compatibility check karta hai
Dependency tree resolve karta hai (package A ko B chahiye, B ko C)
Step 3: Topological order mein download karta hai (dependents se pehle dependencies)
Step 4: Har package install karta hai:
# Simplified pip install logicdef install_package(wheel_path, target_dir): with zipfile.ZipFile(wheel_path) as whl: whl.extractall(target_dir / 'site-packages') # site-packages/ metadata mein register karo
Version pinning kyun zaroori hai: == ke bina, pip latest version install karta hai. Chhe mahine baad, us version mein breaking changes aa sakti hain → tumhara code fail ho jaata hai.
Rule of thumb: Data science ke liye conda use karo (MKL, CUDA easily handle karta hai), baaki sab ke liye pip.
Recall Ek 12-Saal Ke Bacche Ko Explain Karo
Socho tum do LEGO projects bana rahe ho: ek castle aur ek spaceship. Tumhare paas ek bada dabba hai LEGOs ka. Har baar project switch karte ho, tumhe dabbe mein khod ke sahi pieces dhundne padte hain. Kabhi pieces kho jaate hain, ya spaceship ka piece castle mein galti se lag jaata hai.
Virtual environments do alag dabbe rakhne jaisi hain. Jab castle par kaam karo, sirf castle LEGOs dikhte hain. Jab spaceship par kaam karo, sirf spaceship LEGOs dikhte hain. Koi mixing nahi, koi confusion nahi.
pip ek catalog se LEGOs order karne jaisa hai—tum exactly wahi choose karte ho jo chahiye. conda ek pre-built LEGO kit lene jaisa hai jisme batteries, motors, aur instructions sab included hain. Conda zyada convenient hai lekin boxes bade hote hain.
2.1.3-Setting-up-development-environment - ML workflows mein venvs ka practical use
1.4.12-Profiling-and-optimization - Benchmarking ke liye isolated environments
#flashcards/ai-ml
Virtual environment kya hota hai? :: Ek isolated Python installation apni khud ki package directory (site-packages) ke saath, jo projects ke beech version conflicts rokta hai.
Venv activate karne se Python ka behavior kaise badalta hai?
Ye venv ki bin/ directory ko $PATH mein aur site-packages/ ko sys.path mein prepend kar deta hai, taaki python aur pip pehle venv versions resolve karein.
pip aur conda mein key difference kya hai?
pip sirf Python packages manage karta hai PyPI se; conda Python interpreter + system libraries (CUDA, MKL) + Python packages manage karta hai, pre-compiled binaries use karke.
Python ke built-in module se venv banane ka command?
python3 -m venv <env_name>
pip packages ko exact versions ke saath export karne ka command?
pip freeze > requirements.txt
Doosri machine par pip environment recreate karne ka command?
pip install -r requirements.txt
Environment activate karne ke baad hi pip install kyun karna chahiye?
Taaki packages venv ke site-packages mein install hon, globally nahi. "Works on my machine" jaisi problems rokta hai aur requirements.txt accurate rehti hai.
conda env export kya capture karta hai jo pip freeze nahi karta? :: Python version, conda packages, system libraries, aur pip packages—sab kuch jo environment completely recreate karne ke liye chahiye.