From script to project
Interviewers want to see that you can structure code into packages, understand imports and keep environments clean and reproducible.
- ImportsModules, packages and sys.path.
- Structure__main__ guard, __init__ and circular imports.
- Environmentsvenv, pip, pyproject.toml and lock files.
Writing a script is easy; organising a real project is where many candidates struggle. These questions check that you understand how imports work, how to structure packages, and how to keep dependencies reproducible across laptops, CI and production.
1. What is the difference between a module and a package?
A module is a single .py file. A package is a folder of modules, usually with an __init__.py that runs on import and can expose a clean public API. Packages let you organise code as app/services/payments.py and import it as app.services.payments.
shop/
├── pyproject.toml
└── src/shop/
├── __init__.py # from .cart import Cart (public API)
├── cart.py
└── payments/
├── __init__.py
└── upi.py2. What does if __name__ == "__main__": do?
Every module has __name__. When a file is run directly it is "__main__"; when imported it is the module's name. The guard lets a file act as both an importable module and a script, without running the script part on import (important for tests and multiprocessing).
def main():
print("Running report...")
if __name__ == "__main__":
main() # only when run as: python report.py3. How does Python find modules when you import them?
It checks sys.modules (already imported — modules load only once), then searches sys.path: the script's folder, PYTHONPATH, the standard library and installed site-packages. That is why a local file named random.py can shadow the standard library module.
4. Absolute vs relative imports?
Absolute imports name the full path (from shop.payments import upi) and are clearer — the default recommendation. Relative imports (from .payments import upi) are fine inside a package to refer to sibling modules. Avoid from module import *: it hides where names come from.
5. How do you fix a circular import?
Two modules importing each other at top level can fail because one is only half-initialised. Fixes: move shared code into a third module, import inside the function that needs it, or import the module rather than names from it. A circular import usually signals a design problem worth fixing.
6. Why use a virtual environment?
A virtual environment is an isolated folder with its own Python packages, so each project has its own versions and does not break others or the system Python. Create one per project and never commit it to Git.
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install fastapi==0.115.*
pip freeze > requirements.txt7. What is pyproject.toml and why is it replacing setup.py?
pyproject.toml is the standard configuration file for Python projects: metadata, dependencies, build system and tool settings (pytest, Ruff, mypy) in one place. Modern tools such as uv, Poetry and Hatch use it, together with a lock file for exact, reproducible installs.
[project]
name = "placement-portal"
version = "1.2.0"
requires-python = ">=3.12"
dependencies = ["fastapi>=0.115", "sqlalchemy>=2.0"]
[tool.pytest.ini_options]
testpaths = ["tests"]8. How do you keep production installs reproducible?
Pin exact versions with a lock file (uv.lock, poetry.lock or a fully pinned requirements.txt), install from it in CI and Docker images, upgrade dependencies deliberately, and scan them for known vulnerabilities with tools such as pip-audit.
Remember
Key points
- Modules load once
Cached in sys.modules.
- Use the __main__ guard
Scripts stay importable and testable.
- One venv per project
Never install project packages globally.
- Lock your dependencies
Same versions in dev, CI and production.
Interview drill
Follow-up questions
What the interviewer asks after your first answer. Turn on practice mode, answer each one aloud, then reveal.
-
1 What is __init__.py for if namespace packages exist? Intermediate
Folders without it can still be imported as namespace packages, but
__init__.pymarks a regular package, runs initialisation code and defines the public API. -
2 What does __all__ do? Intermediate
It lists the names exported by
from module import *and documents the public API. -
3 How do you reload a module after editing it? Basic
importlib.reload(module)— useful in notebooks; in applications, restart the process. -
4 pip install -e . — what does editable mode mean? Intermediate
The package is installed as a link to your source folder, so code changes take effect without reinstalling.
-
5 How would you share internal code between several services? Advanced
Package it as a versioned library published to a private index, or use a monorepo with a workspace tool; avoid copy-pasting modules.
-
6 Where should secrets such as API keys go? Basic
In environment variables or a secrets manager, read with
os.environ; keep.envfiles out of Git.