当从单元测试调用我的代码时,为什么会出现多处理导入错误? (PyCharm Python 3)
在我的一个模块中,我执行以下导入:
from multiprocessing import Pool
正常调用时该模块工作正常,但是当我从单元测试中使用它时,出现以下错误:
Error
Traceback (most recent call last):
File "/share/work/peter/software/lib/python3.2/unittest/case.py", line 387, in _executeTestPart
function()
File "/home/peter/current/parallelize/src/parallelize/backend/tests.py", line 52, in test_submit_ok_job
backend = self._get_multi_processing_backend()
File "/home/peter/current/parallelize/src/parallelize/backend/tests.py", line 46, in _get_multi_processing_backend
from parallelize.backend.multiprocessing import MultiprocessingBackend
File "/home/peter/current/parallelize/src/parallelize/backend/multiprocessing.py", line 2, in <module>
from multiprocessing import Pool,cpu_count
File "/home/peter/current/parallelize/src/parallelize/backend/multiprocessing.py", line 2, in <module>
from multiprocessing import Pool,cpu_count
ImportError: cannot import name Pool
为什么会发生这种情况?我正在使用 Python 3.2
编辑 我知道注意到只有从我的 PyCharm IDE 启动单元测试时才会出现这种情况。当从命令行运行时(python3 -m unittest ....)它可以工作。这是 IDE 中的错误吗?
In one of my modules I do the following import:
from multiprocessing import Pool
This module works fine when called normally, but when I use this from a unittest, I get the following error:
Error
Traceback (most recent call last):
File "/share/work/peter/software/lib/python3.2/unittest/case.py", line 387, in _executeTestPart
function()
File "/home/peter/current/parallelize/src/parallelize/backend/tests.py", line 52, in test_submit_ok_job
backend = self._get_multi_processing_backend()
File "/home/peter/current/parallelize/src/parallelize/backend/tests.py", line 46, in _get_multi_processing_backend
from parallelize.backend.multiprocessing import MultiprocessingBackend
File "/home/peter/current/parallelize/src/parallelize/backend/multiprocessing.py", line 2, in <module>
from multiprocessing import Pool,cpu_count
File "/home/peter/current/parallelize/src/parallelize/backend/multiprocessing.py", line 2, in <module>
from multiprocessing import Pool,cpu_count
ImportError: cannot import name Pool
Why does this happen? I am using Python 3.2
EDIT I know notices that this is only the case when the unittest is started from my PyCharm IDE. When run from the commandline (python3 -m unittest ....) it works. Is this a bug in the IDE?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能是因为您的 Python 路径上有
...src/parallelize/backend/
目录(也许它是工作目录),并且 Python 正在尝试导入Pool
来自您自己的multiprocessing.py
,而不是来自标准库。我不知道 PyCharm 是否可行,但请尝试将工作目录更改为其他目录。It may be because you have the
...src/parallelize/backend/
directory on your Python path (perhaps it is the working directory), and Python is trying to importPool
from your ownmultiprocessing.py
there, not from the standard library. I don’t know if it’s possible with PyCharm, but try changing the working directory to something else.