Même si beaucoup de gens ont déjà expliqué à propos de importvs import from, je veux essayer d'expliquer un peu plus ce qui se passe sous le capot et où se trouvent tous les endroits où cela change.
import foo:
Importe fooet crée une référence à ce module dans l'espace de noms actuel. Ensuite, vous devez définir le chemin d'accès au module terminé pour accéder à un attribut ou une méthode particulière depuis l'intérieur du module.
Par exemple foo.barmais pasbar
from foo import bar:
Importe fooet crée des références à tous les membres répertoriés ( bar). Ne définit pas la variable foo.
Par exemple, barmais pas bazoufoo.baz
from foo import *:
Importe fooet crée des références à tous les objets publics définis par ce module dans l'espace de noms actuel (tout ce qui est répertorié dans __all__s'il __all__existe, sinon tout ce qui ne commence pas _). Ne définit pas la variablefoo .
Par exemple baret bazmais pas _quxou foo._qux.
Voyons maintenant quand nous le faisons import X.Y:
>>> import sys
>>> import os.path
Vérifiez sys.modulesavec nom oset 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'>
Vérifier globals()et locals()dicter l'espace de noms avec oset 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'
>>>
À partir de l'exemple ci-dessus, nous avons constaté que seul osest inséré dans l'espace de noms local et global. Donc, nous devrions pouvoir utiliser:
>>> 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'>
>>>
Mais non path.
>>> path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'path' is not defined
>>>
Une fois que vous avez supprimé l' osespace de noms de locals (), vous ne pourrez plus y accéder osaussi bien os.pathqu'ils existent dans 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
>>>
Parlons maintenant de import from:
from:
>>> import sys
>>> from os import path
Vérifiez sys.modulesauprès de oset 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'>
Nous avons constaté que sys.modulesnous avons trouvé la même chose qu'avant en utilisantimport name
OK, vérifions à quoi ça ressemble dans locals()et les globals()espaces de noms dict:
>>> 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'
>>>
Vous pouvez y accéder en utilisant un nom et pathnon en 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
>>>
Supprimons le «chemin» de locals():
>>> del locals()['path']
>>> path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'path' is not defined
>>>
Un dernier exemple utilisant 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'>
>>>
Et aucun chemin défini:
>>> globals()['path']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'path'
>>>