What Does Python's __init__.py Do?

Steven Loyens
Steven Loyens 10 Lessons 32m Mar 03, 2026 basics python

Python’s special __init__.py file marks a directory as a regular Python package and allows you to import its modules. This file runs automatically the first time you import its containing package. You can use it to initialize package-level variables, define functions or classes, and structure the package’s namespace clearly for users.

By the end of this video course, you’ll understand that:

  • A directory without an __init__.py file becomes a namespace package, which behaves differently from a regular package and may cause slower imports.
  • You can use __init__.py to explicitly define a package’s public API by importing specific modules or functions into the package namespace.
  • The Python convention of using leading underscores helps indicate to users which objects are intended as non-public, although this convention can still be bypassed.
  • Code inside __init__.py runs only once during the first import, even if you run the import statement multiple times.

Understanding how to effectively use __init__.py helps you structure your Python packages in a clear, maintainable way, improving usability and namespace management.

What’s Included:

  • 10 Lessons
  • Video Subtitles and Full Transcripts
  • 2 Downloadable Resources
  • Accompanying Text-Based Tutorial
  • Interactive Quiz to Check Your Progress
  • Q&A With Python Experts: Ask a Question
  • Certificate of Completion

Downloadable Resources:

Related Learning Paths:

About Steven Loyens

Steven is an Actuary, Economist and Engineer who found his passion for coding messing about with his Commodore 64 in the eighties. He loves understanding how things work and sharing that knowledge. He's an experienced coach and mentor.

» More about Steven

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

← Browse All Courses