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:
Hi,

I am using Ironpython with C#

I wrote below code to access test.py 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.


C#
ScriptEngine pyEngine = Python.CreateEngine();
                int _ans = pyEngine.Execute(@"test.py");
Posted

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.

Regards,
 
Share this answer
 
Comments
ashu2188 23-Jul-13 2:07am    
Hi,

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


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

object _ans = pyEngine.ExecuteFile("test.py",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.


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

Python code (Test.py)
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("Test.py");
sScope = sEngine.Runtime.CreateScope();
sEngine.SetVariable(sScope, "x", 100);
sEngine.SetVariable(sScope, "y", 200);
sSrc.Execute(sScope);

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