Using Queue in python

前端 未结 5 1688
无人及你
无人及你 2021-02-06 23:31

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<

相关标签:
5条回答
  • 2021-02-07 00:12

    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)
    
    0 讨论(0)
  • 2021-02-07 00:12

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

    0 讨论(0)
  • 2021-02-07 00:17

    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.

    0 讨论(0)
  • 2021-02-07 00:18

    You Can install kombu with pip install kombu

    and then Import queue Just like this

    from kombu import Queue

    0 讨论(0)
  • 2021-02-07 00:34

    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.

    0 讨论(0)
提交回复
热议问题