470,625 Members | 1,960 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,625 developers. It's quick & easy.

how to "source" a file?

I have a set of configuration files that are expressed in
python. Users can set up triggers and callbacks as
python function calls.

Some of these functions have become shared between several
products, so I would like to factor out these functions into
a separate file.

Can someone give me a clue as to the best way to proceed?
I'm trying various combinations of import, but with no success.

Can someone loan me a clue?

Many TIA,
Mark

--
Mark Harrison
Pixar Animation studios
Jul 18 '05 #1
3 2208
Mark Harrison wrote:
Can someone give me a clue as to the best way to proceed?
I'm trying various combinations of import, but with no success.


What does "with no success" mean? What have you done, what have you
expected, what happened?

Mathias
Jul 18 '05 #2
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']
Many TIA,
Mark
Jul 18 '05 #3
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 (ha**@zephyrfalcon.org)
http://zephyrfalcon.org/

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Phil Powell | last post: by
18 posts views Thread by Lorem Ipsum | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.