Skip to main content

What are __init__.py and __main__ used for?

Entry Python
Quick Answer __init__.py makes a directory a Python package (can be empty). Controls what gets imported with from package import *. __main__.py lets a package be run directly: python -m mypackage. if __name__ == "__main__": guard runs code only when script is executed directly (not when imported as a module). Essential for making scripts work both as executables and importable modules.

Answer

__init__.py marks a folder as a package.
__main__ allows a script to run directly using main block.
S
SugharaIQ Editorial Team Verified Answer

This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.

Want to bookmark, take notes, or join discussions?

Sign in to access all features and personalize your learning experience.

Sign In Create Account

Source: SugharaIQ

Ready to level up? Start Practice