0

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 ?

8
  • I would say so. Why are you asking?
    – timgeb
    Commented Sep 9, 2018 at 10:26
  • Actually i have confusion regarding this. Commented Sep 9, 2018 at 10:29
  • so if i consider this as module so can i import this ?? Commented Sep 9, 2018 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
    Commented Sep 9, 2018 at 10:31
  • 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? Commented Sep 9, 2018 at 13:06

1 Answer 1

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.