Estoy usando Python 3.5.1. Leí el documento y la sección del paquete aquí: https://docs.python.org/3/tutorial/modules.html#packages
Ahora, tengo la siguiente estructura:
/home/wujek/Playground/a/b/module.py
module.py:
class Foo:
def __init__(self):
print('initializing Foo')
Ahora, mientras que en /home/wujek/Playground:
~/Playground $ python3
>>> import a.b.module
>>> a.b.module.Foo()
initializing Foo
<a.b.module.Foo object at 0x100a8f0b8>
Del mismo modo, ahora en casa, supercarpeta de Playground:
~ $ PYTHONPATH=Playground python3
>>> import a.b.module
>>> a.b.module.Foo()
initializing Foo
<a.b.module.Foo object at 0x10a5fee10>
En realidad, puedo hacer todo tipo de cosas:
~ $ PYTHONPATH=Playground python3
>>> import a
>>> import a.b
>>> import Playground.a.b
¿Por qué funciona esto? Pensé que tenía que haber __init__.pyarchivos (los vacíos funcionarían) en ambos ay bpara module.pypoder ser importados cuando la ruta de Python apunta a la Playgroundcarpeta.
Esto parece haber cambiado desde 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
Con __init__.pyen ambos ~/Playground/ay ~/Playground/a/bfunciona bien.