当从命令行导入 Python 运行 时,导入 urllib.parse 失败

import urllib.parse fails when Python run from command line

我在 python 3.4.2 中观察到以下行为,我无法解释。希望有人能对此事有所了解:

在IPython中:

In [129]: import urllib

In [130]: print(urllib.parse)
<module 'urllib.parse' from '/Users/ashwin/.pyenv/versions/3.4.2/lib/python3.4/urllib/parse.py'>

我导入了一个模块,并打印了它的一个属性。一切都按预期工作。到目前为止,生活还不错。

现在,我从命令行做同样的事情:

$ python -c 'import urllib; print(urllib.parse)'  
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: 'module' object has no attribute 'parse'

说什么?!这不是应该的工作方式。
好的,也许这是 python 范围内的行为;使用 -c 标志时,可能不会立即导入模块。让我们试试另一个模块:

$ python -c 'import datetime; print(datetime.datetime)'
<class 'datetime.datetime'>

什么?!它如何适用于 datetime 而不适用于 urllib?我在两个地方 (3.4.2)

使用相同版本的 python

有人对此有什么想法吗?

编辑:

根据评论之一:

$ which -a ipython
/Users/ashwin/.pyenv/shims/ipython
/Library/Frameworks/Python.framework/Versions/2.7/bin/ipython
/usr/local/bin/ipython
/usr/local/bin/ipython

$ which -a python
/Users/ashwin/.pyenv/shims/python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
/usr/bin/python
/usr/bin/python

urllib.parse 从 Python 3 开始可用。我认为您可能需要 import urllib.parse,而不是 import urllib。不确定子模块导入是否(何时)是隐式的。

我猜 IPython 在启动时导入 urllib.parse,这就是它可用的原因。

parse 是模块而不是属性:

Python 3.4.2 (default, Oct 15 2014, 22:01:37)
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
>>> urllib.parse
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'parse'
>>> import urllib.parse
>>> urllib.parse
<module 'urllib.parse' from '/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/parse.py'>

当您 运行 import urllib 时,它会创建 urllib 模块(实际上是 package)的模块对象 而无需导入其子模块(解析、请求等)。

如果您想使用属性访问访问其子模块,您需要父模块对象 (urllib) 在您的命名空间中。除此之外,该子模块 必须 已经加载(导入)。来自 documentation:

if package spam has a submodule foo, after importing spam.foo, spam will have an attribute foo which is bound to the submodule. [...] The invariant holding is that if you have sys.modules['spam'] and sys.modules['spam.foo'] (as you would after the above import), the latter must appear as the foo attribute of the former.

每个模块只有一个实例,因此对 urllib 模块对象(存储在 sys.modules['urllib'] 中)所做的任何更改都会在所有地方反映出来。

你不导入 urllib.parse,但是 IPython 导入 为了证明这一点,我将要创建一个启动文件:

import urllib
print('Running the startup file: ', end='')
try:
    # After importing  'urllib.parse' ANYWHERE,
    # 'urllib' will have the 'parse' attribute.
    # You could also do "import sys; sys.modules['urllib'].parse"
    urllib.parse
except AttributeError:
    print("urllib.parse hasn't been imported yet")
else:
    print('urllib.parse has already been imported')
print('Exiting the startup file.')

并启动 ipython

vaultah@base:~$ ipython
Running urllib/parse.py
Running the startup file: urllib.parse has already been imported
Exiting the startup file.
Python 3.6.0a0 (default:089146b8ccc6, Sep 25 2015, 14:16:56) 
Type "copyright", "credits" or "license" for more information.

IPython 4.0.0 -- An enhanced Interactive Python.

是IPython启动时导入pydoc的副作用(which ipython/usr/local/bin/ipython):

/usr/local/bin/ipython, line 7:
  from IPython import start_ipython
/usr/local/lib/python3.6/site-packages/IPython/__init__.py, line 47:
  from .core.application import Application
/usr/local/lib/python3.6/site-packages/IPython/core/application.py, line 24:
  from IPython.core import release, crashhandler
/usr/local/lib/python3.6/site-packages/IPython/core/crashhandler.py, line 28:
  from IPython.core import ultratb
/usr/local/lib/python3.6/site-packages/IPython/core/ultratb.py, line 90:
  import pydoc
/usr/local/lib/python3.6/pydoc.py, line 68:
  import urllib.parse

这解释了为什么下面的代码失败了——你只导入了 urllib 而似乎没有导入任何东西 urllib.parse:

$ python -c 'import urllib; print(urllib.parse)'

另一方面,以下命令有效,因为 datetime.datetime 不是 模块。这是在 import datetime 期间导入的 class。

$ python -c 'import datetime; print(datetime.datetime)'