multiprocessing.dummy为什么会出现AttributeError:' module'对象没有属性' dummy'

时间:2015-11-18 08:11:09

标签: python module multiprocessing python-import

我在Python中遇到了一件奇怪的事情:

>>> import multiprocessing

>>> thread_pool = multiprocessing.dummy.Pool()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'dummy'

然而,当我尝试以下内容时:

>>> from multiprocessing.dummy import Pool as ThreadPool
>>> ThreadPool()
<multiprocessing.pool.ThreadPool object at 0x7faf9308d4a8>

>>> import multiprocessing.dummy
>>> multiprocessing.dummy.Pool()
<multiprocessing.pool.ThreadPool object at 0x7faf9308d2e8>

一切都好。 我知道import xxxfrom xxx import之间的区别,我想知道它为什么会引发AttributeError。 我的问题是为什么在multiprocessing.dummy之后我无法使用import multiprocessing

1 个答案:

答案 0 :(得分:0)

multiprocessing.dummy包中未导入multiprocessing.__init__.py个包。因此,

>>> import multiprocessing

>>> thread_pool = multiprocessing.dummy.Pool()

不起作用。形成文档字符串:

# This package is intended to duplicate the functionality (and much of
# the API) of threading.py but uses processes instead of threads.  A
# subpackage 'multiprocessing.dummy' has the same API but is a simple
# wrapper for 'threading'.

我猜这个子包故意隐藏隐藏以防止人们意外地使用它。