Est __init__.py pas requis pour les colis en Python 3.3+

Je suis à l'aide de Python 3.5.1. J'ai lu le document et la section de package ici: https://docs.python.org/3/tutorial/modules.html#packages

Maintenant, j'ai la structure suivante:

/home/wujek/Playground/a/b/module.py

module.py:

class Foo:
    def __init__(self):
        print('initializing Foo')

Maintenant, alors que dans /home/wujek/Playground:

~/Playground $ python3
>>> import a.b.module
>>> a.b.module.Foo()
initializing Foo
<a.b.module.Foo object at 0x100a8f0b8>

De la même façon, maintenant dans la maison, superfolder de Playground:

~ $ PYTHONPATH=Playground python3
>>> import a.b.module
>>> a.b.module.Foo()
initializing Foo
<a.b.module.Foo object at 0x10a5fee10>

En fait, je peux faire toutes sortes de choses:

~ $ PYTHONPATH=Playground python3
>>> import a
>>> import a.b
>>> import Playground.a.b

Pourquoi ce travail? Je pensais qu'il fallait être __init__.py fichiers (vides fonctionne) dans les deux a et b pour module.py être importable lorsque le Python path points à la Playground dossier?

Cela semble avoir changé de Python 2.7:

~ $ PYTHONPATH=Playground python
>>> import a
ImportError: No module named a
>>> import a.b
ImportError: No module named a.b
>>> import a.b.module
ImportError: No module named a.b.module

Avec __init__.py dans les deux ~/Playground/a et ~/Playground/a/b il fonctionne très bien.

InformationsquelleAutor wujek | 2016-05-10