1

I created my own module named queue, and am trying to import it. However, when trying to use it, I get the error 'Queue' object has no attribute 'enqueue'.

How can I import my own queue (which does have an enqueue), and not the standard-library one (which does not)?

def driver():
    import queue
    q = queue.Queue()
    for line in df:
        if 'received' in line:
            q.enqueue(line)
            print("Adding job " + new_item.job_ID + " to the queue with the timestamp: " + new_item.time_stamp + ".")
            print("The prority of the job is: " + new_item.job_priority)
            print("The job type is: " + new_item.job_type)
DennisLi
  • 3,915
  • 6
  • 30
  • 66
  • 2
    You're almost certainly importing `queue` from the standard library. Probably best to rename your `queue` module to avoid confusion – Iain Shelvington Aug 05 '19 at 20:27
  • 1
    Exactly -- you're getting the standard-library `queue`, and its `Queue` implementations have `get()` and `put()`, not `enqueue()`. – Charles Duffy Aug 05 '19 at 20:28
  • 1
    This is very closely related to https://stackoverflow.com/questions/1224741/trying-to-import-module-with-the-same-name-as-a-built-in-module-causes-an-import – Charles Duffy Aug 05 '19 at 20:30
  • Possible duplicate of [Importing from builtin library when module with same name exists](https://stackoverflow.com/questions/6031584/importing-from-builtin-library-when-module-with-same-name-exists) – Vaulstein Aug 06 '19 at 06:22

4 Answers4

1

In Python 2.x, you can gain the ability to disambiguate between your local queue and the standard-library one by adding the following line before any other imports:

from __future__ import absolute_import

...and then using:

import .queue as local_queue
q = local_queue.Queue()

...to get your own implementation, not the standard one.


In Python 3, this behavior is default, so you don't need the from __future__ import to be able to use import .queue to explicitly import from a queue.py in the same package or directory as your current code.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
0

I think the proble is that python is importing it's buit-in module queue, so when instanciating a Queue class, you are creating an object of the pthon built-in type queue.

a solution to this problem is changing the module and the class name, and there is this convention in python when creating a function, a module or class with the same name as a built-in function, module or name is that you should add a _ to the end of your own module and class,

so, may be try

 queue ==> queue_
 Queue ==> Queue_ 
0

Rename your internal file to something like my_queue.py and import it into your file like this. That avoids bad practices like from queue import * and name conflicts with the standard library which is most likely the problem you are running into now.

import my_queue

def driver():
    q = my_queue.Queue()
    for line in df:
        if 'received' in line:
            q.enqueue(line)
            print("Adding job " + new_item.job_ID + " to the queue with the timestamp: " + new_item.time_stamp + ".")
            print("The prority of the job is: " + new_item.job_priority)
            print("The job type is: " + new_item.job_type)
Daniel Butler
  • 3,239
  • 2
  • 24
  • 37
-1

Try this:

from queue import *

q = Queue ()

def driver():
    for line in df:
        if 'received' in line:
            q.enqueue(line)
            print("Adding job " + new_item.job_ID + " to the queue with the timestamp: " + new_item.time_stamp + ".")
            print("The prority of the job is: " + new_item.job_priority)
            print("The job type is: " + new_item.job_type)
  • Why would this import the local queue module if `import queue` imports the standard-library one? It's strictly worse, insofar as it's wildcard imports introduce namespace pollution, and it doesn't change the behavior that's actually causing the OP trouble. – Charles Duffy Aug 05 '19 at 20:30