import and function question

Moray Taylor mtaylor at lowebroadway.com
Mon Apr 8 11:19:30 EDT 2002


Alex Martelli <aleax at aleax.it> wrote in message news:<0aes8.59173$pT1.1704009 at news1.tin.it>...
> Moray Taylor wrote:
> 
> > Hi,
> > 
> > I have two questions..
> > 
> > a) Can I 'import' a module with the full path, i.e. I don't want to
> > add it to sys.path, just something like 'import /home/me/mod.py', is
> > this possible.
> > I have tried adding it to the path, importing, and immediatly removing
> > it from the path, but the next time I do an 'import' with a different
> > module of the same name, it picks up the old one, even though it isn't
> > in the sys.path.
> 
> The import statement will always pick up sys.modules[name] when you
> import name.  If that's not what you need (and for plugins you may
> not want that, indeed) see the imp module in the standard library.
> imp.find_module lets you find a module on a directory-path of your
> choice, then imp.load_module lets you load the module you've found.
> 
> > b) I need to overwrite a function with a new one of the same name, but
> > 'import'ed from a module. Is this possible, I really just need to
> > destroy this function before I import the new one, can this be done?
> 
> del thefunctionname
> 
> will unbind the name from the function object ("destroy" only if there
> are no other references -- you cannot destroy an object to which there
> are outstanding references, Python saves you from dangling reference
> problems; if you have subtle needs e.g. for caching, see the weak
> references module).  Most often, though, just:
> 
> thefunctionname = themodulename.thefunctionname
> 
> (after importing the module and binding the module object to name
> themodulename -- whether by an import statement or by more devious
> means) will suffice.
> 
> 
> Alex

Thanks a lot, that was exactly what I was after, works great now!

Thanks again

Moray



More information about the Python-list mailing list