← Back to Python Course | Chapter 8: Modules & Packages | Lesson 4 of 6

Python pip और Packages

pip Python के लिए एक app store जैसा है: यह दूसरे लोगों द्वारा लिखे गए extra code packages download करता है ताकि आप उन्हें इस्तेमाल कर सकें। Packages खास कामों के लिए तैयार toolkits होते हैं।
Syntax
python
pip install package_name
pip uninstall package_name
pip list

pip क्या है?

pip Python का package installer है, जो आपको Python Package Index (PyPI) से third-party code को 'pip install requests' जैसे एक ही command से खींचने देता है, बजाय इसके कि आप किसी library को खुद manually download और configure करें।

उदाहरण: What is pip?

python
# pip install requests
print("pip installs packages from PyPI")

requirements.txt File

एक requirements.txt file किसी project के dependencies वाले exact packages (और अक्सर exact versions) को pin कर देती है, ताकि कोई भी और व्यक्ति 'pip install -r requirements.txt' से आपका environment फिर से बना सके, बजाय यह अंदाज़ा लगाने के कि आपने कौन-सी libraries और versions इस्तेमाल की थीं।

उदाहरण: The requirements.txt File

python
# requirements.txt would contain:
# requests==2.31.0
# Install with: pip install -r requirements.txt
print("requirements.txt pins project dependencies")

Virtual Environments

एक virtual environment सिर्फ़ एक project के लिए एक isolated Python installation बनाता है, ताकि वहाँ कोई package install करने से उसी machine के दूसरे projects के लिए install किए गए packages प्रभावित (या टकराव में) न आएँ -- यही वह चीज़ है जो reproducible, per-project dependencies को संभव बनाती है।

उदाहरण: Virtual Environments

python
# python -m venv venv
# source venv/bin/activate
print("Virtual environments isolate per-project dependencies")

Installed Packages को Query करना

आप किसी project की site-packages directory जाँचकर देख सकते हैं कि pip वाकई packages कहाँ install करता है, जो 'Python मेरी installed library क्यों नहीं ढूँढ पा रहा' जैसी समस्याओं को debug करने में उपयोगी है -- अक्सर इसका जवाब यह होता है कि pip ने आपकी script चलाने वाले environment से अलग किसी Python environment में install किया।

उदाहरण: Querying Installed Packages

python
import sys
print(sys.prefix)  # shows which Python environment is active

Best Practices

किसी third-party import को try/except ImportError में लपेटने से आपका program एक confusing traceback की बजाय एक स्पष्ट, actionable message ('please install requests') के साथ fail होता है, जो उन optional dependencies के लिए सबसे ज़्यादा मायने रखता है जिनकी आपकी script के हर user को ज़रूरत नहीं होगी।

उदाहरण: Best Practices

python
try:
    import requests  # optional third-party dependency
except ImportError:
    print("please install requests")  # clear message instead of a raw traceback
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. #}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

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.