Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'module' object has no attribute

Tags:

python

I've been scouring the internet for a solution and everything i've come across hasn't helped. So now i turn to you.

Traceback (most recent call last):
  File "cardreader.py", line 9, in <module>
    import ATRdb as ATR
  File "/home/pi/Desktop/CardReader/ATRdb.py", line 4, in <module>
    import cardreader
  File "/home/pi/Desktop/CardReader/cardreader.py", line 113, in <module>
    main()
  File "/home/pi/Desktop/CardReader/cardreader.py", line 40, in main
    getData(db)
  File "/home/pi/Desktop/CardReader/cardreader.py", line 98, in getData
    if ATR.checkPerms(db,track1):
AttributeError: 'module' object has no attribute 'checkPerms'

I have two files cardreader.py & ATRdb.py

---ATRdb.py has this setup

import sys
import MYSQLdb
import datetime
import cardreader

def checkPerms(db, securitycode):
    try:
       cursor = db.cursor()
       cursor.execute("""SELECT permissions FROM atrsecurity.employee WHERE securitycode = %s""", (securitycode))
       r = cursor.fetchone()
       Permissions = r
       if '3' in Permissions[0]:
          return True
       else:
          return False
     except Exception:
         cardreader.main()
         return False

---cardreader.py has this setup

import sys
import usb.core
import usb.util
import MYSQLdb
import ATRdb as ATR

def main():
    db = MYSQLdb.connect(HOST,USER, PASS, DB)
    print("Please swipe your card...")
    getData(db)
    main()
    db.close()
def getData(db):
    #
    #lots of code to get card data
    #
    if ATR.checkPerms(db, track1):
       print ("User has permission")
       unlockDoor()

i get the error at the "If ATR.checkPerms():" part. Any help would be appreciated (first python project)

like image 502
MaylorTaylor Avatar asked Feb 14 '23 08:02

MaylorTaylor


1 Answers

Your problem is circular imports.

In cardreader, you do this:

import ATRdb as ATR

That starts importing ATRdb, but a few lines into the code, it hits this:

import cardreader

The exact sequence from here depends on whether cardreader.py is your main script or not, and on whether your top-level code that calls main is protected by an if __name__ == '__main__' guard (and assuming that top-level code is in cardreader rather than elsewhere). Rather than try to explain all the possibilities in detail (or wait for you to tell us which one matches your actual code), let's look at what we know is true based on the behavior:

In some way, you're calling main before finishing the import of ATRdb.

This means that, at this point, ATRdb has nothing in it but sys, MYSQLdb, and datetime (and a handful of special attributes that every module gets automatically). In particular, it hasn't gotten to the definition of checkPerms yet, so no such attribute exists in the module yet.

Of course eventually it's going to finish importing the rest of ATRdb, but at that point it's too late; you've already called main and it tried to call ATR.checkPerms and that failed.

While there are various complicated ways to make circular imports work (see the official FAQ for some), the easiest and cleanest solution is to just not do it. If ATRdb needs some functions that are in cardreader, you should probably factor those out into a third module, like cardutils, that both ATRdb and cardreader can import.

like image 155
abarnert Avatar answered Feb 17 '23 03:02

abarnert