Anche se molte persone hanno già spiegato riguardo al importvs 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 fooe 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.barma nobar
from foo import bar:
Importa fooe crea riferimenti a tutti i membri elencati ( bar). Non imposta la variabile foo.
Ad esempio barma no bazofoo.baz
from foo import *:
Importa fooe 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, bare bazma non _quxo foo._qux.
Ora vediamo quando lo facciamo import X.Y:
>>> import sys
>>> import os.path
Verifica sys.modulescon nome ose 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 ose 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 osspazio dei nomi from local (), non sarà possibile accedervi os, os.pathanche 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.modulescon ose 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.modulesabbiamo 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 pathnon 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'
>>>