15

I have Visual Studio 2015 with my main form written in C# and from there I have different classes written in Python (normal Python not Iron Python). How do I call the Python functions from my C# Code?

I know there are multiple topics about this but most of them are too old and some solutions are too difficult or involve to use a middle language like C++.

Here are some links I found useful but didn’t provided with the answer I was exactly searching for:

Is there an easy way or do I still need a workaround? And if I need a workaround then what is the easiest one?

Community
  • 1
  • 1
Morganis
  • 652
  • 5
  • 13
  • 29

2 Answers2

7

The recommended way to do this is to use IronPython. There is another answer which goes over the basics of using IronPython to access Python code from C#: How do I call a specific Method from a Python Script in C#?

If IronPython isn't a choice, as you mentioned, you can alternatively run the function from the command line and pass in the arguments as needed, though for anything complex this could turn into spaghetti. Below is a basic example of running a python function in a module directly from the command line:

python -c 'import foo; print foo.hello()'

So to run the above in C#:

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "python.exe";
startInfo.Arguments = "-c import foo; print foo.hello()";
process.StartInfo = startInfo;
process.Start();

Note that if you are trying to get a return value back, in this case you will have to print the data to the console and then serialize the output from C# as you see fit. This can be potentially problematic if the function outputs anything to the console on it's own, unless you filter it out or there is an option for suppressing the output from that function.

Community
  • 1
  • 1
codewario
  • 19,553
  • 20
  • 90
  • 159
7

You can call everything through your command line

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "python whatever";
process.StartInfo = startInfo;
process.Start();

Or even better, just call Python.exe and pass your py files as its argument:

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "Python.exe";
startInfo.Arguments = "yourfile.py";
process.StartInfo = startInfo;
process.Start();
Yar
  • 7,020
  • 11
  • 49
  • 69
  • 2
    I think OP is looking for a solution to call a specific function rather than just executing the script – codewario Feb 17 '16 at 17:00
  • 2
    @AlexanderMiles the process would be still the same. You can call `python -c 'import foo; print foo.hello()'` command through `System.Diagnostics.Process` – Yar Feb 17 '16 at 17:02
  • 2
    There's a well-written 2015 article that uses @Yar's second approach at https://code.msdn.microsoft.com/windowsdesktop/C-and-Python-interprocess-171378ee . It also includes sending your Python script arguments and capturing its output in your C# app. – snark Jul 04 '17 at 12:41
  • Or you can call the IronPython interpreter from within your C# app: https://medium.com/@dpursanov/running-python-script-from-c-and-working-with-the-results-843e68d230e5 – snark Jul 04 '17 at 12:53
  • @Yar It's not the same. What about return values, especially structured return values like lists, tuples or dictionaries? Just because you can print it doesn't mean you can easily rebuild it in C#. – Joshua Chia May 23 '20 at 14:14