py2exe and dynamic modules

Uwe C. Schroeder uwe at
Mon Jun 9 21:19:26 CEST 2003

Peter Hansen wrote:

> "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

Nope, doesn't work for me.
The problem is, that I don't use a normal "import blah" statement, since I
don't know the name yet. AFAIK py2exe actually creates a dict of modules t
load, so if it doesn't find it at compile time, it's still in the dict -
making it possible to do a later import. If the module wasn't specified at
compile time it won't be found.


Open Source Solutions 4U, LLC   2570 Fleetwood Drive
Phone:  +1 650 872 2425         San Bruno, CA 94066
Cell:   +1 650 302 2405         United States
Fax:    +1 650 872 2417

More information about the Python-list mailing list