queue ImportError in python 3

Question:

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
Asked By: Liondancer

||

Answers:

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).

Answered By: Mike Vella

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
Answered By: m9_psy

for ImportError: No module named ‘Queue’ in Python3, just replace the sentence “import Queue” with “import queue as Queue“.

Answered By: Dhawaleswar

A kinda standard cross py2-py3 compatible version:

try: 
    import queue
except ImportError:
    import Queue as queue
Answered By: sorin

If you are using python3, try

from queue import Queue
queue = Queue()

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

Answered By: Greta

I am using python 3.6

import queue as Queue

workQueue = Queue.Queue(10)

This method works for me.

Answered By: Sachitha Hirushan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.