The online information about the role of __init__.py is everywhere, and I'm not going to nag about it here.
If you need, please crossing to search.
I've been having this interesting thing since I first started using Python3.
Gossip less, the following is to be introduced in the Python3 in __init__.py some of the slightly different places
The directory structure is as follows:
├──mdl│ __init__ . py│ ├──bar.py│ └──foo.py└──test.py
The contents of the file, from top to bottom, sequentially
mdl/__init__.py:
# Encoding:utf-8 from Import Foo #python2, 3 are applicable fromimport Bar #python2, 3 # From mdl.foo import foo #python2, 3 applies #frommdl.bar import Bar #python2, 3 applies #fromfoo import foo #python3不适用 #frombar import Bar # Python3 Not available
mdl/bar.py:
class Bar (): def __init__ (self): Pass
mdl/foo.py
class Foo (): def __init__ (self): Pass
test.py
from Import Foo from Import == Bar ()
You can see the sticker code, you may know what I want to say is what the meaning of it ...
Yes, that's right.
Before we used Python2 (at least after python2.6), we usually put the __init__.py empty (PS: Empty, and python3 can also be common) or directly (as shown)
from Import Foo from Import Bar
You can then use the Python test.py normally and happily without any error messages.
But at Python3 (at least after python3.4), we do this.
from Import Foo from Import Bar
Running Python test.py found an error, embarrassing ...
' Foo '
I'm not going to tell you anything else, I think everybody's got it.
Recommend that you use this method later (common usage)
from Import Foo from Import Bar
Appendix
1, after Python2 to run the program (that is, Python test.py), found
__init____init__. pyc│├──bar.py│├──bar.pyc│├──foo.py│└──foo.pyc└──test.py
(yes, we are very common. PYc appeared)
2, after Python3 to run the program, found
__init____pycache____init__. cpython-34. Pyc││├──bar.cpython- Pyc││└──foo.cpython-34. pyc│├──bar.py│└──foo.py└──test.py
(See the difference ...) )
Python3 module in the __init__.py of the need to pay attention to the place