Click here to Skip to main content
15,907,395 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I am using Ironpython with C#

I wrote below code to access script file which will process the input and return the output. But I don't know how can I pass the parameter in it?

Anyone familiar with C#, Ironpython, kindly respond.

ScriptEngine pyEngine = Python.CreateEngine();
                int _ans = pyEngine.Execute(@"");

1 solution

Hello Ashu,

Please have a look at this[^] tutorial.

Basically you need to use createScope method to create the scope within which the script is going to run and then set the variables (scope.SetVariable(Key, Value)) you want to pass to the script in it. The script can refer to these variables using the key specified in setVariable call.

Share this answer
ashu2188 23-Jul-13 2:07am    

I tried going through that article. I wrote this code.

pyEngine = Python.CreateEngine();
pyScope = pyEngine.CreateScope();
pyScope.SetVariable("Argument", "Ashutosh");

object _ans = pyEngine.ExecuteFile("",pyScope);

I am still not getting the desired output.

I feel I am wrong somewhere in python code. I am still searching for any sample example. Python is new for me.

Prasad Khandekar 23-Jul-13 3:58am    
Perhaps following example will help you understand this.

Python code (
def sum(a, b):
return a+b

print sum(x,y);

C# Code
ScriptScope sScope = null;
ScriptEngine sEngine = null;
sEngine = IronPython.Hosting.Python.CreateRuntime().GetEngine("python");
ScriptSource sSrc = sEngine.CreateScriptSourceFromFile("");
sScope = sEngine.Runtime.CreateScope();
sEngine.SetVariable(sScope, "x", 100);
sEngine.SetVariable(sScope, "y", 200);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900