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
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
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.
Python Modules & Packages Next