ImportError:无法从“concurrent.futures.process”导入名称“ProcessPoolExecutor”

2024-10-03 00:28:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个问题,我不知道发生了什么: 我尝试使用以下代码学习多处理教程:

import concurrent.futures
import time

start = time.perf_counter()

def do_something(seconds):
    print(f'sleeping {seconds} second(s)...')
    time.sleep(seconds)
    return 'Done sleeping'

with concurrent.futures.ProcessPoolExecutor() as executor:
    f1 = executor.submit(do_something, 1)
    print(f1.result()) 

我在jupyter笔记本中成功运行了这段代码,但在Pycharm中运行时收到以下错误消息:

/Users/jiangxu/PycharmProjects/Assign_compartment/venv/bin/python /Users/jiangxu/PycharmProjects/Assign_compartment/multiprocessing.py
Traceback (most recent call last):
  File "/Users/jiangxu/PycharmProjects/Assign_compartment/multiprocessing.py", line 14, in <module>
    with concurrent.futures.ProcessPoolExecutor() as executor:
  File "/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/__init__.py", line 43, in __getattr__
    from .process import ProcessPoolExecutor as pe
  File "/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/process.py", line 53, in <module>
    import multiprocessing as mp
  File "/Users/jiangxu/PycharmProjects/Assign_compartment/multiprocessing.py", line 14, in <module>
    with concurrent.futures.ProcessPoolExecutor() as executor:
  File "/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/__init__.py", line 43, in __getattr__
    from .process import ProcessPoolExecutor as pe
ImportError: cannot import name 'ProcessPoolExecutor' from 'concurrent.futures.process' (/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/process.py)

Process finished with exit code 1

发生了什么事


Tags: inpyimportaswithlineprocessusers