Also as a side point... It's best to anonymize the code as best you can so people cannot attach your code to your site (IP theft and security risk)<br><br><div class="gmail_quote">On Wed, Jan 20, 2010 at 8:38 AM, MRAB <span dir="ltr"><<a href="mailto:python@mrabarnett.plus.com">python@mrabarnett.plus.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="im">Victor Subervi wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi;<br>
I think I finally have an interesting problem for y'all. I need to import a script from a lower dir, forcing me to change dirs:<br>
<br>
cwd = os.getcwd()<br>
os.chdir('%s/..' % cwd)<br>
sys.path.append(os.getcwd())<br>
from templateFrame import top, bottom<br>
os.chdir(cwd)<br>
<br>
</blockquote></div>
There's no need to actually change current directory. Just do this:<br>
<br>
sys.path.append(os.path.dirname(os.getcwd()))<div class="im"><br>
from templateFrame import top, bottom<br>
<br>
</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="im">
Because I've found I must do my form evaluations *before* changing dir as above, I am forced to call these values as globals:<br>
<br>
form = cgi.FieldStorage()<br>
store = form.getfirst('store')<br>
cat = form.getfirst('cat')<br>
id = form.getfirst('id')<br>
pkg = form.getfirst('pkg')<br>
patientID = form.getfirst('patientID')<br>
<br>
Now, apparently because of python's problem with globals, when I call "id" as follows:<br>
<br>
cursor.execute('select ProductID from productsPackages where PackageID=%s' % id)<br>
<br>
I get the following error:<br>
<br></div>
/var/www/html/<a href="http://angrynates.com/cart/Store_frame2.py" target="_blank">angrynates.com/cart/Store_frame2.py</a> <<a href="http://angrynates.com/cart/Store_frame2.py" target="_blank">http://angrynates.com/cart/Store_frame2.py</a>><div class="im">
<br>
135 cursor.close()<br>
136 bottom()<br>
137<br>
138 Store_frame2()<br>
139<br>
Store_frame2 = <function Store_frame2><br></div>
/var/www/html/<a href="http://angrynates.com/cart/Store_frame2.py" target="_blank">angrynates.com/cart/Store_frame2.py</a> <<a href="http://angrynates.com/cart/Store_frame2.py" target="_blank">http://angrynates.com/cart/Store_frame2.py</a>> in Store_frame2()<div class="im">
<br>
119 printAProduct()<br>
120 else:<br>
121 cursor.execute('select ProductID from productsPackages where PackageID=%s' % id)<br>
122 for id in [itm[0] for itm in cursor]:<br>
123 printAProduct(id)<br>
global cursor = <MySQLdb.cursors.Cursor object>, cursor.execute = <bound method Cursor.execute of <MySQLdb.cursors.Cursor object>>, global id = '1'<br>
<br>
UnboundLocalError: local variable 'id' referenced before assignment<br>
args = ("local variable 'id' referenced before assignment",)<br>
<br>
Full code below. Please advise.<br>
<br>
</div></blockquote>
Line 121 refers to 'id', but line 122 assigns to 'id' (the 'for' loop).<br>
<br>
In a function, if you assign to a variable then that variable defaults<br>
to being local. You're trying to use the local variable before assigning<br>
a value to it. Having a local variable called 'id' means that you can't<br>
refer to the global variable of the same name.<br>
<br>
You're also assigning to 'i' on line 113, but not referring to it<br>
elsewhere in the function.<br><font color="#888888">
-- <br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></blockquote></div><br><br clear="all"><br>-- <br><a href="http://www.astorandblack.com">http://www.astorandblack.com</a><br><br>--<br><br><br><br><br><br>