Ad

Failed To Import Multiprocessing Queue Object In Python3.6

I was using multiprocessing library in python. I have python 3.6. Whenever i try to create the multiprocessing. Queue() object i get an error.

My code looks like:

import multiprocessing

def square(arr,q):
    for i in arr:
        q.put(i*i)


arr=[1,2,3,4,5,6]
q=multiprocessing.Queue()
p1=multiprocessing.Process(target=square,args=(arr,q,))

p1.start()
p1.join()

result=[]

while q.empty() is False:
    result.append(q.get())

print(result)

and error is :

Traceback (most recent call last):
  File "qu.py", line 9, in <module>
    q=multiprocessing.Queue()
  File "/usr/lib/python3.6/multiprocessing/context.py", line 101, in Queue
    from .queues import Queue
  File "/usr/lib/python3.6/multiprocessing/queues.py", line 20, in <module>
    from queue import Empty, Full
  File "/home/vivek/Desktop/code/par/queue.py", line 11, in <module>
    q=Queue()
  File "/usr/lib/python3.6/multiprocessing/context.py", line 101, in Queue
    from .queues import Queue
ImportError: cannot import name 'Queue'
Ad

Answer

As you can see in the import chain listed in the error traceback, Python is trying to import the Queue definition from:

/home/vivek/Desktop/code/par/queue.py

This indicates you have somehow broken Python import logic as usually it prioritizes modules in /lib/usr/lib folders. This usually happens if you set your custom PYTHONPATH environment variable or if you mess with module variables such as sys.path.

Quick fix is to rename your file from queue.py to something else.

Ad
source: stackoverflow.com
Ad