Python Modules & Virtual Environments 🔍 | pip Introduction #14 || Python ka Pitara || DeathCode - DeathCode

📜 Python Modules & pip Mastery 🔥 | Install, Create, Freeze & Upgrade Like a Pro!


🌟 Introduction

"Python ke har bade developer ka ek hi secret hota hai — woh dusron ke banaye tools ka best use karta hai… aur khud bhi banata hai!

Aaj hum karenge deep dive into modules, custom modules, aur pip — Python ka Google Play Store!"


🔹 1. Built-in Modules vs. Custom Modules

📌 Built-in Modules:

  • Pre-installed hote hain (math, random, os, etc.)
  • Direct import karo, install ki zarurat nahi

📌 Custom Modules:

  • Apna code ek alag .py file me likho, fir kahin bhi import karke use karo

Yeh reusability aur clean separation ke liye best practice hai


🔹 2. Apna Khud ka Module Banao (Custom Module)

📁 my_utils.py

def greet(name):
    return f"Hello, {name}!"
 
def square(num):
    return num ** 2

📁 main.py

import my_utils
 
print(my_utils.greet("DeathCode"))
print(my_utils.square(4))

🧬 Why?

  • Ek hi function ko alag-alag files me reuse kar sakte ho
  • Production-level code modular hota hai

🔹 3. __name__ == "__main__" ka Funda

# my_utils.py
 
def greet(name):
    return f"Hello, {name}!"
 
if __name__ == "__main__":
    print(greet("Test"))

Jab ye file direct run hoti hai to __name__ ki value "__main__" hoti hai. Agar kisi aur file se import hoti hai, to ye block run nahi hota.


🔹 4. pip Kya Hai?

  • pip = Python ka official package installer
  • Jaise Play Store me apps hote hain, waise Python ke packages PyPI pe hote hain
  • Already aata hai Python ke sath (Python >= 3.4)

🔹 5. pip se Package Install Karna

pip install requests

requests ek popular HTTP library hai

Check karo:

pip show requests

Tip: Use pip list to see all installed packages


🔹 6. Uninstall Karna

pip uninstall requests

Clean uninstall ke liye confirm prompt aayega


🔹 7. Upgrade Karna

pip install --upgrade requests

Latest version install karega, system crash se bachaoge


🔹 8. Freeze Your Environment (requirements.txt)

pip freeze > requirements.txt

📁 requirements.txt

requests==2.31.0
flask==2.3.3

Dusra banda sirf itna kare:

pip install -r requirements.txt

Isse exact same setup har machine pe ready ho jaata hai


🔹 9. Virtual Environment (venv) ka Powerful Use

Jab aapko alag-alag projects ke liye alag package versions chahiye hote hain, tab virtual environment sabse bada hero hota hai.

Banane ka Tarika:

python -m venv env

Activate:

Linux/Mac:

source env/bin/activate

Windows:

env\Scripts\activate

Ab aapka terminal me (env) prefix dikhega = you're in isolated environment

Pip ab is env ke andar kaam karega:

pip install flask

Deactivate karne ke liye:

deactivate

Tips:

  • Har project ke liye ek venv banao
  • Git me env/ folder ko .gitignore me daalo
  • Team ke sath sirf requirements.txt share karo

🔥 Real-World Use Cases

  • Apna utility package banao jaise math_tools.py
  • Web scraping ke liye install karo beautifulsoup4
  • Backend project? → pip install flask
  • Data analysis ke liye → pandas, numpy

🚀 Keep it up!

"Ab tum Python ke developer zone me enter kar chuke ho — jahan tum package bana bhi sakte ho, aur manage bhi kr sakte ho."

"Agla part hoga: Most-Used Python Modules jaise time, random, os, math, sys — inke powerful use-cases ke sath!"

Like karo, comment karo, aur share zaroor karo — Python ka pitara khul chuka hai bhai!

© 2024 DeathCode. All Rights Reserved.