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

5 Answers 5

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)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! Now why does it do that? I thought importing them all via the * works better or something.
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)
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
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 *.

Comments

2

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.

1 Comment

Thanks @Mahadev. I named my file queue.py and it was not working. After renaming the file, the issue was resolved.
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.

Comments

-5

You Can install kombu with pip install kombu

and then Import queue Just like this

from kombu import Queue

1 Comment

Why would anyone do this if Queue is implemented to python by default ?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.