how to "source" a file?

Hans Nowak hans at zephyrfalcon.org
Thu Mar 18 05:40:51 CET 2004


Mark Harrison wrote:

> Specifically, I'm confused why
> 
> def dfpostadd(name):
>     """per-unit postprocessing step"""
>     sys.path.append('/my/config/path')
>     import dfacommon
>     dfacommon.addm(name)
> 
> works, but this doesn't:
> 
> sys.path.append('/my/config/path')
> import dfacommon
> def dfpostadd(name):
>     """per-unit postprocessing step"""
>     dfacommon.addm(name)
> 
> which gives me:
> 
> msg=global name 'dfacommon' is not defined trace=['  File "<string>",
> line 341, in doadd\n', '  File "/usr/anim/config/ndfd/mark.cfg",
> line 55, in dfpostadd\n    dfacommon.addm(name)\n'] 

Strange.  Is there any code later on in the file that removes dfacommon from 
the module namespace?  Something like this:

   sys.path.append('/my/config/path')
   import dfacommon
   def dfpostadd(name):
       """per-unit postprocessing step"""
      dfacommon.addm(name)

   # ...other stuff here...

   del dfacommon

Or maybe some functions are extracted from the config file and the module 
itself is discarded, getting rid of the dfacommon global in the process.  In 
such cases, the first function works, because it imports and uses the module 
immediately.  The second function, however, depends on the name 'dfacommon' 
available in global namespace.  Normally this isn't a problem, but apparently 
something unusual is going on with the namespaces here.  I wonder how these 
config files are used/imported...

Cheers,

-- 
Hans (hans at zephyrfalcon.org)
http://zephyrfalcon.org/






More information about the Python-list mailing list