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
Liondancer
  • 15,721
  • 51
  • 149
  • 255
  • 1
    Do you have a vanilla python implementation? Where/how did you download/install it? – Dan Oberlam Apr 16 '15 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` – Dan Oberlam Apr 16 '15 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 Apr 16 '15 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? – Dan Oberlam Apr 16 '15 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 Apr 17 '15 at 00:04
  • @Dannnno running on OSX – Liondancer Apr 17 '15 at 00:11
  • @AndrewLorente I imported the module to use the function in another file – Liondancer Apr 17 '15 at 00:12
  • That should be fine then, I should have said a Unix based machine – Dan Oberlam Apr 17 '15 at 00:14
  • @AndrewLorente I am running another program like so `./program.py` and this program invokes functions from `tree_diff.py` – Liondancer Apr 17 '15 at 00:16
  • Ah yeah, then the shebang in `tree_diff.py` won't matter at all. It's down to what python is specified by `program.py`. – Erin Call Apr 17 '15 at 00:42
  • I believe the issue you're encountering has to do with how you run your other script (`./test_jabba.py`), not with anything in the `tree_diff` module. Even if you fixed the issue with `queue`, you'd probably have issues elsewhere, since there are a good number of differences between Python 2 and 3. For whatever reason, your script is being run in Python 2, not Python 3 as you intend (and as the `tree_diff` module requires). Unfortunately, you haven't provided much information about how you're running `test_jabba.py`. Does it have a `#!` line at the top? How are you invoking it? – Blckknght Apr 17 '15 at 00:55
  • @Blckknght evey file has `#!/usr/bin/env python3` on top. Might be an installation issue because I do not see `python3` in `/usr/bin/` but it is located in another directory – Liondancer Apr 17 '15 at 01:03
  • How are you running the file? Are you calling it as a script with `./test_jaba.py` from the shell, double clicking on it in a window, or running the interpreter explicitly? The third (and maybe the second) will not make use of the `#!` line. If you're calling `python test_jaba.py`, try using `python3 test_jaba.py` instead. If you're using one of the other approaches to run it, I don't know what the issue is (I'm not very knowledgeable about MacOS), but please describe it more thoroughly, so that perhaps somebody else will know what to do. – Blckknght Apr 17 '15 at 01:37

6 Answers6

31

A kinda standard cross py2-py3 compatible version:

try: 
    import queue
except ImportError:
    import Queue as queue
wihlke
  • 2,455
  • 1
  • 19
  • 18
sorin
  • 161,544
  • 178
  • 535
  • 806
  • 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. – user2357112 Dec 14 '19 at 06: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
Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
m9_psy
  • 3,217
  • 5
  • 25
  • 38
  • 1
    There is a simplified solution https://stackoverflow.com/a/53021998/99834 -- less code, does the same, no side effects. – sorin Oct 27 '18 at 12:43
  • @sorin: what do you mean by "no side effects"? you are importing module, catching exception, those are statements with side effects – Azat Ibrakov Oct 27 '18 at 12:48
  • importing sys, which was not needed. anyway what one would use is matter of personal preference, both approaches do work. – sorin Oct 27 '18 at 12:51
5

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

Dhawaleswar
  • 99
  • 1
  • 5
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).

Mike Vella
  • 10,187
  • 14
  • 59
  • 86
  • What happens when you type `/usr/bin/python3 --version` ? – Mike Vella Apr 17 '15 at 00:16
  • I get a no such directory or file – Liondancer Apr 17 '15 at 00:17
  • OK - what happens when you type `/usr/bin/env python3` – Mike Vella Apr 17 '15 at 00:17
  • same output. However I did this: `mtvl1289dd026:bin bli1$ python3 --version Python 3.4.2 mtvl1289dd026:bin bli1$ pwd /usr/bin ` – Liondancer Apr 17 '15 at 00: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 Apr 17 '15 at 00:19
  • `/Library/Frameworks/Python.framework/Versions/3.4/bin/python3` – Liondancer Apr 17 '15 at 00:20
  • I guess the paths are causing an issue. How would I fix this so that my `shebang` would work in this case? – Liondancer Apr 17 '15 at 00:21
  • OK -r it sounds like your python install is a bit unusual. Put `#!/Library/Frameworks/Python.framework/Versions/3.4/bin/python3` at the top of your file and I expect it will work - this is *not* a permanent solution of course – Mike Vella Apr 17 '15 at 00:21
  • I'm guessing you installed python 3 in a way which was somehow unorthodox? Try installing using your package manager (see my addition to my answer) – Mike Vella Apr 17 '15 at 00:25
  • Okay so it turns out python3 is not located in `/usr/bin/` I tried `brew install python3` but I am getting `Error: python3-3.4.1_1 already installed` – Liondancer Apr 17 '15 at 00:26
  • `ln -s /Library/Frameworks/Python.framework/Versions/3.4/bin/python3 /usr/bin/python3` might work. But really this appears to be an issue with your installation, there will be a way to uninstall and reinstall with brew. – Mike Vella Apr 17 '15 at 00:28
  • Just tried the command and it did not work. I agree it is probably an installation issue and it is not installed in the correct directory – Liondancer Apr 17 '15 at 01:02
1

If you are using python3, try

from queue import Queue
queue = Queue()

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

Greta
  • 27
  • 1
0

I am using python 3.6

import queue as Queue

workQueue = Queue.Queue(10)

This method works for me.