[Tutor] functions in other files, executing

Emil Styrke emil@lysator.liu.se
18 Oct 2002 21:56:59 +0200


Thomi Richards <thomi@thomi.imail.net.nz> writes:

> yes, i am aware of the import function. the problem i am trying to get
> around however, is that i have about 20-50 to import, an each one is in
> a different directory. i know where the directories are, but i do not
> want to fill up sys.path with a whole heap of directories...
> 

Maybe the imp module could be interesting?  The following code is
untested, though.

>>> def load_from_path(name, path):
>>>     (file, pathname, desc) = imp.find_module(name, path)
>>>     return imp.load_module(name, file, pathname, desc)

        /Emil


> then again, maybe this is OK?
> 
> On Thu, 17 Oct 2002 10:54:44 +0100 Thus said alan.gauld@bt.com:
> 
> > > I am assuming that there is an easier way to execute a function from
> > > another file, assuming that you know it's location, and the function
> > > name than importing the file as a module?? 
> > 
> > There might be another way(although I can't think of it!) 
> > but the easiest way is to import the module or import 
> > the function name:
> > 
> > import mod
> > mod.foo()   # more typing but safest
> > 
> > OR
> > 
> > from mod import foo   # usually a bad idea...
> > foo()
> > 
> > Alan g.
> > Author of the 'Learning to Program' web site
> > http://www.freenetpages.co.uk/hp/alan.gauld
> > 
> > _______________________________________________
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> 
> 
> -- 
>  "Avoid the Gates of Hell.  Use Linux"
> 
> Thomi Richards,
> thomi@imail.net.nz
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor