我想知道代码片段之间是否有任何区别

from urllib import request

这个片段

import urllib.request

或者它们是否可以互换。如果它们是可互换的,那么哪种是“标准”/“首选”语法(如果有的话)?


当前回答

这取决于您在引用导入时希望如何访问它。

from urllib import request
# access request directly.
mine = request()

import urllib.request
# used as urllib.request
mine = urllib.request()

当你为了简单或避免屏蔽内置而导入时,你也可以自己给东西起别名:

from os import open as open_
# lets you use os.open without destroying the 
# built in open() which returns file handles.

其他回答

这是有区别的。在某些情况下,其中一种可行,另一种行不通。这里有一个例子:假设我们有以下结构:

foo.py
mylib\
    a.py
    b.py

现在,我想把b.py导入a.py。我想把a。py导入到foo。我怎么做呢?两句话,在I中写道:

import b

在foo.py中我写道:

import mylib.a

当试图运行foo.py时,这将生成一个ImportError。解释器会抱怨a.py (import b)中的import语句说没有模块b。那么如何解决这个问题呢?在这种情况下,更改a中的import语句来导入mylib.b 将不会工作,因为a和b都在mylib中。这里的解决方案(或至少一个解决方案)是使用绝对导入:

from mylib import b

import a module用于导入一个模块

你在包中使用的是Python3 urllib。两种形式都是可以接受的,没有一种形式的导入优于另一种。有时,当涉及多个包目录时,您可以从x.y.z.a导入使用前者

在这个urllib包的特殊情况下,第二种方法导入urllib。请求和使用urllib。请求是标准库统一使用它的方式。

我对import urllib的主要不满。请求是你仍然可以引用urllib。即使没有导入也要解析。

>>> import urllib3.request
>>> urllib3.logging
<module 'logging' from '/usr/lib/python2.7/logging/__init__.pyc'>

urllib3下也有我的请求。Python 2.7.4 ubuntu

很多人已经解释了import和from的区别,所以我想进一步解释一下它们的区别。

首先,让我解释一下基本的import语句是做什么的。

进口X

类中创建对该模块的引用 当前的名称空间。然后需要定义完成的模块路径 从模块内部访问一个特定的属性或方法(例如: X.name或X.attribute)

从X导入*

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.

现在让我们看看导入X.Y时会发生什么:

>>> import sys
>>> import os.path

检查系统。命名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'>

用os和os.path检查globals()和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
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'path' is not defined 
>>>

一旦你从locals()命名空间中删除os,你将不能访问os或os。路径,即使它们确实存在于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
>>>

现在我们来看from。

from

>>> import sys
>>> from os import path

检查系统。命名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'>

所以系统。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.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的一个陷阱

当你从两个不同的模块导入相同的名称时:

>>> 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'>
>>>

最后一个导入将获胜

这取决于您在引用导入时希望如何访问它。

from urllib import request
# access request directly.
mine = request()

import urllib.request
# used as urllib.request
mine = urllib.request()

当你为了简单或避免屏蔽内置而导入时,你也可以自己给东西起别名:

from os import open as open_
# lets you use os.open without destroying the 
# built in open() which returns file handles.