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.

Himanshu
  • 4,327
  • 16
  • 31
  • 39
wethecom
  • 9
  • 5
  • 1
    Can you please clarify your question. You want to run IronPython from inside C# and pass it a parameter? – Mathemats Apr 12 '15 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 Apr 13 '15 at 00:45
  • possible duplicate of [Script arguments and Embedded IronPython](http://stackoverflow.com/questions/1601364/script-arguments-and-embedded-ironpython) – Simon Opelt Apr 13 '15 at 05: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 Apr 13 '15 at 06: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 Apr 13 '15 at 06:43
  • Then what are you trying to say with "within C#"? I assumed that you are talking about IronPython.Hosting, instantiating and Engine/Scope and executing the script file. The Q/A I linked talks about embedded IronPython in terms of embedded/hosted engine. That does not necessarily imply embedded script source. – Simon Opelt Apr 13 '15 at 06:51
  • using the c sharp language using iron python – wethecom Apr 13 '15 at 18:09
  • I still stand by this being a duplicate. You would just have to change the answer to `options["Arguments"] = new [] { "generator.py", "/somefile.dat" };`. – Simon Opelt Apr 14 '15 at 20:21
  • that dose not come close answer my question thats a vague snippet of code. the code you referenced is far from proper and every line of it throws errors – wethecom Apr 16 '15 at 00:06
  • its clear you do not know the answer or you would have done so by answering it – wethecom Apr 16 '15 at 00:09
  • I am not usually one to let someone bully me into answering but I am curious if my wild assumptions and speculations of what you are trying to do were correct. – Simon Opelt Apr 19 '15 at 21:34

1 Answers1

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.

Simon Opelt
  • 6,136
  • 2
  • 35
  • 66