Python Quest

Track 10 · Lesson 7 of 12

Packages and the main guard

Group modules into packages with __init__.py, import from them with absolute and relative imports, and use if __name__ == "__main__" so a file works both as a script and as a module.

About 45 minIntermediate3 exercises

You will be able to

  • Organise modules into a package folder and import from it with dotted names
  • Explain what __init__.py does and what importing a package does and does not load
  • Read relative imports such as from .pricing import apply_vat inside a package
  • Use __name__ and the if __name__ == "__main__" guard so importing a file has no side effects

Included with a Formula Quest membership

It has 3 graded exercises with feedback, hints and solutions. Python Quest is included in Formula Quest memberships that cover Python; membership and billing are managed on Formula Quest.

View Formula Quest memberships

Tracks 1 and 2, the first lesson of every track, the playground and the dataset library are free.