0

IronPython will run directly from C# without Process.Start

The script file I want to load requires one parameter (filename), how do you write that out in C#?

Note: This will hopefully be a a conversion of a Python script to a Windows GUI application where you use the GUI to choose the file.

11
  • 1
    Can you please clarify your question. You want to run IronPython from inside C# and pass it a parameter?
    – Mathemats
    Commented Apr 12, 2015 at 22:36
  • If speed isn't a major issue, why not write the params to a file with c#, close the file, then let your python script read the paras from the same file.
    – calmond
    Commented Apr 13, 2015 at 0:45
  • possible duplicate of Script arguments and Embedded IronPython Commented Apr 13, 2015 at 5:58
  • i have a project setup with iron python now i need to run a scriptfile an give it a argument "generator.py /somefile.dat"
    – wethecom
    Commented Apr 13, 2015 at 6:09
  • this is not a duplicate of that what ever they wrote out. it dose not even come close to being able to compile...my question is broader in scope and im not asking to embed anything. i want to run a py script with command line parameter the py script is proprietary and im not allowed to change if i redistribute it
    – wethecom
    Commented Apr 13, 2015 at 6:43

1 Answer 1

1

Assuming a script generator.py that could look like

import sys
fileName = sys.argv[0]
print "working on", fileName

an IronPython engine can be hosted in C#/.NET by

var options = new Dictionary<string, object>();
options["Arguments"] = new[] { @"/somefile.dat" };

var engine = Python.CreateEngine(options);
var runtime = engine.Runtime;
var scope = engine.CreateScope();

var script = engine.CreateScriptSourceFromFile(@"generator.py");
script.Execute(scope);

which results in the output working on /somefile.dat.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.