Interview Q&A · Python · 15 questions in this topic

Modules, Packages & Virtual Environments — Python Interview Q&A

All Python topics

Imports and the import cache, packages and __init__.py, pip, virtual environments and how a project keeps its dependencies reproducible. Part of Modules, Packages & Virtual Environments · 375 questions in the Python bank.

What is a module in Python?

Answer

A module is simply a single.py file containing Python code — functions, classes, or variables — that can be imported and reused in other files.

When you import a module, Python runs that file's code once (top to bottom) and caches the resulting module object in sys.modules, keyed by its name — that's why importing the same module multiple times in a program doesn't re-execute its code each time.

Example

Example.pypython
# file: shapes.py
def area_of_circle(radius):
    return 3.14159 * radius ** 2
PI = 3.14159
# file: main.py
import shapes
print(shapes.area_of_circle(5)) # 78.53975
print(shapes.PI) # 3.14159

💡 Mentioning that a module is only executed once, and cached in sys.modules on subsequent imports, is a small detail that explains a lot of behavior — including why module-level print statements or side effects only ever run a single time no matter how many files import that module.

What is a package, and how does it differ from a module?

Answer

A package is a directory containing multiple related modules (and possibly sub-packages), traditionally identified by an __init__.py file inside it. A module is a single file; a package is a folder that groups several modules under one importable namespace.

Packages let you organize a larger codebase hierarchically — import mypackage.submodule reflects the actual folder structure on disk, making large projects navigable and preventing naming collisions between unrelated modules.

Example

Example.pypython
myapp/
    __init__.py
    models.py
    utils/
        __init__.py
        formatting.py
        validation.py

# then, in application code:
from myapp.utils.formatting import format_currency
from myapp.models import User

💡 Draw or describe the folder structure explicitly when explaining packages — interviewers want to confirm you understand that Python's import paths mirror the actual filesystem layout, not just an abstract naming convention.

What is the difference between import module and from module import name?

Answer

import module brings in the whole module as a namespace — you access its contents with module.name. from module import name brings a specific name directly into your current namespace, letting you use it unqualified as just name.

import module is generally considered safer for avoiding naming collisions (everything stays namespaced under module.), while from module import name is more concise for names you use frequently — the trade-off is readability/brevity versus namespace clarity and collision safety.

Example

Example.pypython
import math
print(math.sqrt(16)) # namespaced access -- clear where sqrt comes from
from math import sqrt
print(sqrt(16)) # direct access -- more concise, but less explicit about origin
# Collision risk with "from" imports:
from math import pi
# from mymodule import pi # would silently overwrite the previous 'pi'!

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits