Loading video player…

What Does Python's __init__.py Do? (Overview)

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.

Resources mentioned in this lesson:

Download

Course Slides (.pdf)

3.2 MB
Download

Sample Code (.zip)

7.7 KB

00:00 Hey, and welcome to this course where you can get to grips with that maybe bizarre looking __init__.py file. So first things first, I’ll refer to that file as the dunder init file. That should make things a little bit easier. So what will be your rewards for successfully completing this course?

00:24 Well, firstly, there’ll be dunder magic, of course, like all dunder objects, __init__.py has some built-in magic, and you’ll find out exactly what that is.

00:35 Now, if you don’t believe in magic, but if learning about the inner workings of Python appeals to you, then you’re also in the correct place, because you’ll get your hands dirty with some code, and you’ll see how to implement that magic that you no longer believe in.

00:52 And then finally, and I should warn you, there is a spoiler in this slide, you’ll learn about the relationship between __init__.py and packages.

01:04 In this course, you’ll learn what __init__.py is, what it does, and how you can use it. Is there any useful knowledge you should have before taking this course?

01:16 Well, given that you and I will be creating a package, it’s best that you have a basic understanding of modules and packages. And also, if importing modules is completely new to you, then you might want to brush up on that knowledge a little bit.

01:32 Now, luckily for you, I’ve included some helpful links in the resources section to get you going.

01:38 So in this course, you’ll first spend some time getting to know __init__.py, and then you’ll move on to managing the public interface of your package.

01:49 Now, that’s exciting stuff. Well, at least I think so. So I can’t wait to get going, and I hope to see you in the next lesson.

Become a Member to join the conversation.