La diferencia entre tener _ init _.py y no tener uno en su directorio de módulos es:
Cuando tiene__init__.py
(en blanco uno), puede importar el módulo usando
from dirname import MyModule
Pero cuando no tiene _ init _.py en absoluto, no puede importar el módulo sin agregar la ruta hasta ese módulo a PYTHONPATH. En este casofrom dirname import MyModule
FAILS, o informa error.
# Estructura del directorio/Módulo sin _ init _.py
[[email protected] python]# cd NoInitModule/
[[email protected] NoInitModule]# pwd
/python/NoInitModule
[[email protected] NoInitModule]# ls -l
total 4
-rw-r--r--. 1 root root 44 Jan 16 07:02 ModuleX.py
# Estructura del directorio/módulo con init _ _.py
[[email protected] NoInitModule]# cd ../InitModule/
[[email protected] InitModule]# ls -l
total 4
-rw-r--r--. 1 root root 0 Jan 16 07:13 __init__.py
-rw-r--r--. 1 root root 49 Jan 16 07:12 ModuleY.py
[[email protected] InitModule]#
Ejemplos:
# NO _ init _.py , desde la declaración del módulo de importación de dir: NO FUNCIONA
[[email protected] python]# cat impomod.py
from NoInitModule import ModuleX
ModuleX.printBye()
[[email protected] python]# python impomod.py
Traceback (most recent call last):
File "impomod.py", line 7, in <module>
from NoInitModule import ModuleX
ImportError: No module named NoInitModule
[[email protected] python]#
# NO _ init _.py, la declaración de importación - NO FUNCIONA
[[email protected] python]# vim impomod.py
[[email protected] python]# cat impomod.py
#from NoInitModule import ModuleX
import ModuleX
ModuleX.printBye()
[[email protected] python]# python impomod.py
Traceback (most recent call last):
File "impomod.py", line 8, in <module>
import ModuleX
ImportError: No module named ModuleX
[[email protected] python]#
# NO _ init _.py, Insertar ruta en PYTHONPATH, importar la instrucción del módulo después de insertar la ruta - OBRAS
[[email protected] python]# vim impomod.py
[[email protected] python]# cat impomod.py
import sys
sys.path.append('/python/NoInitModule')
print sys.path
import ModuleX
ModuleX.printBye()
[[email protected] python]# python impomod.py
Bye from Module X with no __init__.py
[[email protected] python]#
# HAVING _ init _.py, sin inserción de ruta, de la declaración módulo de importación dir - WORKS
[[email protected] python]# cat impXmod.py
import sys
from InitModule import ModuleY
ModuleY.printSeeU()
[[email protected] python]#
[[email protected] python]# python impXmod.py
See you from Module Y having __init__.py
# TIENE _ init _.py, sin inserción de ruta, la importación declaración módulo - NO FUNCIONA
[[email protected] python]# vim impXmod.py
[[email protected] python]# cat impXmod.py
import sys
#from InitModule import ModuleY
import ModuleY
ModuleY.printSeeU()
[[email protected] python]# python impXmod.py
Traceback (most recent call last):
File "impXmod.py", line 3, in <module>
import ModuleY
ImportError: No module named ModuleY
Por supuesto, cuestión subcarpetas se puede añadir a esta
Ok, supongamos que hay una carpeta: "A". Y "A" tiene un '__init __. Py'. Pero "A" no está en PYTHONPATH. ¿Puedo importar un módulo desde la carpeta "A"? ¿Python busca todas las carpetas en mi computadora para saber si tienen "' __init __. Py' "s en ellas? Quiero decir, ¿cómo sabe Python el camino a "A"? – alwbtc
@steve: Aún debe agregar la ruta del paquete a su 'PYTHONPATH'. Python no busca en toda su computadora carpetas que contengan '__init __. Py'. –