潇潇雨雨
很多人已经解释过importvs from,所以我想尝试更多地解释一下,实际的差异在于。首先,让我准确解释一下基本的import语句。import X导入模块X,并在当前命名空间中创建对该模块的引用。然后,您需要定义已完成的模块路径以从模块内部访问特定属性或方法(例如: X.name或X.attribute)from X import *导入模块X,并创建对当前命名空间中该模块定义的所有公共对象的引用(即,没有名称的所有公共对象_)或您提到的任何名称。或者,换句话说,在运行此语句之后,您可以简单地使用普通(非限定)名称来引用模块中定义的内容X。但是X它本身没有定义,所以X.name不起作用。如果name 已经定义,它将被新版本替换。如果将name in X更改为指向其他某个对象,则您的模块将不会注意到。这使得模块中的所有名称都可在本地名称空间中使用。现在让我们看看当我们这样做时会发生什么import X.Y:>>> import sys>>> import os.path检查sys.modules姓名os和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'>检查globals()和locals()命名空间dict的名称os和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'>>>从上面的例子中,我们发现只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:>>> pathTraceback (most recent call last):
File "<stdin>", line 1, in <module>NameError: name 'path' is not defined
>>>删除osfrom locals()命名空间后,您将无法访问os或者os.path,即使它们确实存在于sys.modules:>>> del locals()['os']>>> osTraceback (most recent call last):
File "<stdin>", line 1, in <module>NameError: name 'os' is not defined>>> os.pathTraceback (most recent call last):
File "<stdin>", line 1, in <module>NameError: name 'os' is not defined>>>现在让我们来看看from。from>>> import sys>>> from os import path检查sys.modules姓名os和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'>所以sys.modules看起来和我们导入时一样import name。好的。让我们看一下它locals()和globals()命名空间字符串的含义:>>> 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.pathTraceback (most recent call last):
File "<stdin>", line 1, in <module>NameError: name 'os' is not defined>>>让我们从locals()中删除'path':>>> del locals()['path']>>> pathTraceback (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'>>>关于使用的一个陷阱 fromname从两个不同的模块导入时:>>> 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'>>>>最后的进口将赢得胜利