The recommended way to do this is to use IronPython . There is another answer that talks about using IronPython to access Python code from C #: How do I call a specific method from Python Script in C #?
If IronPython is not a choice, as you mentioned, you can also run the function from the command line and pass the necessary arguments, although for something complicated it can turn into spaghetti. The following 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 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();
Please note that if you are trying to return the 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 wish. This can be potentially problematic if the function outputs something to the console on its own, if you do not filter it, or there is an option to suppress the output from this function.
Bender the greatest
source share