Python Modules & Packages
Python Modules & Packages Interview Questions
What is a module in Python?
A module is a Python file containing Python definitions and statements. It helps organize code into logical units and promotes code reusability. Example: math.py, os.py.
What is a package in Python?
A package is a directory containing Python modules and a special __init__.py file. It helps organize related modules into a hierarchical namespace. Example: numpy, pandas.
What is the purpose of __init__.py file?
The __init__.py file indicates that a directory should be treated as a Python package. It can be empty or contain initialization code. In Python 3.3+, it's optional for implicit namespace packages.
What are the different ways to import modules?
1. import module_name
2. from module_name import function_name
3. from module_name import * (not recommended)
4. import module_name as alias
2. from module_name import function_name
3. from module_name import * (not recommended)
4. import module_name as alias
What is the Python Standard Library?
The Python Standard Library is a collection of modules and packages that come with Python installation, providing functionality for various tasks like file I/O, system calls, mathematics, etc., without requiring external installation.
What is the difference between import and from-import?
import module imports the entire module, accessing items via dot notation. from module import item imports specific items directly into the current namespace, allowing direct use without module prefix.
What is PYTHONPATH?
PYTHONPATH is an environment variable that specifies additional directories where Python looks for modules and packages during import. It's similar to the system PATH variable but for Python modules.
What is pip and how is it used?
pip is Python's package installer. It's used to install, upgrade, and remove Python packages from the Python Package Index (PyPI). Commands: pip install package_name, pip uninstall package_name.
What is __name__ variable in Python?
__name__ is a special variable that holds the name of the module. When a module is run directly, __name__ equals "__main__". When imported, it equals the module's name.
What is the if __name__ == "__main__": construct used for?
This construct allows code to execute only when the module is run directly, not when imported. It's commonly used for testing, demo code, or when a module can act as both a library and a standalone script.
What is a namespace in Python?
A namespace is a mapping from names to objects. Modules and packages create namespaces to avoid naming conflicts. Examples: built-in namespace, global namespace, local namespace.
What are relative imports?
Relative imports use dots to specify location relative to the current module: . (current directory), .. (parent directory). Example: from .submodule import function.
What is the difference between a module and a script?
A module is designed to be imported and used by other code. A script is designed to be run directly. However, a Python file can serve as both using the if __name__ == "__main__": construct.
What is sys.path in Python?
sys.path is a list of directory names where Python searches for modules when an import statement is executed. It includes the current directory, PYTHONPATH directories, and installation-dependent default paths.
How to reload a module in Python?
Use importlib.reload(module_name) in Python 3 or reload(module_name) in Python 2. This is useful during development when you need to reload a module without restarting the interpreter.
What is the Python Package Index (PyPI)?
PyPI is the official third-party software repository for Python. It hosts thousands of packages that can be installed using pip. It's the primary source for Python packages not included in the standard library.
What is a namespace package?
A namespace package is a package without an __init__.py file (Python 3.3+). It allows multiple distributions to contribute to a single namespace, useful for large projects split across multiple directories.
What is the dir() function used for with modules?
The dir(module_name) function returns a sorted list of names defined in a module. Without arguments, it returns names in the current local scope. Useful for exploring module contents.
What are built-in modules in Python?
Built-in modules are compiled into the Python interpreter and are always available without explicit import. Examples: sys, builtins. They can be imported like regular modules.
How to create a Python package?
1. Create a directory for the package
2. Add an __init__.py file (can be empty)
3. Add Python module files (.py)
4. Optionally add a setup.py for distribution
5. Organize sub-packages in subdirectories with their own __init__.py
2. Add an __init__.py file (can be empty)
3. Add Python module files (.py)
4. Optionally add a setup.py for distribution
5. Organize sub-packages in subdirectories with their own __init__.py
Fill in the blanks
After import json, the module is bound in your namespace under the name .
json — Plain import uses the module name as the local variable.
pip downloads packages from the official repository called .
PyPI (Python Package Index).
When a .py file is executed directly, __name__ equals .
"__main__" — When imported, __name__ is the module’s dotted name instead.
A directory is traditionally treated as a package if it contains (possibly empty).
__init__.py — Namespace packages (PEP 420) can omit it under certain layouts.
Python’s module search path is the list .
sys.path — Includes the script directory, PYTHONPATH entries, and standard library locations.
To reload a module object m in Python 3, call .
importlib.reload(m) — Handy in interactive development; avoid relying on it in production flows.
In from . import utils, the leading dot means a import.
relative — One dot is the current package; two dots (..) refer to the parent.
The environment variable adds directories searched before imports.
PYTHONPATH — Colon-separated on Unix, semicolon-separated on Windows.
The CLI tool used to install third-party distributions is .
pip — Invoked as pip install / pip uninstall etc.
For from mypkg import *, public names are limited by when that list is defined.
__all__ — If absent, behavior falls back to names not starting with an underscore.
Names intended as internal begin with and are skipped by default in star-imports.
A single underscore prefix (convention for “internal use”).
Python 3.3+ allows packages spread across multiple directories without a single __init__.py.
namespace — Described in PEP 420.
Running python -m pip executes pip as a module.
__main__ — Same mechanism as python -m pytest, python -m venv, etc.
Installations from pip usually live under the directory in an environment.
site-packages — Where most third-party code is placed.
dir(some_module) returns names defined in that module’s .
namespace — Mapping from identifiers to objects (plus some dunder attributes).
The special attribute holds the path to the loaded module file.
__file__ — May point to a .py source or extension module loader path.
Inside package modules, records which package the module belongs to.
__package__ — Empty string or None for top-level modules (depending on context).
To remove an installed package, you run pip pkg_name.
uninstall — Often paired with pip show to verify metadata.
Nested imports such as import os.path use names split by dots.
dotted — Each segment resolves a submodule or package attribute.
A venv isolates project from the global interpreter.
dependencies — Keeps site-packages separate per project.
Note: Understanding modules and packages is crucial for writing maintainable, reusable Python code. Modules help organize code within files, while packages organize modules within directories. The import system and packaging tools are fundamental to Python's ecosystem and code sharing.