2

I want to pass a parameter from a C# code to Python using ProcessStartInfo. I am obliged to avoid IronPython (by creating a scope) because of its incompatibility with numpy. I found some answers which are unsuccessful. Here is what I wrote:

var x = 8;
        string arg = string.Format(@"C:\Users\ayed\Desktop\IronPythonExamples\RunExternalScript\plot.py", x);
        try
        {
            Process p = new Process();
            p.StartInfo = new ProcessStartInfo(@"D:\WinPython\WinPython-64bit-2.7.5.3\python-2.7.5.amd64\python.exe", arg);
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.Start();
            p.WaitForExit();
        }
        catch (Exception ex)
         {
             Console.WriteLine("There is a problem in your Python code: " + ex.Message);
         }

         Console.WriteLine("Press enter to exit...");
         Console.ReadLine();

The test python code allows to plot a curve and print the value of x. Ok for the curve, but I am getting this exception: name 'x' is not defined.

How can I properly pass a variable to this python code?

1
  • You're going to have to modify the Python code to accept the value as a command line parameter or via its standard input. Without seeing the python code being invoked, we can't tell you what to change. Commented Nov 13, 2015 at 14:32

1 Answer 1

2

It looks like x should be an argument for plot.py, but you forgot to add x to arg.

string.Format(@"C:\Users\ayed\Desktop\IronPythonExamples\RunExternalScript\plot.py {0}", x);
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Gabriel, I have just tried this and I keep have the same problem.
No, I am just expecting it to be passed from C# as it is. What do you mean by reading the value of x from the command line?
You expect x to be passed to plot.py 'as it is', how is that exactly?
I thought that x value can be passed in a similar way to that of IronPython without having to read it from the command line. How can I read it from the command line anyway?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.