Modules for Pythons
Modules are Python files that hold reusable code, and packages are folders of modules. The import statement loads them, and the standard library offers modules such as os, pathlib, json, datetime and collections. pip installs third-party packages, ideally into a virtual environment made with venv.
Stage: Functions and Modules · Part of the Python roadmap · Last reviewed 19 November 2026
What to learn in Modules
- Import a module and use from-import with aliases
- Create your own package with an __init__.py file
- Use if __name__ == "__main__" to separate script and import behaviour
- Create a virtual environment and install packages with pip
- Use pathlib, datetime and collections for everyday tasks
Modules interview questions
Why use a virtual environment in Python?
A virtual environment gives each project its own installed packages, so different projects can use different versions without conflict and without touching the system Python. Create one with python -m venv and record dependencies in requirements.txt.
What does if __name__ == "__main__" do?
When a file is run directly, its __name__ is set to "__main__", but when imported it is the module name. This check lets a file act as both a script and an importable module, running example code only when executed directly.
Where Modules fits in the Python roadmap
Modules belongs to the Functions and Modules stage (beginner level), which takes about 28 hours. Organise code.
Other units in this stage
Back to the full Python roadmap