当从单元测试调用我的代码时,为什么会出现多处理导入错误? (PyCharm Python 3)

发布于 2024-11-24 02:40:50 字数 1160 浏览 2 评论 0原文

在我的一个模块中,我执行以下导入:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

芸娘子的小脾气 2024-12-01 02:40:50

可能是因为您的 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 import Pool from your own multiprocessing.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文