Back to search

The `__name__` guard, package structure, and relative vs absolute imports

The `if __name__ == "__main__"` guard separates script execution from import behavior, and package imports can be relative or absolute depending on context.

Python Medium Theory

The `__name__` guard, package structure, and relative vs absolute imports

The `if __name__ == "__main__"` guard separates script execution from import behavior, and package imports can be relative or absolute depending on context.

  • `__name__` changes when imported
  • Absolute imports are usually clearer
  • Relative imports are useful inside packages

The `__name__` guard, package structure, and relative vs absolute imports