8

While working the examples in a book on Parallel Programming in Python, I encountered the following error in code that uses the multiprocessing queue:

    File "C:\pyDev\multiproc\queue-test.py", line 4, in <module>
      queue = multiprocessing.Queue()
    File "C:\Anaconda3\Lib\multiprocessing\context.py", line 100, in Queue
      from .queues import Queue
    File "C:\Anaconda3\Lib\multiprocessing\queues.py", line 20, in <module>
      from queue import Empty, Full

builtins.ImportError: cannot import name 'Empty'

After some experimentation, I determined that all it takes to generate this error is the following code (which is the entirety of the queue-test.py file mentioned in the error message above).

import multiprocessing

if __name__ == "__main__":
    queue = multiprocessing.Queue()

I'm running Python 3.5.1 with Anaconda 4.1.0 on a machine with Windows 7. I have ported the code above and the example from the book to another machine with Python 2.7.11 with Anaconda 2.5.0, with Windows 10 and it works fine on that machine.

Thinking that perhaps there was a corrupt file or other issue with my Python installation, I tried reinstalling Anaconda and it did not help. I performed a Google search and did not find this particular error. Other stackoverflow postings such as the one found here: ImportError: Cannot import name X don't appear to be relevent because this involves part of the Python standard library and not code and classes I wrote myself.

Community
  • 1
  • 1
Patrick R
  • 81
  • 1
  • 2
  • I'm here from Review, and just want to say _thank you_ for being the first new-user question I've seen that's well-constructed, well-researched, and has an MCVE ([mcve]). +1 – KernelPanic Mar 11 '17 at 04:40

1 Answers1

11

I encountered almost the same error in my code and finally figure out where went wrong. Hope it will help you somehow.

I named my python script as "queue.py", and then I run it, I got error info. below just like yours:

Traceback (most recent call last):
File "F:/02_Coding/01_Projects/PyHomeWork/Day23_Process/queue.py", line 19, in <module>
queue = multiprocessing.Queue()
File "E:\02_CodingSoftware\02_Installed\Anaconda3\lib\multiprocessing\context.py", line 101, in Queue
from .queues import Queue
File "E:\02_CodingSoftware\02_Installed\Anaconda3\lib\multiprocessing\queues.py", line 20, in <module>
from queue import Empty, Full
ImportError: cannot import name 'Empty'

I use the below method to create a Queue:

if __name__ =="__main__":
queue = multiprocessing.Queue()

Finally, I notice that I should not named the file in a name of "queue.py", it seems it will cause a misleading to python when it interpreter the script. And after I rename the script, the error is gone. What a stupid mistake, LoL.

So my suggestion is maybe you can check whether under your folder is there a script named as "queue.py" or any custom module would conflict with that in libraries.

Hope you can solve the issue. Best

Adam.Shen
  • 111
  • 1
  • 4