PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Modules in Python: import, from ... import, as Aliasing (मॉड्यूल्स: कोड पुन: उपयोग, import और as अलियासिंग) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Python modules encapsulate functions, classes, and variables in `.py` files to promote code reuse and prevent namespace collisions. The import system caches loaded modules in sys.modules and supports selective imports via `from ... import`, aliasing via `as`, and `__all__` controls.
अध्याय सार: पायथन मॉड्यूल्स कोड पुन: उपयोग और नेमस्पेस अलगाव के लिए फंक्शन्स, क्लासेस और वेरिएबल्स को `.py` फाइलों में व्यवस्थित करते हैं। इम्पोर्ट प्रणाली लोड किए गए मॉड्यूल्स को sys.modules में कैश करती है तथा `from ... import`, `as` अलियासिंग और `__all__` नियंत्रण प्रदान करती है।
Modules in Python: import, from ... import, as Aliasing: Detailed Notes & Architecture (मॉड्यूल्स: कोड पुन: उपयोग, import और as अलियासिंग)
1. Module Architecture, Search Path, and sys.modules Caching (मॉड्यूल संरचना, सर्च पाथ और sys.modules कैशिंग)
[EN] Any `.py` file is a Python module whose name is the filename stem. When `import mod` runs, Python checks `sys.modules` first; if cached, it reuses the existing module object without re-executing top-level code. Otherwise, it searches built-in modules and then every directory listed in `sys.path` (current script directory, `PYTHONPATH` environment variable, and `site-packages`). Upon compiling a module, CPython caches bytecode in a `__pycache__` directory as `.pyc` files tagged with the interpreter version (e.g., `mod.cpython-312.pyc`) to speed up subsequent startups.
[HI] कोई भी `.py` फाइल एक पायथन मॉड्यूल होती है। जब `import mod` चलता है, तो पायथन सबसे पहले `sys.modules` डिक्शनरी में देखता है; यदि मॉड्यूल पहले से लोड है तो उसका टॉप-लेवल कोड दोबारा नहीं चलता। कैश में न होने पर पायथन बिल्ट-इन मॉड्यूल्स और फिर `sys.path` की डायरेक्टरीज (स्क्रिप्ट फोल्डर, `PYTHONPATH`, और `site-packages`) में खोजता है। संकलन के बाद CPython बाइटकोड को `__pycache__` फोल्डर में `.pyc` फाइलों के रूप में सहेजता है ताकि अगली बार लोडिंग तेज हो।
2. import module vs from module import name (import module बनाम from module import name)
[EN] `import math` loads the module and binds only the module object `math` into the current namespace, keeping attributes safely prefixed as `math.sqrt`. By contrast, `from math import sqrt` loads the entire module into `sys.modules` just the same, but binds only the local reference `sqrt` directly into the caller's namespace without binding `math`. Using `from mod import var` copies the object reference at import time: if you later rebind `var = 99` locally, `mod.var` remains untouched, and vice versa if `mod.var` is reassigned inside `mod`. Dot notation via `import mod` is generally preferred in large systems because it clearly documents origin and prevents accidental shadowing.
[HI] `import math` मॉड्यूल को लोड करके केवल `math` नाम को वर्तमान नेमस्पेस में जोड़ता है, जिससे नाम टकराते नहीं हैं। वहीं `from math import sqrt` भी पूरा मॉड्यूल लोड करता है, लेकिन वर्तमान स्कोप में केवल `sqrt` को सीधे बाइंड करता है और `math` नाम को उपलब्ध नहीं कराता। ध्यान देने योग्य बात यह है कि `from mod import var` केवल रेफरेंस कॉपी करता है; यदि आप लोकल रूप से `var = 99` कर दें तो मॉड्यूल के भीतर `mod.var` पर असर नहीं पड़ता। बड़े प्रोजेक्ट्स में `import mod` को प्राथमिकता दी जाती है क्योंकि इससे कोड स्पष्ट रहता है।
3. Aliasing with as and Wildcard Import Controls (as कीवर्ड से अलियासिंग और वाइल्डकार्ड इम्पोर्ट)
[EN] The `as` clause assigns an alternative local identifier to an imported module or symbol, such as `import statistics as stats` or `from datetime import datetime as dt`. When an alias is specified, the original name (`statistics`) is NOT bound in the importing namespace and referencing it causes `NameError`. Wildcard syntax `from mod import *` is allowed only at module top level (not inside functions, where it raises `SyntaxError`) and pulls every name not starting with `_` unless the module defines an `__all__` list. Even with `__all__`, wildcard imports obscure symbol origins and confuse static analyzers, so PEP 8 strongly advises against them.
[HI] `as` कीवर्ड किसी मॉड्यूल या फंक्शन को छोटा या वैकल्पिक नाम देने के लिए प्रयुक्त होता है, जैसे `import statistics as stats`। अलियास देने पर मूल नाम (`statistics`) वर्तमान नेमस्पेस में बाइंड नहीं होता, बल्कि केवल अलियास नाम (`stats`) ही उपलब्ध रहता है। वाइल्डकार्ड `from mod import *` केवल मॉड्यूल के टॉप-लेवल पर मान्य है (फंक्शन के अंदर SyntaxError देता है); यह `_` से शुरू न होने वाले सभी नाम या `__all__` लिस्ट में दिए गए नाम इम्पोर्ट करता है। PEP 8 के अनुसार वाइल्डकार्ड इम्पोर्ट से नेमस्पेस प्रदूषण होता है, इसलिए इससे बचना चाहिए।
4. Packages, __init__.py, dir(), and importlib.reload() (पैकेजेस, __init__.py, dir() और importlib.reload())
[EN] A package is a directory grouping related modules; traditional packages include an `__init__.py` file that executes upon first package import, while Python 3.3+ also supports PEP 420 namespace packages without `__init__.py`. Inside packages, relative imports use leading dots (`from . import helper` for same package, `from ..core import db` for parent package). At runtime, `dir(mod)` returns a sorted list of attribute names defined in `mod`, and `mod.__file__` reveals its path on disk. Because repeated imports return the cached entry in `sys.modules`, updating a module interactively requires calling `importlib.reload(mod)` to force re-execution.
[HI] पैकेज संबंधित मॉड्यूल्स का समूह (डायरेक्टरी) होता है; पारंपरिक पैकेज में `__init__.py` फाइल होती है, जबकि पायथन 3.3+ में इसके बिना PEP 420 नेमस्पेस पैकेज भी बन सकते हैं। पैकेजों के भीतर रिलेटिव इम्पोर्ट के लिए डॉट्स (`from . import helper` या `from ..core import db`) प्रयुक्त होते हैं। `dir(mod)` मॉड्यूल में मौजूद सभी नामों की सूची देता है और `mod.__file__` फाइल का पाथ बताता है। चूंकि सामान्य `import` कैश का उपयोग करता है, इसलिए बदले गए मॉड्यूल को दोबारा चलाने के लिए `importlib.reload(mod)` का उपयोग करना पड़ता है।
Syntax, Formula & Technical Example (Modules in Python: import, from ... import, as Aliasing)
import math
import statistics as stats
from math import pi, sqrt as root
nums = [4, 9, 16, 25]
print("pi:", round(pi, 4), "root(16):", root(16))
print("mean:", stats.mean(nums))
print("math cached:", "math" in __import__("sys").modules)
Modules in Python: import, from ... import, as Aliasing Quick Reference & Comparison Table
| Import Statement | Namespace Binding Behavior | Key Exam Fact |
|---|---|---|
| import math | Binds module object 'math' | Access via math.sqrt(); lowest name collision risk |
| import math as m | Binds module under alias 'm' | Name 'math' is NOT bound (NameError if used) |
| from math import sqrt | Binds 'sqrt' directly in caller | Entire math module still loads into sys.modules |
| from mod import * | Imports public names or __all__ | Skips names starting with '_'; invalid inside functions |
Modules in Python: import, from ... import, as Aliasing Top 20 Bilingual MCQs with Answers
Q1. Where does Python cache already-imported module objects so subsequent import statements do not re-execute them?
(पायथन पहले से इम्पोर्ट किए गए मॉड्यूल्स को कहां कैश करता है ताकि बार-बार इम्पोर्ट करने पर वे दोबारा न चलें?)
(A) sys.modules
(B) __pycache__
(C) builtins.__dict__
(D) os.environ
Correct Answer: (A)
[EN] sys.modules is a dictionary mapping module names to already loaded module objects.
[HI] sys.modules एक डिक्शनरी है जो लोड किए गए मॉड्यूल नामों को उनके मॉड्यूल ऑब्जेक्ट्स से जोड़कर कैश रखती है।
Q2. If you run `import numpy as np`, what happens when you subsequently evaluate `numpy.array([1, 2])`?
(यदि आप `import numpy as np` लिखने के बाद `numpy.array([1, 2])` चलाएं, तो क्या होगा?)
(A) It works identically to np.array([1, 2])
(B) NameError: name 'numpy' is not defined
(C) AttributeError is raised
(D) ImportError is raised
Correct Answer: (B)
[EN] Aliasing binds only the alias `np` in the current namespace; `numpy` itself is not bound.
[HI] `as` अलियास देने पर वर्तमान स्कोप में केवल `np` बाइंड होता है, मूल नाम `numpy` उपलब्ध नहीं रहता और NameError देता है।
Q3. When a module defines `__all__ = ['foo', '_bar']`, what does `from mod import *` import?
(यदि किसी मॉड्यूल में `__all__ = ['foo', '_bar']` परिभाषित हो, तो `from mod import *` क्या इम्पोर्ट करेगा?)
(A) SyntaxError because __all__ cannot contain underscore names
(B) Only 'foo' and '_bar'
(C) Every symbol in mod except '_bar'
(D) Only 'foo' because names starting with _ are always skipped
Correct Answer: (B)
[EN] When __all__ is explicitly defined, `from mod import *` imports the exact list of names specified in __all__.
[HI] जब मॉड्यूल में `__all__` परिभाषित होता है, तो `from mod import *` ठीक उन्हीं नामों को इम्पोर्ट करता है जो `__all__` में सूचीबद्ध हैं।
Q4. When `__all__` is NOT defined in a module, which names does `from mod import *` ignore?
(यदि मॉड्यूल में `__all__` परिभाषित न हो, तो `from mod import *` किन नामों को छोड़कर बाकी नाम इम्पोर्ट करता है?)
(A) All integer variables
(B) Names written in ALL CAPS
(C) Names beginning with an underscore (_)
(D) Names containing digits
Correct Answer: (C)
[EN] Without __all__, wildcard imports skip private-by-convention names starting with a leading underscore `_`.
[HI] __all__ के अभाव में वाइल्डकार्ड इम्पोर्ट अंडरस्कोर (`_`) से शुरू होने वाले नामों को छोड़ देता है।
Q5. Which directory in Python 3 stores compiled `.pyc` module bytecode files?
(पायथन 3 में संकलित `.pyc` मॉड्यूल बाइटकोड फाइलें किस डायरेक्टरी में संग्रहीत होती हैं?)
(A) bin
(B) site-packages
(C) __init__
(D) __pycache__
Correct Answer: (D)
[EN] Python 3 places version-tagged `.pyc` files inside a `__pycache__` subdirectory alongside the `.py` files.
[HI] पायथन 3 मॉड्यूल्स की `.pyc` बाइटकोड फाइलों को `__pycache__` फोल्डर में सहेजता है।
Q6. Which standard library function forces an already imported module to be re-read and re-executed from disk?
(पहले से इम्पोर्ट किए गए मॉड्यूल को डिस्क से दोबारा पढ़कर निष्पादित करने के लिए किस फंक्शन का उपयोग होता है?)
(A) sys.refresh()
(B) importlib.reload()
(C) module.rerun()
(D) os.restart()
Correct Answer: (B)
[EN] importlib.reload(module) re-executes a module's code and updates its module dictionary in place.
[HI] importlib.reload(module) पहले से कैश्ड मॉड्यूल को डिस्क से पुनः चलाकर अपडेट करता है।
Q7. What does `from math import sqrt` load into memory compared to `import math`?
(`import math` की तुलना में `from math import sqrt` मेमोरी में क्या लोड करता है?)
(A) It extracts and compiles only the sqrt function from disk
(B) It loads a faster C-only copy outside Python
(C) It still loads and caches the entire math module in sys.modules, but binds only sqrt locally
(D) It avoids adding math to sys.modules
Correct Answer: (C)
[EN] Python always executes and caches the full module in sys.modules; `from ... import` only limits which names enter the caller's namespace.
[HI] पायथन हमेशा पूरा मॉड्यूल sys.modules में लोड करता है; `from ... import` केवल वर्तमान स्कोप में आने वाले नामों को सीमित करता है।
Q8. Which list variable controls the directories Python searches when importing modules?
(मॉड्यूल इम्पोर्ट करते समय पायथन किन डायरेक्टरीज में खोजेगा, यह किस लिस्ट द्वारा नियंत्रित होता है?)
(A) os.path
(B) sys.path
(C) sys.argv
(D) os.listdir
Correct Answer: (B)
[EN] sys.path is the list of directory strings (plus zip files) searched by the import machinery.
[HI] sys.path उन डायरेक्टरीज की सूची है जिनमें पायथन इम्पोर्ट किए जाने वाले मॉड्यूल खोजता है।
Q9. What happens if you place `from math import *` inside a Python 3 function body?
(यदि आप पायथन 3 के किसी फंक्शन के भीतर `from math import *` लिखें तो क्या होगा?)
(A) It succeeds with a DeprecationWarning
(B) It imports all math functions into the function's local scope
(C) SyntaxError: import * only allowed at module level
(D) It imports all math functions into the global scope
Correct Answer: (C)
[EN] Wildcard `import *` is forbidden inside functions in Python 3 because local fast-slot variable optimization requires statically known locals.
[HI] पायथन 3 में फंक्शन के भीतर `import *` लिखना अवैध है और यह SyntaxError उत्पन्न करता है।
Q10. Which environment variable allows users to add extra directories to `sys.path` before Python starts?
(पायथन शुरू होने से पहले `sys.path` में अतिरिक्त डायरेक्टरीज जोड़ने के लिए कौन-सा एनवायरनमेंट वेरिएबल उपयोग होता है?)
(A) PYTHONHOME
(B) PYTHONPATH
(C) PYMOD
(D) PATH_MODULES
Correct Answer: (B)
[EN] Directories listed in the PYTHONPATH environment variable are appended to sys.path at interpreter startup.
[HI] PYTHONPATH एनवायरनमेंट वेरिएबल में दी गई डायरेक्टरीज पायथन शुरू होते ही sys.path में जुड़ जाती हैं।
Q11. What does calling `dir(math)` return?
(`dir(math)` को कॉल करने पर क्या लौटता है?)
(A) The source code of the math module
(B) A dictionary of math values and docstrings
(C) The filesystem directory path containing math
(D) A sorted list of strings naming the attributes and functions in math
Correct Answer: (D)
[EN] dir(obj) returns an alphabetically sorted list of attribute names available on that module or object.
[HI] dir(math) मॉड्यूल में परिभाषित सभी एट्रिब्यूट्स और फंक्शन्स के नामों की क्रमबद्ध सूची लौटाता है।
Q12. Given module `cfg.py` with `LIMIT = 5`, if `main.py` runs `from cfg import LIMIT; LIMIT = 20`, what is `cfg.LIMIT`?
(यदि `cfg.py` में `LIMIT = 5` है और `main.py` में `from cfg import LIMIT; LIMIT = 20` चलाया जाए, तो `cfg.LIMIT` क्या होगा?)
(A) 5 (unchanged, because main.py only rebound its own local name LIMIT)
(B) None
(C) 20 (because integers are shared globally across modules)
(D) AttributeError
Correct Answer: (A)
[EN] Rebinding `LIMIT` in main.py merely changes main.py's local binding; `cfg.LIMIT` still points to 5.
[HI] main.py में `LIMIT = 20` केवल main.py के अपने नाम को बदलता है, `cfg.LIMIT` का मान 5 ही रहता है।
Q13. What file inside a folder traditionally marks that folder as a regular Python package?
(किसी फोल्डर के भीतर कौन-सी फाइल उसे पारंपरिक पायथन पैकेज के रूप में चिह्नित करती है?)
(A) __setup__.py
(B) __init__.py
(C) __mod__.py
(D) __package__.py
Correct Answer: (B)
[EN] An `__init__.py` file marks a directory as a regular Python package and runs initialization code when imported.
[HI] `__init__.py` फाइल किसी डायरेक्टरी को पारंपरिक पायथन पैकेज के रूप में पहचानती है।
Q14. In a package `pkg` containing subpackage `sub`, what does `from .. import utils` inside `pkg/sub/a.py` mean?
(पैकेज `pkg` के सब-पैकेज `sub` की फाइल `pkg/sub/a.py` में `from .. import utils` का क्या अर्थ है?)
(A) Import utils from two levels up above pkg
(B) Import utils from the parent package pkg
(C) Import utils from the current directory sub
(D) SyntaxError because double dots are invalid in Python
Correct Answer: (B)
[EN] A single dot (.) refers to the current package, whereas two dots (..) refer to the immediate parent package.
[HI] सिंगल डॉट (.) वर्तमान पैकेज को और डबल डॉट (..) तुरंत ऊपरी पैरेंट पैकेज को संदर्भित करता है।
Q15. Which dunder attribute on an imported pure-Python module holds the path to its `.py` source file?
(किसी इम्पोर्ट किए गए पायथन मॉड्यूल का कौन-सा डंडर एट्रिब्यूट उसकी `.py` फाइल का पाथ रखता है?)
(A) __path__
(B) __file__
(C) __loc__
(D) __dir__
Correct Answer: (B)
[EN] `module.__file__` stores the filesystem path from which the module was loaded.
[HI] `module.__file__` में उस फाइल का पूरा पाथ होता है जिससे मॉड्यूल लोड किया गया है।
Q16. Which of the following is NOT true about Python modules and imports?
(पायथन मॉड्यूल्स और इम्पोर्ट्स के बारे में निम्न में से कौन-सा कथन सत्य नहीं है?)
(A) `from m import a as b` imports a under the local alias b
(B) Importing the same module three times executes its top-level print statements three times
(C) A single Python file with .py extension acts as a module
(D) os.path is a module while sys.path is a list of search paths
Correct Answer: (B)
[EN] Due to sys.modules caching, top-level code in a module runs only on the very first import unless explicitly reloaded.
[HI] sys.modules कैशिंग के कारण मॉड्यूल का टॉप-लेवल कोड केवल पहली बार इम्पोर्ट होने पर चलता है, बार-बार नहीं।
Q17. Within Modules in Python: import, from ... import, as Aliasing, which import statement directly maps to: "Binds module object 'math'"?
(Modules in Python: import, from ... import, as Aliasing में कौन सा Import Statement सीधे "Binds module object 'math'" से संबंधित है?)
(A) from mod import *
(B) import math as m
(C) from math import sqrt
(D) import math
Correct Answer: (D)
[EN] In Modules in Python: import, from ... import, as Aliasing, 'import math' handles 'Binds module object 'math'' (Access via math.sqrt(); lowest name collision risk).
[HI] Modules in Python: import, from ... import, as Aliasing में 'import math' का कार्य/विवरण 'Binds module object 'math'' (Access via math.sqrt(); lowest name collision risk) है।
Q18. Which option accurately describes 'import math as m' in Modules in Python: import, from ... import, as Aliasing?
(Modules in Python: import, from ... import, as Aliasing के संदर्भ में 'import math as m' की सही विशेषता कौन सी है?)
(A) Binds module under alias 'm'
(B) Binds module object 'math'
(C) Imports public names or __all__
(D) Binds 'sqrt' directly in caller
Correct Answer: (A)
[EN] 'import math as m' provides 'Binds module under alias 'm'' — key point: Name 'math' is NOT bound (NameError if used).
[HI] 'import math as m' का मुख्य विवरण 'Binds module under alias 'm'' है (Name 'math' is NOT bound (NameError if used))।
Q19. In Modules in Python: import, from ... import, as Aliasing, identify the correct parameter for the specification: "Binds 'sqrt' directly in caller".
(Modules in Python: import, from ... import, as Aliasing में "Binds 'sqrt' directly in caller" विवरण किस विकल्प के लिए सही है?)
(A) from math import sqrt
(B) from mod import *
(C) import math as m
(D) import math
Correct Answer: (A)
[EN] 'from math import sqrt' corresponds to 'Binds 'sqrt' directly in caller' (Entire math module still loads into sys.modules).
[HI] 'from math import sqrt' का संबंध 'Binds 'sqrt' directly in caller' (Entire math module still loads into sys.modules) से है।
Q20. Considering Modules in Python: import, from ... import, as Aliasing, what key behavior defines 'from mod import *'?
(Modules in Python: import, from ... import, as Aliasing में 'from mod import *' की मुख्य भूमिका या मान क्या है?)
(A) Binds module under alias 'm'
(B) Binds 'sqrt' directly in caller
(C) Binds module object 'math'
(D) Imports public names or __all__
Correct Answer: (D)
[EN] 'from mod import *' represents 'Imports public names or __all__' (Skips names starting with '_'; invalid inside functions).
[HI] 'from mod import *' का अर्थ/मान 'Imports public names or __all__' है (Skips names starting with '_'; invalid inside functions)।
Modules in Python: import, from ... import, as Aliasing — Exam FAQs
Q1: Why is `from module import *` discouraged in production Python code?
प्रोडक्शन पायथन कोड में `from module import *` के उपयोग से बचने की सलाह क्यों दी जाती है?
[EN] Wildcard imports pull dozens of unknown identifiers into your namespace, silently overwriting existing variables or functions with identical names and making it impossible for humans or linters to see which module defined a given function.
[HI] वाइल्डकार्ड इम्पोर्ट अनगिनत नामों को आपके नेमस्पेस में ले आता है जिससे पुराने नाम चुपचाप ओवरराइट हो सकते हैं और यह पहचानना कठिन हो जाता है कि कौन-सा फंक्शन किस मॉड्यूल से आया है।
Q2: What happens if two modules `a.py` and `b.py` import each other (circular import)?
यदि दो मॉड्यूल `a.py` और `b.py` एक-दूसरे को इम्पोर्ट करें (सर्कुलर इम्पोर्ट), तो क्या होता है?
[EN] When `a` begins importing `b`, `a` is already registered in `sys.modules` as partially initialized. If `b` immediately accesses a function in `a` that hasn't been defined yet at top level, Python raises ImportError or AttributeError; moving imports inside functions or refactoring shared code resolves it.
[HI] जब `a`, `b` को इम्पोर्ट करता है तो `a` आंशिक रूप से `sys.modules` में दर्ज होता है। यदि `b` तुरंत `a` के किसी अपरिभाषित नाम को पढ़े, तो ImportError या AttributeError आता है; इसे फंक्शन के भीतर इम्पोर्ट करके या साझा कोड अलग करके सुलझाया जाता है।
Q3: Does `from math import sqrt` save memory compared to `import math`?
क्या `import math` की तुलना में `from math import sqrt` मेमोरी बचाता है?
[EN] No. In both cases, Python loads and caches the entire `math` module in `sys.modules`. The only difference is which identifier name is bound in your current module's namespace dictionary.
[HI] नहीं, दोनों ही मामलों में पूरा `math` मॉड्यूल `sys.modules` में लोड होता है। अंतर केवल इस बात का है कि वर्तमान नेमस्पेस में किस नाम की बाइंडिंग बनती है।
Post a Comment
Post a Comment