Module lessons (4/4)
import and modules
import, from ... import, aliases with as, multi-file layout, and if __name__ == "__main__".
What you'll learn in this lesson
- Choose wisely between
import modulo,import modulo as aliasandfrom modulo import nome. - Understand how Python loads, caches (
sys.modules), and resolves names in namespaces. - Structure multiple files and packages with
__init__.py. - Separate reusable code from direct execution using
if __name__ == "__main__":.
Overview
Each .py file in Python is a module: it defines an isolated namespace where functions, classes and variables live without polluting the other project files.
At the first instruction import, Python searches for the module in the configured paths (sys.path), compiles/executes it only once and saves its reference in the global cache sys.modules: all subsequent import reuse the same object already loaded in RAM.
Sections covered in this lesson
- 11. Explicit namespaces vs selective imports
- 22. Conventional aliases and compatibility
- 33. Organize a multi-file and package project
- 44. Protect the entrypoint with
if __name__ == "__main__": - 5Check for understanding
- 6Test yourself
Unlock "import and modules" and the full Python Course curriculum
import, from ... import, aliases with as, multi-file layout, and if __name__ == "__main__".
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Python Course plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.