3 回答

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超13個(gè)贊
這取決于您在引用它時(shí)如何訪問(wèn)導(dǎo)入。
from urllib import request# access request directly.mine = request()import urllib.request# used as urllib.requestmine = urllib.request()
為簡(jiǎn)單起見,您還可以在導(dǎo)入時(shí)自己為別名設(shè)置別名,或者避免屏蔽內(nèi)置插件:
from os import open as open_# lets you use os.open without destroying the # built in open() which returns file handles.

TA貢獻(xiàn)1833條經(jīng)驗(yàn) 獲得超4個(gè)贊
很多人已經(jīng)解釋過(guò)import
vs from
,所以我想嘗試更多地解釋一下,實(shí)際的差異在于。
首先,讓我準(zhǔn)確解釋一下基本的import語(yǔ)句。
import X
導(dǎo)入模塊
X
,并在當(dāng)前命名空間中創(chuàng)建對(duì)該模塊的引用。然后,您需要定義已完成的模塊路徑以從模塊內(nèi)部訪問(wèn)特定屬性或方法(例如:X.name
或X.attribute
)
from X import *
導(dǎo)入模塊
X
,并創(chuàng)建對(duì)當(dāng)前命名空間中該模塊定義的所有公共對(duì)象的引用(即,沒(méi)有名稱的所有公共對(duì)象_
)或您提到的任何名稱。或者,換句話說(shuō),在運(yùn)行此語(yǔ)句之后,您可以簡(jiǎn)單地使用普通(非限定)名稱來(lái)引用模塊中定義的內(nèi)容
X
。但是X
它本身沒(méi)有定義,所以X.name
不起作用。如果name
已經(jīng)定義,它將被新版本替換。如果將name inX
更改為指向其他某個(gè)對(duì)象,則您的模塊將不會(huì)注意到。這使得模塊中的所有名稱都可在本地名稱空間中使用。
現(xiàn)在讓我們看看當(dāng)我們這樣做時(shí)會(huì)發(fā)生什么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'>>>
從上面的例子中,我們發(fā)現(xiàn)只os
添加到本地和全局命名空間。所以,我們應(yīng)該可以使用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 >>>
刪除os
from locals()
命名空間后,您將無(wú)法訪問(wèn)os
或者os.path
,即使它們確實(shí)存在于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>>>
現(xiàn)在讓我們來(lái)看看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
看起來(lái)和我們導(dǎo)入時(shí)一樣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
,但不能通過(guò)以下方式訪問(wèn)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>>>
使用別名的最后一個(gè)示例:
>>> 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'> >>>
并沒(méi)有定義路徑:
>>> globals()['path']Traceback (most recent call last): File "<stdin>", line 1, in <module>KeyError: 'path'>>>
關(guān)于使用的一個(gè)陷阱 from
name
從兩個(gè)不同的模塊導(dǎo)入時(shí):
>>> import sys>>> from os import stat>>> locals()['stat']<built-in function stat>>>>>>> stat<built-in function stat>
shutil
再次導(dǎo)入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'>>>>
最后的進(jìn)口將贏得勝利
添加回答
舉報(bào)