dynamically importing a module and function

rkmr.em at gmail.com rkmr.em at gmail.com
Thu Apr 24 00:19:10 EDT 2008


On Mon, Apr 21, 2008 at 4:47 PM, John Machin <sjmachin at lexicon.net> wrote:
> > >  saved = sys.path
> > >  sys.path = data['cwd']
> > >  module = __import__(data['module'])
> > >  sys.path = saved
> > >
> > > > > import os
> > > > > os.chdir('/home/mark/work/proj1')
> > > > > import sys
> > > > > sys.path.append('/home/mark/work/proj1')
> > > > > module = __import__('app')
> > > > > function = getattr(module, 'new')
> > > > > function(1)
> > > > >
> > > >
> > >
> > 1
> >
>
>  It's not at all obvious that the "works in shell" code is the same as the
> code in your script.
>
>  Consider the possibility that as a result of frantic experimentation you
> have multiple copies of app.* with varying contents lying around.
thanks a lot!
there was a file app.pyc lying around in the dir from which i was
running the script...



More information about the Python-list mailing list