Loading video player…

Introducing __init__.py

Resources mentioned in this lesson:

00:00 So in this lesson, you’ll get a quick introduction into __init__.py before diving into some examples later in the course.

00:10 So what is __init__.py? Well, it ends with .py, so therefore it’s a module, but it starts with two underscores and ends with two underscores, so that’s a dunder module, if you like, so that makes it a magic module. Now, if you want to find out more about the use of underscores in Python, I include a link to a Real Python video course to get you up to speed with that.

00:36 Magic module, yes, well, what the magic is you’ll discover a little bit later in this course. So what does the __init__.py module do? Well, it declares its containing folder as a regular Python package.

00:52 Now, what does that mean? So which folder is the containing folder? What is a regular package or what is a package for that matter? And what goes into the __init__.py module? Well, let me break that down for you. So the containing folder, well, that is the folder that contains the __init__.py file. I think that makes sense.

01:15 And so it’s that folder that becomes a regular package. So imagine you have the following structure. You have a project folder. So I’ve included a forward slash behind the word project to show you that it’s a folder.

01:29 So that folder has a subfolder called package. And within the package folder, there is our __init__.py module. And there are other modules such as main.py.

01:42 So the folder that contains the __init__.py module, that is the package folder. So it’s the containing folder and is therefore the package folder that will become the regular package.

01:57 What is a regular package? Well, if you take a step back and look at the Real Python glossary to see what a package is, well, a package in Python is a directory hierarchy that allows you to organize related modules and subpackages.

02:14 Now, a regular package is a package that has the __init__.py module in its package folder. Now, you might discover there’s another type of package called a namespace package.

02:26 And that one doesn’t have the __init__.py file in its package folder. Now, given that in this course you’re looking at the __init__.py module, and given that namespace packages don’t have that, namespace packages clearly aren’t part of the scope of this course.

02:43 But I do include a link in the resource section if you want to find out more about namespace packages.

02:51 So what goes into the __init__.py module? Well, it’s a magic module. And what does that mean? Well, it means that the contents of that module are easily available to other modules in the package.

03:04 So therefore, that makes it a perfect spot to stick package-level functions, such as helper functions or constants and variables for settings for your package, for example. And that also makes it the ideal place to stick metadata, for example, version of your package or the author or the name, etc.

03:27 Now, the other part of the magic is that the contents are also easily available to package users. So therefore, this is also the ideal spot to stick imports from package or subpackages to create a coherent namespace. And you’ll see an example of that later in the course.

03:47 When is the __init__.py module executed? Well, it’s executed the first time the package is imported and only the first time. Well, with all that knowledge and theory under your belt, I think you’re ready to start tackling a real-life example in the next lesson.

Become a Member to join the conversation.