Python错误:AttributeError:' module'对象没有属性' heappush'

时间:2016-09-22 12:11:29

标签: python python-2.7

我正在尝试一些涉及Python中多处理功能的简单程序。

代码如下:

DoSomething(string)

但是,这会给出错误消息from multiprocessing import Process, Queue def print_square(i): print i*i if __name__ == '__main__': output = Queue() processes = [Process(target=print_square,args=(i,)) for i in range(5)] for p in processes: p.start() for p in processes: p.join() 执行脚本时的完整输出如下:

Traceback (most recent call last):
  File "parallel_3.py", line 15, in 
    output = Queue()
  File "C:\Users\abc\AppData\Local\Continuum\Anaconda2\lib\multi
processing\__init__.py", line 217, in Queue
    from multiprocessing.queues import Queue
  File "C:\Users\abc\AppData\Local\Continuum\Anaconda2\lib\multi
processing\queues.py", line 45, in 
    from Queue import Empty, Full
  File "C:\Users\abc\AppData\Local\Continuum\Anaconda2\lib\Queue
.py", line 212, in 
    class PriorityQueue(Queue):
  File "C:\Users\abc\AppData\Local\Continuum\Anaconda2\lib\Queue
.py", line 224, in PriorityQueue
    def _put(self, item, heappush=heapq.heappush):
AttributeError: 'module' object has no attribute 'heappush'

如果对output = Queue()语句进行了注释,则代码编译正常。 什么可能导致这个错误?

1 个答案:

答案 0 :(得分:1)

您的文件名应该是包名称。更改为另一个文件名,例如heap,它将起作用。