Loading

Quipoin Menu

Learn • Practice • Grow

python / Modules and Packages
interview

Q1. What is a module in Python?
A module is a file containing Python definitions and statements. It allows you to organize code into reusable units. Modules are imported using the import statement. Example:
import math print(math.sqrt(16))

Q2. What is a package in Python?
A package is a way of organizing related modules into a directory hierarchy. A package must contain an __init__.py file (can be empty) to be recognized. Example:
from mypackage import module1

Q3. What are the different ways to import a module?
Common ways: import module, from module import function, from module import *, import module as alias. Using from ... import * is discouraged because it pollutes namespace. Aliasing helps with long module names.

Q4. What is the purpose of __name__ == '__main__'?
This condition checks if the module is being run directly (not imported). It allows code to be executed only when the file is the main program. This is useful for testing and script functionality. When imported, the code inside the if block does not run.

Q5. How does Python find modules and packages?
Python searches for modules in the directories listed in sys.path. This includes the current directory, PYTHONPATH environment variable, and installation-dependent default paths. You can modify sys.path programmatically.