Need Assistance?

support@tutorialshub.in

Functions and Modules

Modules and imports

ADVERTISEMENT
Python Free Lesson
5 min read
ADVERTISEMENT

Grouping modules into packages

A package organizes related modules into a directory structure. This becomes important when an application grows beyond a few files and needs clear boundaries between features.

The __name__ check

When a file is run directly, Python gives it the special name __main__. When it is imported, its module name is used instead. The common if __name__ == '__main__': pattern lets you keep command-line startup behavior separate from reusable definitions.

Avoid circular dependencies

Packages should have a sensible dependency direction. If module A imports B while B imports A, initialization can become confusing. Moving shared concepts into a lower-level module often removes the cycle.

Practice: build a small calculator package and a main entry point that imports its functions without executing command-line code during import.

ADVERTISEMENT