0
votes

I'm noticing a strange issue with IronPython. Here is a very simple script that my application runs:-

import globals
x = globals.x * globals.y

The file "globals.py" resides on the ScriptEngine's search path, and just contains a series of name=value lines (basically "global constants" that may be needed by our scripts), e.g.:-

x=5
y=10
..etc..

Normally, the application's script executes fine every time it runs, however I encounter a problem if I do the following:-

First, add a new line to globals.py (e.g. z=15). Next, edit my application's script to use the new variable:-

import globals
x = globals.x * globals.y + globals.z

When the script is executed next (assuming I haven't closed down my application) then I get the following error message:-

'module' object has no attribute 'z'

If I restart my application then execute the script, it works fine. My app uses a singleton ScriptEngine, and can only assume something is being cached to cause this (e.g. an old version of the globals.py file, somehow?).

I've now fixed the issue by creating a new ScriptEngine instance each time I execute the script, but I'm curious to know what might be causing the problem, and whether there is anything that I can "clear down", rather than have to instantiate a new ScriptEngine every time.

FYI the script being executed by my application isn't compiled. It's simply run using:-

_engine.CreateScriptSourceFromString(
                myScript,
                SourceCodeKind.Statements);
2

2 Answers

0
votes

Never mind, just found this article: IronPython hosting caching imported module?

stating that IronPython does indeed cache imported modules.

0
votes

You can shutdown the engine after execution.

_engine.Runtime.Shutdown();