Is __init__.py
file is a module ? I am learning python and come across packages and modules and i have one doubt that weather __init__.py
file can be consider as module Since every python file is module so can we consider __init__.py
as module ?
Asked
Active
Viewed 60 times
0

timgeb
- 76,762
- 20
- 123
- 145

Divesh Sankhla
- 53
- 2
- 13
-
I would say so. Why are you asking? – timgeb Sep 09 '18 at 10:26
-
Actually i have confusion regarding this. – Divesh Sankhla Sep 09 '18 at 10:29
-
so if i consider this as module so can i import this ?? – Divesh Sankhla Sep 09 '18 at 10:30
-
It's actually an interesting question, but knowing whether `__init__.py` is a module has exactly 0 practical benefits. It really doesn't matter one bit. – Aran-Fey Sep 09 '18 at 10:31
-
Well, did you try? – timgeb Sep 09 '18 at 10:31
-
I'd consider the directory where `__init__.py` is to be the module. As long as it contains an `__init__.py` file it can be imported. – Djib2011 Sep 09 '18 at 12:24
-
1@Djib2011: I would think a directory that contains an `__init__.py` file could not be considered a module. It's a *package*, and as such it can be imported, but `import package` is basically syntactic sugar for `import package.__init__ as package`, right? – Daniel Pryden Sep 09 '18 at 13:06
-
@DanielPryden True. I meant that, the way I see it, the `__init__.py` is meant to be a handle for a package and not to be used individually. – Djib2011 Sep 09 '18 at 15:12
1 Answers
2
According to the official glossary a module is
An object that serves as an organizational unit of Python code. Modules have a namespace containing arbitrary Python objects. Modules are loaded into Python by the process of importing.
Which (imho) borders on gibberish for saying "module
objects or files with Python code."
Since even an empty __init__.py
file contains valid Python code, and you can import it via import __init__
, it would technically be considered a module.
I don't know how this knowledge could be useful.

timgeb
- 76,762
- 20
- 123
- 145