py2exe and dynamic modules

Peter Hansen peter at engcorp.com
Mon Jun 9 15:01:25 EDT 2003


"Uwe C. Schroeder" wrote:
> 
> I checked everything and it seems I'm the only one trying to do this.
> I have a large application that dynamically loads plugins.
> There is no way I can say at runtime what names the plugins have, so loading
> them statically is not an option.
> The plugins are quite simple and contain one class only.
> With a full python installation it's no problem to load them using
> __import__. However in a py2exe deployment the importer is replaced, so
> __import__ won't work anymore.
> Also the directory where the plugins are stored is user dependant (on
> windows in the profile tree) and is determined at runtime. The plugins
> don't even exist at application start, they are transfered on request.
> 
> What I can say is that for one user the directory is always the same.
> Is there a way to deploy this using py2exe (or whatever, although I like
> py2exe for it's simplicity) ?
> 
> I can modify the program to store the plugins in a fixed place - but I don't
> like this too much since then deploying it on *nix is a hassle for access
> right reasons.
> 
> Is there any way to tell py2exe's importer to look in a certain directory ?
> as said: currently I just add the directory to sys.path and do an
> __import__.

Have you actually tried doing this?  I just made a file that imports another
file after dynamically adding a directory to sys.path.  Then I ran py2exe
on the first script to generate an executable.  It rightly complains that
it cannot find the second script "The following modules were not found:"
but since that's just a warning, it creates the .exe file.

Then I run the .exe file and it quietly finds the necessary .py script
in the specified directory and imports it.

It seems py2exe does a graceful fallback to the usual sort of import
processing with sys.path, so I don't think you should have too much
trouble with this.

-Peter




More information about the Python-list mailing list