92

I am trying to import requests module, but I got this error my python version is 3.4 running on ubuntu 14.04

>>> import requests
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/connectionpool.py", line 10, in <module>
    from queue import LifoQueue, Empty, Full
ImportError: cannot import name 'LifoQueue'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.4/dist-packages/requests/__init__.py", line 58, in <module>
   from . import utils
  File "/usr/local/lib/python3.4/dist-packages/requests/utils.py", line 26, in <module>
    from .compat import parse_http_list as _parse_list_header
  File "/usr/local/lib/python3.4/dist-packages/requests/compat.py", line 7, in <module>
    from .packages import chardet
  File "/usr/local/lib/python3.4/dist-packages/requests/packages/__init__.py", line 3, in <module>
    from . import urllib3
  File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/__init__.py", line 10, in <module>
    from .connectionpool import (
  File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/connectionpool.py", line 12, in <module>
    from Queue import LifoQueue, Empty, Full
ImportError: No module named 'Queue'
Ali Faki
  • 3,928
  • 3
  • 17
  • 25

7 Answers7

171

import queue is lowercase q in Python 3.

Change Q to q and it will be fine.

(See code in https://stackoverflow.com/a/29688081/632951 for smart switching.)

Community
  • 1
  • 1
Pacerier
  • 86,231
  • 106
  • 366
  • 634
  • 9
    This is relevant to people explicitly importing `Queue`, but the OP's traceback makes it clear the `requests` module is trying `queue` first, then falling back to try `Queue` (making it version portable exactly as described). You're answering the question a lot of people having this error have, but not the OP's question at all. – ShadowRanger Sep 22 '17 at 11:54
  • 1
    @ShadowRanger I'm guessing the code failed for OP because the 3.x-specific version (tried first) didn't provide the appropriate names, in turn because it imported `queue.py` from OP's own project rather than the standard library - as in https://stackoverflow.com/questions/36250353 . – Karl Knechtel Jan 19 '23 at 09:16
  • 1
    Ah, I scrolled down and found the OP's own answer, which confirmed my suspicion. This is a dupe, then, and a very misleading one. It should be scrubbed. A shame it got 286k views. – Karl Knechtel Jan 19 '23 at 09:17
61

Queue is in the multiprocessing module so:

from multiprocessing import Queue
DavidW
  • 29,336
  • 6
  • 55
  • 86
peter
  • 671
  • 1
  • 4
  • 3
  • I don't know why this python comed without it. The good replacement for queue.Queue is multiprocessing.JoinableQueue, because allows to simply replace the import without have to edit .join() and .task_done() flow. – m3nda Jun 19 '17 at 21:09
  • 10
    The `multiprocessing.Queue` is a completely different class with a *lot* higher overhead; for threading, you *want* `Queue` from the `queue`(Py3)/`Queue`(Py2) module. `requests` is correctly trying to get it from both names (so it's version agnostic); the failure indicates a completely different problem (as the OP explains in their answer). – ShadowRanger Sep 22 '17 at 11:44
  • Also, since `multiprocessing.Queue` is pickling based, it's not even functionally equivalent; if you have non-picklable objects getting passed between threads, it will succeed with `queue`/`Queue`'s `Queue` class, and fail with `multiprocessing.Queue`. – ShadowRanger Sep 22 '17 at 11:52
37

I solve the problem my issue was I had file named queue.py in the same directory

Ali Faki
  • 3,928
  • 3
  • 17
  • 25
  • 7
    Helped me. It's embarassing but I've created my own custom queue.py file to learn queues. Thanks for answer. – Gunnm Jun 25 '16 at 10:47
  • 1
    Is there a way to import the real module without naming the local `queue.py`? – fritzo Oct 05 '17 at 08:03
31

It's because of the Python version. In Python 2.x it's import Queue as queue; on the contrary in Python 3 it's import queue. If you want it for both environments you may use something below as mentioned here

try:
   import queue
except ImportError:
   import Queue as queue
GPrathap
  • 7,336
  • 7
  • 65
  • 83
  • 1
    You made typo. You should change to this:In Python 3 it's `import queue`; on the contrary in Python2.x it's `import Queue`. – ChrisZZ Sep 28 '20 at 12:10
1

In my case it should be:

from multiprocessing import JoinableQueue

Since in python2, Queue has methods like .task_done(), but in python3 multiprocessing.Queue doesn't have this method, and multiprocessing.JoinableQueue does.

Panfeng Li
  • 3,321
  • 3
  • 26
  • 34
1

I run into the same problem and learn that queue module defines classes and exceptions, that defines the public methods (Queue Objects).

Ex.

workQueue = queue.Queue(10)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
asfawh
  • 29
  • 1
-2

I just copy the file name Queue.py in the */lib/python2.7/ to queue.py and that solved my problem.

xiaojueguan
  • 870
  • 10
  • 19