Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

builtins.ImportError: cannot import name 'Empty'

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.

like image 428
Patrick R Avatar asked Mar 10 '17 17:03

Patrick R


People also ask

How to Fix ImportError cannot import name?

If the error occurs due to a circular dependency, it can be resolved by moving the imported classes to a third file and importing them from this file. If the error occurs due to a misspelled name, the name of the class in the Python file should be verified and corrected.

How to Fix cannot import name error in Python?

The Python "ImportError: cannot import name" occurs when we have circular imports (importing members between the same files). To solve the error, move the objects to a third file and import them from a central location in other files, or nest one of the imports in a function.

How to Fix an import error in Python?

Python's ImportError ( ModuleNotFoundError ) indicates that you tried to import a module that Python doesn't find. It can usually be eliminated by adding a file named __init__.py to the directory and then adding this directory to $PYTHONPATH .


1 Answers

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

like image 184
Adam.Shen Avatar answered Sep 23 '22 23:09

Adam.Shen