Skip to content

Commit 24d6394

Browse files
committed
docs(notes): add explanation of __init__.py
1 parent 8800753 commit 24d6394

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

_posts/en/2025-01-11-notes-en.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ translated: false
99

1010
These notes are primarily generated by AI chatbots. I used them to summarize key points and will walk through them to enhance my understanding.
1111

12+
* [Why Python Needs `__init__.py`](/notes/2025-07-26-init-py-explanation-en)
1213
* [Fixing macOS File Permission Errors](/notes/2025-07-26-macos-error-fix-en)
1314
* [AI vs. Code: Limitations and Flexibility](/notes/2025-07-24-ai-vs-code-flexibility-en)
1415
* [Building Genuine Tech Relationships](/notes/2025-07-24-building-genuine-relationships-en)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: Why Python Needs `__init__.py`
3+
lang: en
4+
layout: post
5+
audio: false
6+
translated: false
7+
generated: true
8+
---
9+
10+
### Correcting the Typo
11+
It looks like you meant `__init__.py` (with "init" short for "initialization") rather than `__int__.py`. If that's not the case, feel free to clarify! Assuming it is `__init__.py`, here's the explanation.
12+
13+
### Why Python Needs `__init__.py` in a Directory
14+
In Python, a directory isn't automatically treated as a module or package just because it contains Python files. To organize code into reusable packages (which are essentially directories of related modules), Python requires an `__init__.py` file in that directory. Here's why:
15+
16+
- **Marking the Directory as a Package**: The presence of `__init__.py` tells Python that the directory should be considered a package. This allows you to import modules from it using dotted notation, like `import mypackage.mymodule`. Without it (in standard cases), Python would treat the directory as a regular folder and not allow imports from it.
17+
18+
- **Preventing Name Conflicts**: It avoids accidental shadowing of modules. For example, if you have a directory named `string` (a common name), without `__init__.py`, it might unintentionally hide Python's built-in `string` module in the import path.
19+
20+
- **Initialization and Customization**: The file can be empty (which is common for simple packages), but it can also contain code that runs when the package is imported. This is useful for setup tasks, like importing submodules automatically. Additionally, you can define a special variable called `__all__` in `__init__.py` to control what gets imported when someone does `from mypackage import *` (e.g., `__all__ = ['module1', 'module2']`).
21+
22+
### Historical and Version-Specific Context
23+
- This requirement has been part of Python since its early days to keep the module system explicit and organized.
24+
- Starting with Python 3.3, "namespace packages" were introduced, which don't require `__init__.py`. These allow packages to be split across multiple directories (e.g., for distribution purposes) without the file, using tools like `pkgutil` or `importlib`. However, this is considered an advanced feature, and for most everyday use cases, `__init__.py` is still recommended and widely used for regular packages.
25+
26+
If you're working with Python 3.3+, you can experiment with namespace packages, but sticking with `__init__.py` is simpler and more straightforward for beginners.
27+
28+
[Python Tutorial: Modules](https://docs.python.org/3/tutorial/modules.html#packages)

original/2025-01-11-notes-en.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ translated: false
99

1010
These notes are primarily generated by AI chatbots. I used them to summarize key points and will walk through them to enhance my understanding.
1111

12+
* [Why Python Needs `__init__.py`](/notes/2025-07-26-init-py-explanation-en)
1213
* [Fixing macOS File Permission Errors](/notes/2025-07-26-macos-error-fix-en)
1314
* [AI vs. Code: Limitations and Flexibility](/notes/2025-07-24-ai-vs-code-flexibility-en)
1415
* [Building Genuine Tech Relationships](/notes/2025-07-24-building-genuine-relationships-en)

0 commit comments

Comments
 (0)