How Python Imports Modules & Packages #23 || Python ka Pitara || DeathCode - DeathCode

"Python ka import system ek powerful mechanism hai jo humare code ko organize aur optimize karta hai. Aaj hum samjhenge ki kaise modules aur packages kaam karte hain, aur unhe optimize kaise kiya jata hai for better performance. Hum is video mein imports ko deeply samjhenge, aur purani chizon se bhi relate karenge, taaki aapko internal working ka clear understanding mile."


🔹 1. Module in Python Kya Hota Hai?

🛆 Module Ko Samjho

  • Module ek Python file hoti hai (.py file) jisme aap functions, classes, variables likhte ho.
  • Simple shabdon mein, module ek reusable code ka piece hai jo aap dusre files mein import karke use kar sakte ho.

Example:

# greetings.py
def greet(name):
    return f"Hello, {name}!"
# another file
import greetings
 
print(greetings.greet("Alice"))
  • Jaise functions reusability ke liye hote hain, waise hi modules bhi code reuse aur organization ke liye hote hain.
  • Module = multiple functions/classes ka organized box 🛆

🔹 2. Package in Python Kya Hota Hai?

📁 Package Ko Samjho:

  • Package ek folder hota hai jisme multiple modules hote hain.
  • Har package ke andar ek __init__.py file hoti hai jo Python ko batati hai ki yeh folder ek package hai.

🧱 Example Structure:

my_package/
    __init__.py
    module1.py
    module2.py
from my_package import module1

💡 Connection with Function Call Stack:

  • Jaise call stack function calls ko organize karta tha,
  • Waise hi packages modules ko organize karte hain — bada project manage karne ke liye.

🔹 3. Module & Package Ko Import Kaise Karte Hain?

🔍 Import Kaise Kaam Karta Hai?

  1. Python pehle check karta hai ki module already memory mein hai ya nahi.

  2. Agar nahi hai, to Python ye locations check karta hai:

    • Current folder
    • PYTHONPATH
    • Standard Library
    • Installed 3rd-party libraries
import math

Python math ko pehle memory mein dhundhta hai. Nahi milta to search karke load karta hai.

💡 Relating with Call Stack:

  • Jaise call stack avoid karta hai repeated function execution,
  • Waise hi Python import system cached modules reuse karta hai — fast aur efficient.

🔹 4. Python Import System ka Optimization

🧠 Import Optimization Tricks:

  • Python ek internal cache use karta hai: sys.modules

    → Jo module ek baar load ho gaya, dobara load nahi hota.

import time  # Load only when needed

Selective Importing (Memory Efficient):

from math import sqrt  # Only `sqrt`, not whole math module

Isse memory bachti hai aur execution fast hota hai.

♻️ sys.modules Example:

import sys
import math
 
print(sys.modules['math'])  # Module reference

💡 Connection with Stack Frames:

  • Jaise stack frame variables ko efficient way mein store karta hai,
  • Waise hi sys.modules module imports ko cache karta hai — memory save karta hai.

🔹 5. Circular Import – A Common Error

♻️ Circular Import Kya Hota Hai?

  • Jab do modules ek dusre ko import karte hain, to infinite loop create ho jata hai — ImportError deta hai.
# moduleA.py
import moduleB
 
# moduleB.py
import moduleA

Dono ek dusre ko wait karte hain — aur program stuck ho jata hai.

Solution: Local or Lazy Import

def functionA():
    from moduleB import functionB
    functionB()

Is tarike se import tabhi hota hai jab function call hota hai — circular import avoid ho jata hai.


🌟 Conclusion

"Aaj humne Python ke import system ko deep dive kiya, jisme humne modules, packages, aur optimization techniques ko samjha. Humne dekha kaise Python imports ko efficiently handle karta hai, aur kaise lazy loading aur sys.modules se performance optimize hota hai."

"Agar aap chahte hain apne code ko aur efficient aur scalable banana, toh imports aur unke optimizations samajhna zaroori hai!"

"Agla video hoga Class Object & OOP in python"

© 2024 DeathCode. All Rights Reserved.