Anche se molte persone hanno già spiegato riguardo al import
vs import from
, voglio provare a spiegare un po 'di più su cosa succede sotto il cofano e dove si trovano tutti i luoghi in cui cambia.
import foo
:
Importa foo
e crea un riferimento a quel modulo nello spazio dei nomi corrente. Quindi è necessario definire il percorso del modulo completo per accedere a un particolare attributo o metodo dall'interno del modulo.
Ad esempio foo.bar
ma nobar
from foo import bar
:
Importa foo
e crea riferimenti a tutti i membri elencati ( bar
). Non imposta la variabile foo
.
Ad esempio bar
ma no baz
ofoo.baz
from foo import *
:
Importa foo
e crea riferimenti a tutti gli oggetti pubblici definiti da quel modulo nello spazio dei nomi corrente (tutto ciò che è elencato __all__
se __all__
esiste, altrimenti tutto ciò che non inizia con _
). Non imposta la variabile foo
.
Ad esempio, bar
e baz
ma non _qux
o foo._qux
.
Ora vediamo quando lo facciamo import X.Y
:
>>> import sys
>>> import os.path
Verifica sys.modules
con nome os
e 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'>
Controlla globals()
e locals()
dice lo spazio dei nomi con os
e 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'
>>>
Dall'esempio sopra abbiamo scoperto che solo os
è inserito nello spazio dei nomi locale e globale. Quindi, dovremmo essere in grado di usare:
>>> 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'>
>>>
Ma non path
.
>>> path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'path' is not defined
>>>
Una volta eliminato lo os
spazio dei nomi from local (), non sarà possibile accedervi os
, os.path
anche se esistono in 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
>>>
Ora parliamo di import from
:
from
:
>>> import sys
>>> from os import path
Verifica sys.modules
con os
e 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'>
Abbiamo scoperto che in sys.modules
abbiamo trovato lo stesso di prima usandoimport name
OK, controlliamo come appare in locals()
e globals()
spazio dei nomi:
>>> 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'
>>>
È possibile accedere utilizzando il nome path
non tramite 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
>>>
Eliminiamo 'percorso' da locals()
:
>>> del locals()['path']
>>> path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'path' is not defined
>>>
Un ultimo esempio usando 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'>
>>>
E nessun percorso definito:
>>> globals()['path']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'path'
>>>