Obwohl viele Leute bereits über importvs 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 foound 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.baraber nichtbar
from foo import bar::
Importiert foound erstellt Verweise auf alle aufgelisteten Mitglieder ( bar). Setzt die Variable nicht foo.
ZB baraber nicht bazoderfoo.baz
from foo import *::
Importiert foound 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 barund bazaber nicht _quxoder foo._qux.
Nun wollen wir sehen, wann wir es tun import X.Y:
>>> import sys
>>> import os.path
Überprüfen Sie sys.modulesmit Namen osund 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 osund 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 osin 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 osvon Einheimischen () Namespace, werden Sie nicht zugreifen können os, sowie os.pathauch 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.modulesmit osund 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.moduleswir 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, pathnicht 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'
>>>