[IronPython] How to force ipy-engine to reload imported modules ?

Dino Viehland dinov at exchange.microsoft.com
Mon Feb 5 20:38:27 CET 2007

I think the package solution that Michael proposes here is the best one.  It's good both for getting the behavior right from C# (you do nothing in C# and let the package infrastructure work for you) and for being a consistent host within the broader Python perspective.  This way you're not re-inventing the wheel and instead just doing things the "right way".

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Michael Foord
Sent: Monday, February 05, 2007 8:53 AM
To: Discussion of IronPython
Subject: Re: [IronPython] How to force ipy-engine to reload imported modules ?

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'm sure there are alternative approaches from the C# sharp.

All the best,

Michael Foord

> 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

users mailing list
users at lists.ironpython.com

More information about the Ironpython-users mailing list