Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

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

In this video course, you’ve learned about the special role of Python’s __init__.py file in defining regular packages and distinguishing them from namespace packages.

You’ve explored how Python executes code within __init__.py upon package import, how to use it to structure namespaces and APIs, and how to handle imports effectively. Additionally, you’ve learned about Python’s underscore naming conventions for non-public symbols and how the __all__ variable can help explicitly define a module’s public interface.

Effectively using __init__.py can help you create well-organized, maintainable, and intuitive packages. By structuring your packages properly, you improve readability, encourage reusability, and simplify package-level initialization.

In this video course, you’ve learned how to:

  • Mark directories as regular packages using __init__.py
  • Define a clear, explicit public API using imports in __init__.py
  • Use underscore prefixes to indicate non-public symbols
  • Control what’s imported with the __all__ variable
  • Understand when and how Python executes code in __init__.py

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

00:00 Congratulations, you’ve made it all the way to the end. You’ve successfully completed this course. Just as a little summary, what did you learn in this course?

00:11 You’ve learned what __init__.py is, what it does, and how you can use it. So with your newfound knowledge, you can now use the dunder init magic to create regular packages with user-friendly interfaces.

00:26 Now to start putting that knowledge into practice, I would recommend the following resources.

00:33 So firstly, there is the written tutorial that underlies this video course. Then there is some more detail on modules and packages. Then what is a Python namespace package and what is it for?

00:46 Remember, __init__.py creates a regular package, which is different from a namespace package. You also implemented some relative imports. There is some more information here about absolute versus relative imports.

01:02 And then if you’re considering creating packages, then you might want to have a look at the Python Application Layouts reference, because you’ll want to understand how to structure your application.

01:15 And then Python and TOML files, that you’ll probably need if you want to actually publish your packages. And then finally, if you hadn’t heard about dunder before, there is a link to a video course that explains single and double underscore naming conventions in Python. Thank you for giving me your time and attention. I hope you’ve enjoyed taking this course as much as I’ve enjoyed making it.

01:43 So all the best and see you soon.

Become a Member to join the conversation.