By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,768 Members | 2,054 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,768 IT Pros & Developers. It's quick & easy.

global name is not defined

P: n/a
I'm getting an error msg I don't understand, "global name EMR_globals
is not defined", and could use some help.

I've separated the application I'm building into several modules. One
of the modules holds variables I need to pass from one module to
another and is called 'EMR_globals'. Several other modules hold
functions or user menus and then 'EMR_main' controls the initial user
interaction. I'm using MySQL to hold the data.

The initial connection to the database is done by 'EMR_main'.
Functions then define and close a cursor for various queries. The
connection variable, 'conn', is defined 'conn = "" ' in EMR_globals
and then used in EMR_main. Unfortunately when a module.function
attempts to use it I get the error msg.

Here is the source of the error, module 'name_lookup':

def name_find(namefrag):

cursor = EMR_globals.conn.cursor(MySQLdb.cursors.DictCursor )
cursor.execute("SELECT patient_ID, firstname, lastname FROM
demographics WHERE lastname LIKE '%s%%'" % (namefrag))

results = cursor.fetchall()

for index, row in enumerate(results):
print "%d %s %s %s" % (index, row["patient_ID"],
row["firstname"], row["lastname"])

indx = int(raw_input("Select the record you want: "))
results_list = list(results)
a = str(results_list[indx]['patient_ID'])
print 'You have chosen patient ID # ' + a

cursor.execute("SELECT * FROM demographics WHERE patient_ID = %s"
% (a,))
selected_pt = cursor.fetchall()
# if this query returns more than one record the following code will
fail I think
print menus.menu_demographics(selected_pt['firstname'],
selected_pt['lastname'],
selected_pt['address'],
selected_pt['city'],
selected_pt['state'],
selected_pt['zipcode'],
selected_pt['phonenumber'])
print menus.menu_pt_record

cursor.close()
Thanks for any help. Mike

Nov 6 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
En Tue, 06 Nov 2007 18:57:12 -0300, barronmo <ba******@gmail.comescribió:
I'm getting an error msg I don't understand, "global name EMR_globals
is not defined", and could use some help.

I've separated the application I'm building into several modules. One
of the modules holds variables I need to pass from one module to
another and is called 'EMR_globals'. Several other modules hold
functions or user menus and then 'EMR_main' controls the initial user
interaction. I'm using MySQL to hold the data.
Global variables usually are not a good design decision, but that's not
your current problem.
The initial connection to the database is done by 'EMR_main'.
Functions then define and close a cursor for various queries. The
connection variable, 'conn', is defined 'conn = "" ' in EMR_globals
and then used in EMR_main. Unfortunately when a module.function
attempts to use it I get the error msg.
In Python, "global" means "global to the module". If conn is defined in
EMR_globals, when you want to use it elsewhere, you can:

a)
from EMR_globals import conn
....
cursor = conn.cursor(...)

b)
import EMR_globals
....
cursor = EMR_globals.conn.cursor(...)

Usually those import statements are placed at the top of the module.
cursor.execute("SELECT * FROM demographics WHERE patient_ID = %s"
% (a,))
selected_pt = cursor.fetchall()
# if this query returns more than one record the following code will
fail I think
print menus.menu_demographics(selected_pt['firstname'],
selected_pt['lastname'], ...
I think it will fail even with one record, because fetchall() returns a
list of rows. Try using fetchone(). Anyway, if patient_ID is the primary
key, you should always get a single row.

--
Gabriel Genellina

Nov 6 '07 #2

P: n/a
Looks like you forgot to import EMR_globals, EMR_main, etc.
-----Original Message-----
From: py*****************************************@python .org
[mailto:py***************************************** @python.org
] On Behalf Of barronmo
Sent: Tuesday, November 06, 2007 2:57 PM
To: py*********@python.org
Subject: global name is not defined

I'm getting an error msg I don't understand, "global name EMR_globals
is not defined", and could use some help.

I've separated the application I'm building into several modules. One
of the modules holds variables I need to pass from one module to
another and is called 'EMR_globals'. Several other modules hold
functions or user menus and then 'EMR_main' controls the initial user
interaction. I'm using MySQL to hold the data.

The initial connection to the database is done by 'EMR_main'.
Functions then define and close a cursor for various queries. The
connection variable, 'conn', is defined 'conn = "" ' in EMR_globals
and then used in EMR_main. Unfortunately when a module.function
attempts to use it I get the error msg.

Here is the source of the error, module 'name_lookup':

def name_find(namefrag):

cursor = EMR_globals.conn.cursor(MySQLdb.cursors.DictCursor )
cursor.execute("SELECT patient_ID, firstname, lastname FROM
demographics WHERE lastname LIKE '%s%%'" % (namefrag))

results = cursor.fetchall()

for index, row in enumerate(results):
print "%d %s %s %s" % (index, row["patient_ID"],
row["firstname"], row["lastname"])

indx = int(raw_input("Select the record you want: "))
results_list = list(results)
a = str(results_list[indx]['patient_ID'])
print 'You have chosen patient ID # ' + a

cursor.execute("SELECT * FROM demographics WHERE patient_ID = %s"
% (a,))
selected_pt = cursor.fetchall()
# if this query returns more than one record the following code will
fail I think
print menus.menu_demographics(selected_pt['firstname'],
selected_pt['lastname'],
selected_pt['address'],
selected_pt['city'],
selected_pt['state'],
selected_pt['zipcode'],
selected_pt['phonenumber'])
print menus.menu_pt_record

cursor.close()
Thanks for any help. Mike

--
http://mail.python.org/mailman/listinfo/python-list
Nov 6 '07 #3

P: n/a
Thanks, seems to be fixed with importing MySQLdb, menus, EMR_main, etc
in the Name_find module. Is there a better way to do things? I
thought I was avoiding using global variables by putting the shared
ones in their own module.

Thanks for the help.

Mike

Nov 7 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.