doted filenames in import statements

Chris Rebert clp2 at rebertia.com
Tue Jul 21 23:59:17 CEST 2009


On Tue, Jul 21, 2009 at 2:42 PM, Jean-Michel
Pichavant<jeanmichel at sequans.com> wrote:
> Hi fellows,
>
> I'd like to use the dynamic __import__ statement. It works pretty well with
> non dotted names, but I cannot figure how to make it work with dotted file
> paths.
>
> example:
>
> file = "/home/dsp/test.py"
> test = __import__(file)
>
> works like a charm
>
> file = "/home/dsp/4.6.0.0/test.py"
> test = __import__(file)
> => no module name blalalal found.
>
> Any suggestion ? I tried multiple escape technics without any success.

You want the imp.load_module() function:
http://docs.python.org/library/imp.html#imp.load_module

__import__() only operates on module/package names. I'm not sure how
you even got it to work with a filename...

Cheers,
Chris
-- 
http://blog.rebertia.com



More information about the Python-list mailing list