Call Python from C#
The documentation provides a sample implementation of the inter-process communication between C# and Python scripts.
Note that in the example above data is serialized using MongoDB.Bson library that can be installed via NuGet manager.
Otherwise, you can use any JSON serialization library of your choice.
Below are inter-process communication implementation steps:
Input arguments are serialized into JSON string and saved in a temporary text file:
Python interpreter python.exe runs the python script that reads JSON string from a temporary text file and backs-out input arguments:
Python script is executed and output dictionary is serialized into JSON string and printed to the command window:
Read output JSON string from C# application:
I am using the inter-process communication between C# and Python scripts in one of my projects that allows calling Python scripts directly from Excel spreadsheets.
The project utilizes ExcelDNA add-in for C# - Excel binding.
The source-code is stored in the GitHub repository.
Below are links to wiki pages that provide an overview of the project and help to get started in 4 easy steps.
I hope you find the example and the project useful.
C# code calling Python script
Python script to be called by C# application
This modified text is an extract of the original Stack Overflow Documentation created by following contributors
and released under CC BY-SA 3.0