Skip to main content
Module 7 · Lesson 4 of 428/36 in the course~14 min
Module lessons (4/4)

import and modules

Lesson preview5 hands-on exercises readyPython Course · Module 7 — Classes 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 alias and from 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

  1. 11. Explicit namespaces vs selective imports
  2. 22. Conventional aliases and compatibility
  3. 33. Organize a multi-file and package project
  4. 44. Protect the entrypoint with if __name__ == "__main__":
  5. 5Check for understanding
  6. 6Test yourself
Pro Lesson · Module 7 — Classes and modules

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.