Modules and Packages in Python - Interview Questions and Answers
A module in Python is a file containing Python code (functions, classes, variables) that can be imported and used in other Python programs. It helps in code reuse and organization.
To create a module, save a Python file with a .py
extension (e.g., mymodule.py
).
To import it:
import mymodule # Importing the entire module
from mymodule import my_function # Importing a specific function
A module is a single Python file, whereas a package is a collection of modules organized in directories containing an __init__.py
file (to be treated as a package).
It makes a directory a Python package and can also initialize package-level variables or execute startup code.
Some common built-in modules are:
os
(Operating system interactions)sys
(System-specific parameters)math
(Mathematical functions)random
(Random number generation)datetime
(Date and time operations)
Use the dir()
function:
import math
print(dir(math)) # Lists all attributes and functions in the math module
Use importlib.reload()
:
import importlib
import mymodule
importlib.reload(mymodule)
import module
imports the entire module and requires calling functions withmodule.function()
.from module import *
imports all functions directly but may cause namespace conflicts.
Use importlib
:
import importlib
module_name = "math"
math_module = importlib.import_module(module_name)
print(math_module.sqrt(16)) # Output: 4.0
Python searches for modules in the following order:
- The current working directory
- Directories listed in
sys.path
- Installed site-packages
Use the __file__
attribute:
import os
print(os.__file__) # Shows the location of the os module
Use as
:
import numpy as np
A namespace is a mapping between variable names and objects, used to avoid naming conflicts.
To handle circular imports:
- Use lazy imports (import inside a function)
- Use
import module_name
instead offrom module import function
- Break dependencies by restructuring code
Use pip
:
pip install requests
- Use packages (
__init__.py
) to group related modules - Follow a directory structure like:
myproject/
├── main.py
├── mypackage/
│ ├── __init__.py
│ ├── module1.py
│ ├── module2.py
Create a file mymodule.py
:
def greet(name):
return f"Hello, {name}!"
Import it in another script:
import mymodule
print(mymodule.greet("Kartik")) # Output: Hello, Kartik!
Use sys.path.append()
:
import sys
sys.path.append('/path/to/module_directory')
import mymodule
Absolute Import: Uses the full path:
from mypackage import module1
Relative Import: Uses dot notation (.
for current, ..
for parent):
from . import module1
Use pip list
:
pip list
No, self-importing a module results in an ImportError
.
Yes, using if __name__ == "__main__":
to execute only when run as a script.
It creates a circular import error unless handled properly with delayed imports.
Yes, using:
if __name__ == "__main__":
print("Module is running as a script")
Use setuptools
:
python setup.py sdist
Namespace packages allow multiple directories to contribute to the same package without an __init__.py
file.
from math import sqrt, pow
By using the ctypes
or Cython
library.
Using threading or multiprocessing.
Use virtual environments (venv
).
If you delete a module file after importing it, the module remains available in memory until the Python session ends. However, you won’t be able to reload it unless you restore the file or restart the interpreter.
Example:
import mymodule
import os
os.remove("mymodule.py") # Deleting the module file
mymodule.greet("Alice") # Still works because it’s in memory
But trying to reload it will cause an error:
import importlib
importlib.reload(mymodule) # Error: ModuleNotFoundError
Yes, but only if they are in different directories that are not in the same sys.path
. If they are in the same path, Python will import the first matching module it finds, which can cause conflicts.
Solution: Use virtual environments or package namespaces to avoid naming conflicts.
You can use importlib.reload()
:
import importlib
import mymodule
importlib.reload(mymodule) # Reloads the module without restarting Python
However, if the module imports other modules, those won’t be reloaded unless you reload them explicitly.
If a module has execution code, importing it runs that code. To avoid execution:
- Use
if __name__ == "__main__":
to separate execution code from definitions. - Use lazy loading with
importlib
:
import importlib
mymodule = importlib.import_module("mymodule")
You can create dynamic aliases using globals()
or importlib
:
import importlib
alias_name = "m"
globals()[alias_name] = importlib.import_module("math")
print(m.sqrt(16)) # Output: 4.0
This allows flexible module loading in dynamic applications.
The best practice is to use virtual environments:
1. Create a virtual environment:
python -m venv myenv
2. Activate it:
source myenv/bin/activate # Linux/macOS
myenv\Scripts\activate # Windows
3. Install dependencies:
pip install -r requirements.txt
Yes, Python supports C and C++ extensions using .so
or .pyd
files. You can write a C module and import it in Python using ctypes
or Cython
.
Example using ctypes
:
import ctypes
mylib = ctypes.CDLL("mylib.so") # Importing a shared C library
You can use the inspect
module:
import inspect
import math
def log_imports(module):
functions = [name for name, _ in inspect.getmembers(module, inspect.isfunction)]
print(f"Imported functions: {functions}")
log_imports(math)
This helps in debugging and tracking module usage.
Python caches imported modules in sys.modules
to avoid redundant loading. If a module is imported again, Python retrieves it from the cache instead of reloading it.
Example:
import sys
import mymodule
print(sys.modules["mymodule"]) # Shows the cached module location
To force a fresh import, use importlib.reload(mymodule)
.
- Use namespace packages (
mypackage.module1
,mypackage.module2
). - Use absolute imports (avoid
from module import *
). - Use virtual environments to isolate dependencies.
Use the asyncio
module with run_in_executor()
:
import asyncio
import importlib
async def import_async(module_name):
loop = asyncio.get_running_loop()
return await loop.run_in_executor(None, importlib.import_module, module_name)
async def main():
math_module = await import_async("math")
print(math_module.sqrt(16))
asyncio.run(main())
You can import modules the same way as in regular Python, but for local modules, use:
import sys
sys.path.append("/path/to/module")
import mymodule
Or use:
%run mymodule.py
Define __all__
in the module:
__all__ = ["allowed_function"]
Only functions listed in __all__
can be imported.
pkgutil
: Used for extending and manipulating Python packages dynamically.pkg_resources
: Helps manage package dependencies at runtime.
Example:
import pkgutil
print(list(pkgutil.iter_modules())) # Lists available modules
Yes, Python allows importing directly from a ZIP archive:
import sys
sys.path.append("modules.zip")
import mymodule # Imports from the ZIP file
Use pip install
with a version specifier:
pip install requests==2.25.1
Use pip uninstall
:
pip uninstall requests
Use importlib.util.find_spec()
:
import importlib.util
def check_module(module_name):
spec = importlib.util.find_spec(module_name)
return spec is not None
print(check_module("requests")) # Output: True or False
Example API wrapper module (weather.py
):
import requests
def get_weather(city):
url = f"https://api.weatherapi.com/v1/current.json?key=YOUR_API_KEY&q={city}"
response = requests.get(url)
return response.json()
# In another script:
# from weather import get_weather
# print(get_weather("London"))
.pyc
files are compiled Python bytecode files created when a module is imported.- They are stored in the
__pycache__
directory and speed up loading. - They update only when the corresponding
.py
file changes.
To disable .pyc
generation:
PYTHONDONTWRITEBYTECODE=1 python script.py
Tutorials
Random Blogs
- Top 10 Blogs of Digital Marketing you Must Follow
- Understanding OLTP vs OLAP Databases: How SQL Handles Query Optimization
- Mastering Python in 2025: A Complete Roadmap for Beginners
- Datasets for Exploratory Data Analysis for Beginners
- Where to Find Free Datasets for Your Next Machine Learning & Data Science Project
- How to Become a Good Data Scientist ?
- Loan Default Prediction Project Using Machine Learning
- AI in Marketing & Advertising: The Future of AI-Driven Strategies
- SQL Joins Explained: A Complete Guide with Examples
- Top 15 Recommended SEO Tools