473,288 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

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

Nov 6 '07 #1
3 11473
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Eric Lilja | last post by:
Hello, I have a few global variables in my program. One of them holds the name of the application and it's defined in a header file globals.hpp (and the point of definition also happen to be the...
11
by: Capstar | last post by:
Hi, I am working on an application, which will run embedded without an OS. The app is build up out of a couple of well defined parts. At first I wanted to keep those parts seperated and use...
3
by: Anjali Lourda | last post by:
Hi, I have defined a function in global.asax file. Could somebody please tell me how i am supposed to call that function from the other files of the same project. Global.asax public function...
12
by: a | last post by:
def fn(): for i in range(l) global count count= .... how do i declare count to be global if it is an array subsequently i should access or define count as an array error:
8
by: Rob T | last post by:
When I was using VS2003, I was able to compile my asp.net project locally on my machine and copy it to the production server and it would run just fine. I've now converted to VS2005. The project...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
2
by: pythonnewb | last post by:
I am fairly new to programming but have some very basic Java background. I am just learning python and tried to make a module that would allow me to create a file containing an address book. I was...
0
by: Gary Herron | last post by:
Jacob Davis wrote: Yuck, YUCK, YUCK! You are breaking *so* many good-programming-practices, I hardly know where to start. First off: A python global is not what you think. There are *no*...
4
by: RgeeK | last post by:
I have a main module doStuff.py and another module utility.py. At the start of doStuff.py I call import utility.py Then I also proceed to initiallize some global variables sName = "" ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.