26

I am not sure why I am getting this ImportError. queue.Queue() is in the documentation.

https://docs.python.org/3/library/queue.html?highlight=queue#queue.Queue

I am using it in a function like so:

node_queue = queue.Queue()

error:

Traceback (most recent call last):
  File "./test_jabba.py", line 15, in <module>
    from utils import gopher, jsonstream, datagen, event_gen, tree_diff, postal
  File "/Users/bli1/Development/QE/TrinityTestFramework/poc/utils/tree_diff.py", line 5, in <module>
    import queue
ImportError: No module named queue

Line 5 is import queue:

#!/usr/bin/env python3
import sys                      # access to basic things like sys.argv
import os                       # access pathname utilities
import argparse                 # for command-line options parsing
import queue
13
  • 1
    Do you have a vanilla python implementation? Where/how did you download/install it? Commented Apr 16, 2015 at 23:51
  • 4
    Also, can you run python --version? Its possible you're actually running Python 2.x, in which case the queue module was named Queue Commented Apr 16, 2015 at 23:53
  • @Dannnno the command gave me 2.7.6. I thought if I had the #!/usr/bin/env python3 on top it wouldn't matter?
    – Liondancer
    Commented Apr 16, 2015 at 23:54
  • I'm not an expert on shebang lines so I'm not positive exactly how those work. From a cursory google search what you have should work though. I'm assuming you're running this on a Linux machine? Commented Apr 16, 2015 at 23:57
  • How are you invoking your script? If you say python tree_diff.py, the shebang won't matter. It's only inspected if you execute the file directly, like ./tree_diff.py.
    – Erin Call
    Commented Apr 17, 2015 at 0:04

6 Answers 6

31

A kinda standard cross py2-py3 compatible version:

try: 
    import queue
except ImportError:
    import Queue as queue
1
  • This stops the immediate ImportError, but it doesn't do anything about the deeper problem of actually being on Python 2 when you were trying to use Python 3. Commented Dec 14, 2019 at 6:37
30

Another way to avoid version problems is:

import sys
is_py2 = sys.version[0] == '2'
if is_py2:
    import Queue as queue
else:
    import queue as queue
3
  • 1
    There is a simplified solution stackoverflow.com/a/53021998/99834 -- less code, does the same, no side effects.
    – sorin
    Commented Oct 27, 2018 at 12:43
  • @sorin: what do you mean by "no side effects"? you are importing module, catching exception, those are statements with side effects Commented Oct 27, 2018 at 12:48
  • importing sys, which was not needed. anyway what one would use is matter of personal preference, both approaches do work.
    – sorin
    Commented Oct 27, 2018 at 12:51
5

for ImportError: No module named 'Queue' in Python3, just replace the sentence "import Queue" with "import queue as Queue".

1

Replace #!/usr/bin/env python3 with #!/usr/bin/python3

If your env isn't set up correctly then #!/usr/bin/env python3 may not work. If #!/usr/bin/python3 gives the same error then try running /usr/bin/python3 --version in your shell as a sanity check.

If you don't get a sensible output from /usr/bin/python3 --version then you have odd installation of python 3 and I suggest installing it using your package manager (apt-get, yum, homebrew or whatever you prefer - this will probably fix the !#/usr/bin/env issue).

12
  • What happens when you type /usr/bin/python3 --version ?
    – Mike Vella
    Commented Apr 17, 2015 at 0:16
  • I get a no such directory or file
    – Liondancer
    Commented Apr 17, 2015 at 0:17
  • OK - what happens when you type /usr/bin/env python3
    – Mike Vella
    Commented Apr 17, 2015 at 0:17
  • same output. However I did this: mtvl1289dd026:bin bli1$ python3 --version Python 3.4.2 mtvl1289dd026:bin bli1$ pwd /usr/bin
    – Liondancer
    Commented Apr 17, 2015 at 0:18
  • So /usr/bin/env python3 gives you no such directory or file ? That sounds like your problem. What happens when you type which python3
    – Mike Vella
    Commented Apr 17, 2015 at 0:19
1

If you are using python3, try

from queue import Queue
queue = Queue()

ref:https://docs.python.org/3/library/queue.html

0

I am using python 3.6

import queue as Queue

workQueue = Queue.Queue(10)

This method works for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.