[IronPython] How to force ipy-engine to reload imported modules ?
Michael Foord
fuzzyman at voidspace.org.uk
Mon Feb 5 17:53:22 CET 2007
Langer, Jochen ALRT/EEG2 * wrote:
>
> We use IronPython-engine embedded in a C# application. The engine
> executes Python-scripts that are located in several directories with
> ExecuteFile(). The scripts import modules that are located in
> subfolders of folder of the respective script.
>
> Doing so the following problem occurs:
>
> 1. Executing ScriptA in DirectoryA imports Modules that are located in
> subdrectories of DirectoryA - still everything is fine
>
> 2. Executing ScriptB in DirectoryB does not import modules if a module
> with the same name had been imported during the previous call to
> ExecuteFile("DirectoryA\ScriptA"). The previously imported modules
> (belonging to ScriptA) are used instead.
>
> As a workaround we create a new engine prior to each execution and
> dispose it afterwards.
> But this is not the preferred implementation - I'd rather like to have
> one static engine-object that can be used multiple times. (putting all
> scripts into the same folder or using different module-names is not an
> option due to maintainance and reuse)
>
> Any suggestions how the ipy-engine can be forced to reload modules
> upon each call to ExecuteFile() ?
>
Reusing previously imported modules (recognised by name) is a feature
not a bug. :-)
I'm not sure what the best approach from 'outside' the engine is, but
there are a couple of Python tricks to get this to work as you expect.
Imported modules are stored in 'sys.modules' (a dictionary - keyed by
the module name). Deleting entires from here will cause them to be
reloaded next time you import.
If you make your top level directory a package you can import using
absolute references. E.g. if you have your main application libraries in
a directory called main and then two subdirectories called 'a' and 'b'
and you want to import modules from both a and b. Add empty
'__init__.py' files to directories a and b and then import using :
from Main.a import module
from Main.b import module
The entries in sys.modules will then be 'Main.a.module' and
'Main.b.module'; which don't clash.
I HTH.
I'm sure there are alternative approaches from the C# sharp.
All the best,
Michael Foord
http://www.voidspace.org.uk/ironpython/index.shtml
> regards,
>
>
> Jochen
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> users mailing list
> users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
> ------------------------------------------------------------------------
>
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.1.411 / Virus Database: 268.17.25/669 - Release Date: 04/02/2007
>
>
More information about the Ironpython-users
mailing list