1

I keep getting the error "Database.db_search(search) NameError: name 'Database' is not defined".

If anybody could help me I would really appreciate it, I am also new to Stack Overflow so I apologize for any errors I may have made in regards to the overall layout of asking a question.

Thanks.

Database File:

from Staff import *

def db_search(self):
   query = "SELECT * FROM customer_information WHERE Last_Name = %s"
   cursor.execute(query, (last_name,))
   for (Last_Name) in cursor:
        print(Last_Name)
   return last_name

Staff File:

from Database import *
def gett():
    search = search_entry.get()
    Database.db_search(search)`
greg_data
  • 2,247
  • 13
  • 20
Notorious
  • 41
  • 1
  • 7
  • In addition to my answer, I would like to ask why you have some circular importing going on? Why does `Database` need to import all of `Staff`, if `Staff` also imports all of `Database`?? – greg_data Dec 01 '17 at 14:23
  • Hi Greg, really appreciate you taking the time to look at my code, the reason I have that circular importation going on it that there are other methods/functions that are going on elsewhere in the program. I can show you if you like and you could give me any tips on how to improve. Really appreciate the time mate. Thanks again and have a good evening. – Notorious Dec 01 '17 at 20:28
  • Hey Notorious. I'll be honest, I'm not going to look through all your code and suggest improvements, I just don't have the time. All I will say is that usually, circular imports are a bad idea (http://stackabuse.com/python-circular-imports/). Usually it will be the case that if you review the contents of each module, you will realise a way to better group functionality together which removes the need for circular dependencies. Not always the case, and sometimes valid to have circular deps, but most often signals poor design and may introduce side effects/more weird bugs. – greg_data Dec 04 '17 at 11:35

1 Answers1

1

By using

from Database import *

You've just imported all the contents of Database, so to get the thing to work you would now just need to call

db_search(search)

from module import * isn't really a recommended pattern, as you end up rather polluting your namespace (possibility of confusion between methods from different packages).

So if you change your import line to just:

import Database

then your code will work fine.

greg_data
  • 2,247
  • 13
  • 20
  • I understood that. The OP would also be notified of the comment. The thing that prompted me to comment is that the OP has [asked another question](https://stackoverflow.com/questions/61108639/wait-for-button-to-be-pressed-java-gui). I sometimes check their earlier questions to see if they are accepting answers from earlier questions. If they have yet to do so, I give them a nudge. – Andrew Thompson Apr 11 '20 at 22:57
  • Fair play. Good thing to do. Carry on :) – greg_data Apr 14 '20 at 07:38