Obwohl viele Leute bereits über import
vs erklärt haben import from
, möchte ich versuchen, ein bisschen mehr darüber zu erklären, was unter der Haube passiert und wo sich all die Orte befinden, an denen es sich ändert.
import foo
::
Importiert foo
und erstellt einen Verweis auf dieses Modul im aktuellen Namespace. Anschließend müssen Sie den abgeschlossenen Modulpfad definieren, um aus dem Modul heraus auf ein bestimmtes Attribut oder eine bestimmte Methode zugreifen zu können.
ZB foo.bar
aber nichtbar
from foo import bar
::
Importiert foo
und erstellt Verweise auf alle aufgelisteten Mitglieder ( bar
). Setzt die Variable nicht foo
.
ZB bar
aber nicht baz
oderfoo.baz
from foo import *
::
Importiert foo
und erstellt Verweise auf alle öffentlichen Objekte, die von diesem Modul im aktuellen Namespace definiert wurden (alles, was in vorhanden ist, __all__
falls __all__
vorhanden, sonst alles, was nicht beginnt _
). Setzt die Variable nicht foo
.
ZB bar
und baz
aber nicht _qux
oder foo._qux
.
Nun wollen wir sehen, wann wir es tun import X.Y
:
>>> import sys
>>> import os.path
Überprüfen Sie sys.modules
mit Namen os
und 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'>
Check globals()
und locals()
Namespace diktieren mit os
und 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'
>>>
Aus dem obigen Beispiel haben wir herausgefunden, dass nur os
in den lokalen und globalen Namespace eingefügt wird. Wir sollten also in der Lage sein:
>>> 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'>
>>>
Aber nicht path
.
>>> path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'path' is not defined
>>>
Sobald Sie das Löschen os
von Einheimischen () Namespace, werden Sie nicht zugreifen können os
, sowie os.path
auch wenn sie in sys.modules existieren:
>>> 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
>>>
Sprechen wir jetzt über import from
:
from
::
>>> import sys
>>> from os import path
Überprüfen Sie sys.modules
mit os
und 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'>
Wir fanden, dass sys.modules
wir in das gleiche wie zuvor gefunden haben, indem wir verwendet habenimport name
OK, lassen Sie uns überprüfen, wie es in locals()
und globals()
Namespace-Diktaten aussieht :
>>> 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'
>>>
Sie können mit dem Namen zugreifen, path
nicht mit 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
>>>
Löschen wir 'Pfad' aus locals()
:
>>> del locals()['path']
>>> path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'path' is not defined
>>>
Ein letztes Beispiel mit einem 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'>
>>>
Und kein Pfad definiert:
>>> globals()['path']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'path'
>>>