← Back to Django Course | Chapter 1: Django Fundamentals & Setup | Lesson 4 of 9

Python के Virtual Environments

एक virtual environment हर project के लिए एक अलग toy box जैसा है, ताकि एक project के toys दूसरे के साथ न मिलें।

Virtual Environments क्यों मायने रखते हैं

अलग-अलग projects को उसी package के अलग-अलग versions चाहिए हो सकते हैं; एक virtual environment हर project के packages को अलग रखता है ताकि वे कभी एक-दूसरे से conflict न करें।

उदाहरण: Why Virtual Environments Matter

Run after activating a venv, this installs Django 4.2 only inside that project's venv — a different project's venv can have Django 5.0 installed at the same time with no conflict.

bash
pip install django==4.2

⚠️ Run this in your own terminal or Node.js environment.

एक Virtual Environment बनाना

"python -m venv venv" run करना "venv" नाम का एक नया folder बनाता है जिसमें सिर्फ इस project के लिए Python और pip की एक isolated copy होती है।

उदाहरण: Creating a Virtual Environment

Creates a new virtual environment folder named venv in the current directory.

bash
python -m venv venv

⚠️ Run this in your own terminal or Node.js environment.

Virtual Environment Activate करना

Linux या macOS पर आप environment को "source venv/bin/activate" से activate करते हैं; एक बार active होने पर, आपका terminal prompt बदल जाता है और कोई भी pip install सिर्फ उस environment में जाता है।

Note: कभी भी "deactivate" command से deactivate करें।

उदाहरण: Activating the Virtual Environment

Activates the virtual environment so installed packages stay isolated to this project.

bash
source venv/bin/activate

⚠️ Run this in your own terminal or Node.js environment.

Active Environment Confirm करना

Activation के बाद "which python" run करना दिखाता है कि Python interpreter अब venv folder के अंदर point करता है, confirm करते हुए कि isolated environment active है।

Warning: Windows पर equivalent command "where python" है।

उदाहरण: Confirming the Active Environment

Prints the path of the active Python interpreter to confirm the virtual environment is in use.

bash
which python

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Project packages globally install करना, जो projects के बीच conflicts का कारण बनता है जिन्हें अलग-अलग versions चाहिए।
  2. Packages install करने से पहले virtual environment activate करना भूल जाना।
  3. Virtual environment folder को खुद version control में commit करना बजाय इसे ignore करने के।
चैप्टर सारांश
  • एक virtual environment एक project के Python packages को system के बाकी हिस्से से isolate करता है।
  • "venv" module (Python में built-in) virtual environments बनाता है।
  • Packages install करने से पहले आपको एक virtual environment activate करना चाहिए।
  • हर project का अपना virtual environment अलग-अलग package versions के साथ हो सकता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.