0

I am running python 3.4 on the main.py file in the same directory. /root directory is not in python path. It is simply the current directory that the script is executing in. All pycache folders were deleted after each test

So why exactly is __init__.py important? I thought it was necessary as stated in this post:

What is __init__.py for?

If you remove the init.py file, Python will no longer look for submodules inside that directory, so attempts to import the module will fail.

Right now, it seems to me that __init__.py is nothing more than an optional constructor where we do housekeeping and other optional things like specifying the "all" variable, etc. But not a critical item to have.

Image showing the results of the test:

enter image description here

What is going on here?

Community
  • 1
  • 1
AlanSTACK
  • 4,735
  • 3
  • 33
  • 80
  • 1
    [*Namespace packages may have no physical representation, and specifically are not like a regular package because they have no `__init__.py` file.*](https://docs.python.org/3/glossary.html#term-namespace-package) – vaultah Feb 02 '16 at 11:48
  • 2
    I am a giant noobie, and after reading the link you posted, I am not particularly enlightened. What exactly is the difference between namespace package and an ordinary package? Do I have to upload it to pypi for it to be a "package". The word "package" is so overused in python and in differing contexts referring to different things. Could you be a bit more specific as to help us rookies? @vaultah – AlanSTACK Feb 02 '16 at 11:51
  • Well, you can read [Namespace vs regular package](http://stackoverflow.com/q/21819649/2301450) and PEP 420 – vaultah Feb 02 '16 at 12:06
  • 1
    I have read that too, and all the explanations seems to reiterate the same mantra >Namespace packages may have no physical representation >namespace package is a virtual package whose contents can be distributed in various places along Python's lookup path. >Sometimes, a large package is more useful if distributed as a collection of smaller eggs. However, Python does not normally allow the contents of a package to be retrieved from more than one location. "Namespace packages" are a solution for this problem. These examples are ambiguous and provide no effective examples @vaultah – AlanSTACK Feb 02 '16 at 12:30
  • So is the init file essentially totally optional? It only acts as a housekeeping instructor, and we can use folders without an init just as we would be able to use them in php, for example, to organize code? So init is more like a caveat than a requirement? @vaultah – AlanSTACK Feb 02 '16 at 12:37

2 Answers2

1

Answer

essentially we dont need init.py and its purpose is for legacy and optional housekeeping things (2.7 vs 3) you may or may not want to do or need. But at the same time, they have slightly different behavior during more complex parsing which should also be accounted for if you are building something more complex

refer to the following links for more reading material

https://www.python.org/dev/peps/pep-0420/#namespace-packages-today

How do I create a namespace package in Python?

What's the difference between a Python module and a Python package?

https://softwareengineering.stackexchange.com/questions/276888/python-namespace-vs-module-with-underscores

Community
  • 1
  • 1
AlanSTACK
  • 4,735
  • 3
  • 33
  • 80
0

As confusing as it may be, although the basics will work without __init__.py files, you should probably still use them. Many external tools, as well as package-related functions in the standard library, will not work as expected without them. More words of wisdom here (as well as a misleading accepted answer): Is __init__.py not required for packages in Python 3.3+.

lapis
  • 6,554
  • 3
  • 33
  • 38