1.3.1 · HinglishPython Intermediate

Modules — import, from…import, as aliasing

1,791 words8 min readRead in English

1.3.1 · Coding › Python Intermediate


Modules KYUN exist karte hain?


import par ACTUALLY kya hota hai

Figure — Modules — import, from…import, as aliasing

KAISE import karein — char forms

Form 1: import module

import math
print(math.pi)        # 3.14159...
print(math.sqrt(16))  # 4.0

Form 2: from module import name

from math import pi, sqrt
print(pi)        # prefix ki zaroorat nahi
print(sqrt(16))  # 4.0

Form 3: import module as alias

import numpy as np
np.array([1, 2, 3])

Form 4: from module import name as alias

from datetime import datetime as dt
now = dt.now()

Ye clash avoid karne ke liye use hota hai (yahan module datetime aur class datetime ka naam ek hi hai).


Python KAHAN dekhta hai? (sys.path)


Worked examples


Recall Feynman: ek 12-saal ke bachche ko samjhao (click karke kholein)

Socho tumhare toy box mein ek calculator toy hai (math). Tum keh sakte ho "toy box, apna calculator do" (import math, phir math.sqrt) — tum apne har toy se pehle "toy box ka" kehte ho taaki apne toys ke saath mix na ho. Ya tum calculator bahar nikalke apni desk pe rakh sakte ho (from math import sqrt) aur seedha pakad sakte ho — lekin dhyan raho, agar tumhare desk pe pehle se koi cheez "calculator" naam ki thi, to nayi cheez usse dhak degi! Aur as bas toy pe ek nickname sticker lagana hai taaki tum use kuch chhota bol sako.


Flashcards

Python module kya hota hai?
Ek single .py file jisme reusable code (functions, classes, variables) hota hai.
Module aur package mein kya fark hai?
Module ek .py file hoti hai; package ek folder hota hai modules ka jisme __init__.py hoti hai.
import math ke baad sqrt kaise call karein?
math.sqrt(...) — tumhe math. prefix use karna hoga.
from math import sqrt ke baad sqrt kaise call karein?
Bas sqrt(...) — naam tumhare namespace mein copy ho gaya, koi prefix nahi.
Import mein as kya karta hai?
Imported module/naam ko ek alias (nickname) deta hai — wahi object, chhota label.
from module import * kyun discourage kiya jaata hai?
Ye sab public names tumhare namespace mein dump kar deta hai, origins chhupa deta hai aur tumhare apne variables ko silently overwrite kar sakta hai.
Repeated imports par Python ek module ka top-level code kitni baar execute karta hai?
Sirf ek baar — baad ke imports sys.modules cache se serve hote hain.
Python import ke liye module kahan dhundta hai?
sys.path mein listed folders mein (script dir, PYTHONPATH, phir standard/installed libs), pehla match jeet jaata hai.
Apni file ka naam random.py rakhne se import random kyun toot jaata hai?
Tumhari local file sys.path par pehle milti hai aur real standard-library module ko shadow kar deti hai.
Modules ke context mein namespace kya hota hai?
Namen ka container; import module module ke naamon ko uske prefix ke peeche rakhta hai taaki wo tumhare se clash na karein.

Connections

  • Python Functions — modules zyaadatar reusable functions bundle karte hain.
  • Namespaces and Scope — import fundamentally namespaces mein naam bind karne ke baare mein hai.
  • sys and os modulessys.path aur sys.modules import resolution control karte hain.
  • Packages and __init__.py — ek file se kai files tak scale karna.
  • Virtual Environments and pip — third-party modules sys.path par kaise aate hain.
  • DRY Principle — wo design idea jo modules ko motivate karta hai.

Concept Map

motivates

grouped into

provides

runs file once

has form

has form

has form

has form

keeps

can cause

shortens name

avoids

DRY principle

Module = one py file

Package = folder + init

Namespace

import statement

import module

from module import name

import module as alias

from module import name as alias

sys.modules cache

Name clash risk