Aunque muchas personas ya explicaron sobre importvs import from, quiero tratar de explicar un poco más sobre lo que sucede debajo del capó y dónde están todos los lugares donde cambia.
import foo:
Importa fooy crea una referencia a ese módulo en el espacio de nombres actual. Luego debe definir la ruta completa del módulo para acceder a un atributo o método particular desde el interior del módulo.
Por ejemplo, foo.barpero nobar
from foo import bar:
Importa fooy crea referencias a todos los miembros enumerados ( bar). No establece la variable foo.
Por ejemplo, barpero no bazofoo.baz
from foo import *:
Importa fooy crea referencias a todos los objetos públicos definidos por ese módulo en el espacio de nombres actual (todo lo que aparece en __all__si __all__existe, de lo contrario todo lo que no comienza con _). No establece la variable foo.
Por ejemplo bary bazpero no _quxo foo._qux.
Ahora veamos cuando lo hacemos import X.Y:
>>> import sys
>>> import os.path
Consultar sys.modulescon nombre osy os.path:
>>> sys.modules['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> sys.modules['os.path']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
De verificación globals()y locals()de espacio de nombres predice con osy os.path:
>>> globals()['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> locals()['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> globals()['os.path']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'os.path'
>>>
Del ejemplo anterior encontramos que solo osse inserta en el espacio de nombres local y global. Entonces, deberíamos poder usar:
>>> os
<module 'os' from
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> os.path
<module 'posixpath' from
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>>
Pero no path.
>>> path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'path' is not defined
>>>
Una vez que elimine el osespacio de nombres locals (), no podrá acceder ostan bien como os.pathexista en sys.modules:
>>> del locals()['os']
>>> os
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>> os.path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>>
Ahora hablemos de import from:
from:
>>> import sys
>>> from os import path
Consulte sys.modulescon osy os.path:
>>> sys.modules['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> sys.modules['os.path']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
Descubrimos que en sys.modulesencontramos lo mismo que antes usandoimport name
OK, cheque de Let cómo se ve en locals()e globals()espacio de nombres predice:
>>> globals()['path']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> locals()['path']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> globals()['os']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'os'
>>>
Puede acceder usando el nombre, pathno por os.path:
>>> path
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> os.path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>>
Eliminemos 'ruta' de locals():
>>> del locals()['path']
>>> path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'path' is not defined
>>>
Un último ejemplo con un alias:
>>> from os import path as HELL_BOY
>>> locals()['HELL_BOY']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> globals()['HELL_BOY']
<module 'posixpath' from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>>
Y no hay ruta definida:
>>> globals()['path']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'path'
>>>