Hosted IronPython: import files which contain custom functions
Asked Answered
C

1

2

Situation

Hosted IronPython allows developers to set parameters into script. Every time when a IPy engine object is created, I set such a parameter (ParamName), but when I try to import python module, in which my custom parameter is used, I get an exception with message "global name 'ParamName' is not defined".

Code sample

class PythonScriptingEngine
{
    private ScriptEngine pyEngine;
    private ScriptScope pyScope;

    public PythonScriptingEngine()
    {
        pyEngine = Python.CreateEngine();
        pyScope = pyEngine.CreateScope();
    }

    public object Run(string script)
    {
        ScriptSource source = pyEngine.CreateScriptSourceFromString(script);
        CompiledCode compiled = source.Compile();
        return compiled.Execute(pyScope);
    }

    public void SetParameter(string name, int value)
    {
        pyScope.SetVariable(name, value);
    }
}

// execution
var engine = new PythonScriptingEngine();
engine.SetParameter("ParamName", 10);
engine.Run(@"import SampleScriptWithParamName");

Question

Is there any workaround to this situation? How can I import python script in which custom parameter is used?

Canister answered 14/12, 2012 at 7:54 Comment(5)
Could you provide some source snippets in order to clarifying what you are trying to accomplish? Are you setting a variable on the scope, launching some script text via the scope and importing another module from that script text?Uncourtly
I'm setting a variable on the scope, then I'm trying to import *.py file, which uses this variable.Canister
please have a look at Q: https://mcmap.net/q/301551/-global-variable-from-a-different-file-python and A: https://mcmap.net/q/301551/-global-variable-from-a-different-file-python . I think your question is very similar and more python than ironpython related.Uncourtly
I've looked these links, but in my question described another situation. A: https://mcmap.net/q/301551/-global-variable-from-a-different-file-python can't answer my question.Canister
If you look at the linked question your SampleScriptWithParamName is equal to file2.py and your engine.Run(@"import SampleScriptWithParamName") is equal to file1.py. Instead of directly defining the variable as foo = "bar", you set it via the hosting environment. The linked answer explains how the scope defined in file1 wont "spill over" to file2 when file2 is imported. That is why your SampleScriptWithParamName won't see the variable.Uncourtly
D
3

As Simon pointed out, the issue is that ParamName is not in scope for SampleScriptWithParamName. One way to achieve that is to add it to the set of builtin variables like so:

public void SetParameter(string name, int value)
{
    pyEngine.GetBuiltinModule().SetVariable(name, value);
}

This should make it available everywhere, but I don't have the ability to test it right now.

Denomination answered 14/12, 2012 at 16:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.