25

I'm trying to run the following in Eclipse (using PyDev) and I keep getting error :

q = queue.Queue(maxsize=0) NameError: global name 'queue' is not defined

I've checked the documentations and appears that is how its supposed to be placed. Am I missing something here? Is it how PyDev works? or missing something in the code? Thanks for all help.

from queue import *

def worker():
    while True:
        item = q.get()
        do_work(item)
        q.task_done()

def main():

    q = queue.Queue(maxsize=0)
    for i in range(num_worker_threads):
         t = Thread(target=worker)
         t.daemon = True
         t.start()

    for item in source():
        q.put(item)

    q.join()       # block until all tasks are done

main()

Using: Eclipse SDK

Version: 3.8.1 Build id: M20120914-1540

and Python 3.3

Bain
  • 834
  • 6
  • 11
  • 17

5 Answers5

27

You do

from queue import *

This imports all the classes from the queue module already. Change that line to

q = Queue(maxsize=0)

CAREFUL: "Wildcard imports (from import *) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools". (Python PEP-8)

As an alternative, one could use:

from queue import Queue

q = Queue(maxsize=0)
Renato Byrro
  • 3,578
  • 19
  • 34
David Robinson
  • 77,383
  • 16
  • 167
  • 187
  • Thank you! Now why does it do that? I thought importing them all via the * works better or something. – Bain Jan 29 '13 at 14:20
  • 5
    It's not about working better or worse- it's that when you use the `from queue` at the top you no longer need to specify `queue.` when you call it. In some ways that is "better" (it's a shorter line of code) in some ways it is "worse" (someone reading the code doesn't know where that class came from) – David Robinson Jan 29 '13 at 14:31
  • 1
    It should probably be noted that this is considered bad practice for big projects - "PEP-8 recommends avoiding wildcard imports ( from some_module import * ), and it’s absolutely right. One of the most exciting reasons is that it opens your code up to some interesting ways to break in a multideveloper environment." [How to Make Mistakes in Python](http://www.oreilly.com/programming/free/how-to-make-mistakes-in-python.csp) – NoBugs Jul 29 '16 at 06:33
7

That's because you're using : from queue import *

and then you're trying to use :

queue.Queue(maxsize=0) 

remove the queue part, because from queue import * imports all the attributes to the current namespace. :

Queue(maxsize=0) 

or use import queue instead of from queue import *.

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
1

If you import from queue import * this is mean that all classes and functions importing in you code fully. So you must not write name of the module, just q = Queue(maxsize=100). But if you want use classes with name of module: q = queue.Queue(maxsize=100) you mast write another import string: import queue, this is mean that you import all module with all functions not only all functions that in first case.

Vladyslav
  • 2,018
  • 4
  • 18
  • 44
0

make sure your code is not under queue.py rename it to something else. if your file name is queue.py it will try to search in the same file.

-5

You Can install kombu with pip install kombu

and then Import queue Just like this

from kombu import Queue

Mark Francis
  • 340
  • 4
  • 10