472,364 Members | 2,144 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

imported module no longer available

I've come across an error that i'm not yet able to create a test case
for but wanted to get see if someone could shed light on this.

I have imported a module at the top of my file with
import mymodulename

this module is used many times in the current file successfully, but
then I attempt to use it one more time and get: UnboundLocalError:
local variable 'mymodulename' referenced before assignment

if i add `print globals().keys()` right before the line where the
error occurs l see the module in the list (actual output, names
changed to protect the innocent)
['HPADao', 'RDao', 'DriverBase', 'FKSUtility', 'PMDao',
'mymodulename', 'IDriver', 'DriverTrack', 'HPPDao', 'setLogName',
'HPDao', 'iparser', '__builtins__', '__file__', 'driver', '_LOGNAME',
'sys', 'IClient', '__name__', 'copy', 'types', 'logging', 'iloader',
'HPADao', '__doc__', 'PMDao', 'time', 'FormatLoad']
Traceback (most recent call last):
File "testunbound.py", line 475, in <module>
driver.getRData('0605', 22528)
File "testunbound.py", line 256, in getRData
print mymodulename

the code that generates these two lines is:
print globals().keys()
print mymodulename

I can solve this by placing
import mymodulename again in this method, but can't have that everywhere.

I will try to come up with a generic reproduction of this issue, but
that might take some time. I've also seen this in the past with the
python builtin 'sys'
I did see a bug http://bugs.python.org/issue2378, which has very
similar behaviour, but is different.

Python 2.5, ubuntu hardy

Thanks for any insight,
Jeff
Jul 21 '08 #1
1 1249

"Jeff Dyke" <je*******@gmail.comwrote in message
news:ma************************************@python .org...
I've come across an error that i'm not yet able to create a test case
for but wanted to get see if someone could shed light on this.

I have imported a module at the top of my file with
import mymodulename

this module is used many times in the current file successfully, but
then I attempt to use it one more time and get: UnboundLocalError:
local variable 'mymodulename' referenced before assignment

if i add `print globals().keys()` right before the line where the
error occurs l see the module in the list (actual output, names
changed to protect the innocent)
['HPADao', 'RDao', 'DriverBase', 'FKSUtility', 'PMDao',
'mymodulename', 'IDriver', 'DriverTrack', 'HPPDao', 'setLogName',
'HPDao', 'iparser', '__builtins__', '__file__', 'driver', '_LOGNAME',
'sys', 'IClient', '__name__', 'copy', 'types', 'logging', 'iloader',
'HPADao', '__doc__', 'PMDao', 'time', 'FormatLoad']
Traceback (most recent call last):
File "testunbound.py", line 475, in <module>
driver.getRData('0605', 22528)
File "testunbound.py", line 256, in getRData
print mymodulename

the code that generates these two lines is:
print globals().keys()
print mymodulename

I can solve this by placing
import mymodulename again in this method, but can't have that everywhere.

I will try to come up with a generic reproduction of this issue, but
that might take some time. I've also seen this in the past with the
python builtin 'sys'
I did see a bug http://bugs.python.org/issue2378, which has very
similar behaviour, but is different.

Python 2.5, ubuntu hardy

Thanks for any insight,
Jeff
That error will occur if somewhere in a function or method you accidently
assign another value to a global variable, without declaring it with the
global keyword first, even if it occurs later in the function. Example:

import os
print os.getcwd()
def func():
print globals().keys()
print os
os=1
func()

OUTPUT:

c:\
['__builtins__', '__file__', 'func', '__name__', 'os', '__doc__']
Traceback (most recent call last):
File
"C:\dev\python\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py" ,
line 414, in ImportFile
exec codeObj in __main__.__dict__
File "<auto import>", line 1, in <module>
File "test.py", line 10, in <module>
func()
File "test.py", line 7, in func
print os
UnboundLocalError: local variable 'os' referenced before assignment

Check in the function that caused the error to see if later in the code the
module name is being used as a local variable.

--Mark

Jul 21 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Thomas Mlynarczyk | last post by:
Hello, My provider has set register_globals = On and I can't change the php.ini file. Is there a way to unset all the imported get/post etc. variables at the beginning of my script? Thomas
5
by: James Tauber | last post by:
Had a question from a colleague that I embarrassingly couldn't answer. He has a script, foo.py with a global. He wants to import bar.py and needs that global available in bar.py The following...
3
by: Jason | last post by:
Hi, I've been having trouble understanding the difference between global namespace within and between modules. For example, I can program a subthread to see a global name (a threaded event to...
4
by: Martin M. | last post by:
Hi, I have the following question: How can an imported module see/find the path to itself? Background: From my main script I import a module which needs a file (AppleScript) located in the...
10
by: Michael Abbott | last post by:
It seems to be an invariant of Python (insofar as Python has invariants) that a module is executed at most once in a Python session. I have a rather bizzare example that breaks this invariant: can...
0
by: Fredrik Lundh | last post by:
Jeff Dyke wrote: so how did that processing use the "mymodulename" name? the calling method has nothing to do with what's considered to be a local variable in the method being called, so...
3
by: Mohamed Yousef | last post by:
Hello , The problem I'm asking about is how can imported modules be aware of other imported modules so they don't have to re-import them (avoiding importing problems and Consicing code and...
0
by: Mohamed Yousef | last post by:
Hello , The problem I'm asking about is how can imported modules be aware of other imported modules so they don't have to re-import them (avoiding importing problems and Consicing code and...
0
by: Gabriel Genellina | last post by:
En Sun, 24 Aug 2008 07:34:41 -0300, Mohamed Yousef <harrrrpo@gmail.comescribió: Yes. That way, when you see a name "foo" used in a module, you can look at the imports to see where it comes from....
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

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.