473,394 Members | 1,773 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,394 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 1317

"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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.