Imports the module X, and creates a reference to that module in the
current namespace. Then you need to define completed module path to
access a particular attribute or method from inside the module (e.g.:
X.name or X.attribute)
from X import *
Imports the module X, and creates references to all public objects
defined by that module in the current namespace (that is, everything
that doesn’t have a name starting with _) or whatever name
you mentioned.
Or, in other words, after you've run this statement, you can simply
use a plain (unqualified) name to refer to things defined in module X.
But X itself is not defined, so X.name doesn't work. And if name
was already defined, it is replaced by the new version. And if name in X is
changed to point to some other object, your module won’t notice.
This makes all names from the module available in the local namespace.
现在让我们看看当我们做import X.Y时会发生什么:
>>> import sys
>>> import os.path
用名称os和os.path检查sys.modules:
>>> 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'>
检查名称为os和locals()的名称空间dict:
>>> 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'
>>>
从上面的例子中,我们发现只有os被添加到本地和全局名称空间中。
因此,我们应该能够使用os:
>>> 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'>
>>>
…但不是path:
>>> path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'path' is not defined
>>>
>>> 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
>>>
现在让我们看看from。
from
>>> import sys
>>> from os import path
用名称os和os.path检查sys.modules:
>>> 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'>
所以sys.modules看起来和我们使用import name导入时一样。
好吧。让我们检查一下locals()和globals()命名空间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'
>>>
您可以使用path访问,但不能使用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
>>>
让我们从locals()中删除“path”:
>>> del locals()['path']
>>> path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'path' is not defined
>>>
最后一个使用别名的示例:
>>> 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'>
>>>
没有定义路径:
>>> globals()['path']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'path'
>>>
使用from的一个陷阱
当您从两个不同的模块导入相同的name时:
>>> import sys
>>> from os import stat
>>> locals()['stat']
<built-in function stat>
>>>
>>> stat
<built-in function stat>
再次从shutil导入stat:
>>>
>>> from shutil import stat
>>> locals()['stat']
<module 'stat' from
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/stat.pyc'>
>>> stat
<module 'stat' from
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/stat.pyc'>
>>>
这是有区别的。在某些情况下,其中一个可以工作,另一个不行。下面是一个例子:假设我们有以下结构:
现在,我要将
b.py
导入a.py
。我想把a.py
导入foo
。我该怎么做?我在a
中写了两个语句:在
foo.py
中,我写:好吧,这将在尝试运行
foo.py
时生成ImportError
。解释器会抱怨a.py
(import b
)中的import语句说没有模块b。那么如何解决这个问题呢?在这种情况下,将a
中的import语句更改为import mylib.b
不会工作,因为a
和b
都在mylib
中。这里的解决方案(或至少一个解决方案)是使用绝对导入:来源:Python: importing a module that imports a module
这取决于引用导入时要如何访问导入。
在导入时,为了简单或避免掩蔽内置组件,您还可以自己对事物进行别名:
很多人已经解释过
import
与from
的关系,所以我想尝试在幕后解释一下,实际的区别在哪里。首先,让我解释一下基本导入语句的作用。
import X
from X import *
现在让我们看看当我们做
import X.Y
时会发生什么:用名称
os
和os.path
检查sys.modules
:检查名称为
os
和locals()
的名称空间dict:从上面的例子中,我们发现只有
os
被添加到本地和全局名称空间中。 因此,我们应该能够使用os
:…但不是
path
:一旦从
locals()
命名空间中删除os
,就无法访问os
或os.path
,即使它们确实存在于sys.modules
:现在让我们看看
from
。from
用名称
os
和os.path
检查sys.modules
:所以
sys.modules
看起来和我们使用import name
导入时一样。好吧。让我们检查一下
locals()
和globals()
命名空间dict是什么样子的:您可以使用
path
访问,但不能使用os.path
:让我们从locals()中删除“path”:
最后一个使用别名的示例:
没有定义路径:
使用
from
的一个陷阱当您从两个不同的模块导入相同的
name
时:再次从
shutil
导入stat:最后一次导入将获胜
相关问题 更多 >
编程相关推荐