Re: Loading functions from a file during run-time

From: Grant Edwards (grante_at_visi.com)
Date: 02/11/05


Date: 11 Feb 2005 04:31:27 GMT

On 2005-02-11, Bryant Huang <735115@gmail.com> wrote:

> I would like to read in files, during run-time, which contain
> plain Python function definitions, and then call those
> functions by their string name. In other words, I'd like to
> read in arbitrary files with function definitions, using a
> typical 'open()' call, but then have those functions available
> for use.

that's pretty simple:

  $ cat foo.txt
  def foo():
      print "foo here"
  def bar():
      print "bar here"
      
  $ cat foo.py
  filename = 'foo.txt'
  execfile(filename)
  foo()
  bar()
  
  $ python foo.py
  foo here
  bar here

> The 'import' keyword is not appropriate, AFAIK, because I want to be
> able to open any file, not one that I know ahead of time (and thus can
> import at design-time).

This will import a module name determined at run-time:

  exec('import %s' % moduleName)

> The following is a toy example of what I'm doing so far.

[...]

> I'm not sure exactly what happens below the surface, but I'm
> guessing the 'compile()' and 'exec()' commands load in
> 'negate()' and 'square()' as functions in the global scope of
> 'foo.py'. I find that when I run 'compile()' and 'exec()' from
> within a function, say 'f()', the functions I read in from
> 'bar.txt' are no longer accessible since they are in global
> scope, and not in the scope of 'f()'.

Huh? I'm lost. What, exactly, are you trying to accomplish?

Did your example program do what you intended or not?

Is this what you're trying to do?

  $ cat foo.txt
  def foo():
      print "foo here"
  def bar():
      print "bar here"
  
  $ cat bar.py
  def testing():
      filename = 'foo.txt'
      execfile(filename,globals())
      foo()
      bar()
  testing()
  foo()
  bar()
  
  $ python bar.py
  foo here
  bar here
  foo here
  bar here

-- 
Grant Edwards                   grante             Yow!  Clear the
                                  at               laundromat!! This
                               visi.com            whirl-o-matic just had a
                                                   nuclear meltdown!!


Relevant Pages

  • Re: Python 3K or Python 2.9?
    ... function/method argument that might as well be hidden in the compiler ... You could add some syntax to Python such that '.x' was equivalent to ... def factory: ... C.method = foo ...
    (comp.lang.python)
  • Re: How to refer to the current module?
    ... def bar: ... print 'bar with %r' % arg ... python driver.py foo ...
    (comp.lang.python)
  • Re: a Python persons experience with Ruby
    ... I would like to say firstly that I've been using python for a few ... As for ruby, I think the opcodes will help me clarify... ... Notice that #new and #foo are both CALL ops. ... def a; 2; end ...
    (comp.lang.python)
  • Re: How to refer to the current module?
    ... def bar: ... print 'bar with %r' % arg ... python driver.py foo ...
    (comp.lang.python)
  • Re: from __future__ import absolute_import ?
    ... foo not in bar ... Unfortunately this is a side effect of using the os's directory structure to represent a python "package" structure. ...
    (comp.lang.python)