does this exception mean something else?

Terry Reedy tjreedy at udel.edu
Thu May 6 22:01:07 EDT 2010


On 5/6/2010 8:33 PM, Alex Hall wrote:
> Hi all,
> I have a file, pasted below for what good it will do, which makes a
> couple conditional calls to a function called writeDefaults. However,
> when I manually trigger a condition that causes the function to be
> called, Python gives me a name error and says that my writeDefaults
> does not exist.

It does not, when you try to call it, because the call occurs before you 
define it. Python code is executed top to bottom and function defines 
are executable statements. I suspect that you have used some other 
language that operates differently.
...
> #first, load the ini file setting
> #so where is the ini? script_path/config.ini, of course!
> iniLoc=helpers.progdir+'\\config.ini'
> iniLoc=r'c:\arm\config.ni'
> #set up the ini object to read and write settings
> if(os.path.exists(iniLoc)):
>   ini=ConfigObj(iniLoc)
> else:
>   speak("The i n i file was not found. Now generating a default file.")
>   writeDefaults()
> #end except

writeDefaults is not yet defined

...
> def writeDefaults():
>   global ini
>   global iniLoc
>   ini.filename=iniLoc
>   ini["general"]=general
>   ini["enableModes"]=enableModes
>   ini["weatherOptions"]=weatherOptions
>   ini["armOptions"]=armOptions
>   ini["templates"]=templates
>   #create the new file
>   ini.write()
> #end def

Now it is. Move you def statements up closer to the top of the file.

Terry Jan Reedy




More information about the Python-list mailing list