2

I have see this in python tutorial:

The __init__.py files are required to make Python treat the directories as containing packages;

I make the directory hierarchy in pycharm like this, where the subdir1 doesn't contain __init__.py and subdir2 contains a __init__.py file. enter image description here

First, I add Directory into system.pyth.

I write a hello function in hello1.py and hello2.py respectively.

Then I call hello func in test files like this:

# test1.py
from subdir1 import hello1
hello1.hello()

# test2.py
from subdir2 import hello2
hello2.hello()

They all succeed. It seems that the __init__.py does not necessary for import modules from different directories, right?

So, I want to know in what situation a __init__.py is required. Thanks for your answering!

Bicheng
  • 687
  • 8
  • 20
  • Possible duplicate of [What is \_\_init\_\_.py for?](https://stackoverflow.com/questions/448271/what-is-init-py-for) – Naresh Kumar Dec 10 '17 at 12:56

3 Answers3

5

Python 3.3+ has implicit namespace packages that allow you to create packages without __init__.py. In Python 2, __init__.py is the old method and still works.

Allowing implicit namespace packages means that the requirement to provide an __init__.py file can be dropped completely, and affected portions can be installed into a common directory or split across multiple directories as distributions see fit.

Note: init.py files were used to mark directories on your disk as Python packages.

Useful links:

  1. Implicit Namespace Packages
mrhallak
  • 1,138
  • 1
  • 12
  • 27
  • Please note that namespace packages should only be used _if_ you need them. Otherwise you should continue to put a `__init__.py` file in your directories to make them into explicit packages. There are subtle differences between the two which can trip you up if you don't understand the differences. And for the vast majority of cases, you don't need or want namespace packages. – Brett Cannon Jun 30 '20 at 23:52
-1
__init__.py

All the code in the Python interpreter will be lost when we exit the interpreter. But when writing large programs file is broken into multiple different files for ease of use, debugging and readability. In Python modules are used to achieve such goals. Modules are nothing but files with Python definitions and statements. The module name, to import, has the same name of the Python file without the .py extension.

So if you want the files under some directory to be a module and you want it to be a exportable module into some other piece of code then that category need to have the init.py

More details

Naresh Kumar
  • 1,706
  • 1
  • 14
  • 26
  • But in my problem, I imported hello1 from subdir1 which without a init.py in test1 in subdir3, and it works. why? I use python3.6. – Bicheng Dec 10 '17 at 12:20
  • I mean that in your answer you said "So if you want the files under some directory to be a module and you want it to be a exportable module into some other piece of code then that category need to have the init.py". And in my situation subdir1 and subdir3 are different directories, and I actually use some code in subdir1(without the init.py) in subdir3 successfully, doesn't it mean the module in subdir1 exportable? – Bicheng Dec 10 '17 at 12:55
-2

For making Python treat the Directory as a package.

Python Doc

BoRRis
  • 983
  • 8
  • 23