Home > database >  Is there a good way to use C# (.NET Framework) and Python code together?
Is there a good way to use C# (.NET Framework) and Python code together?

Time:01-20

We have a solution written in C#/.NET Framework 4.7. It has a lot of infrastructure code related to environment configurations, database access, logging, exception handling etc. Our co-workers are eager to contribute to the project with Python code that makes a lot of special calculations. Ideally we want to pass configuration plus (big amount of) input data to their code and get back (big amount of) results without resorting to database integration. Is there a viable way to do so? Main goals are: 1) not to rewrite Python code to C# 2) not to duplicate configuration/database related code in Python to make future maintenance easier

CodePudding user response:

Yes this is exactly what Unix (e.g. Gnu/Linux) dose. The Unix philosophy is about creating many (usually small) programs (that usually do one thing well), and connecting them to create a system that is greater than the parts. To do this we use inter-process communication, usually pipelines / streams.

An alternate approach is to compile the C# into a library, that can be called form the python.

CodePudding user response:

Ideally we want to pass configuration plus (big amount of) input data to their code and get back (big amount of) results [...]

So if all you do is want to pass parameters to a python script and get back the result, there is a pretty easy way to do so. I suggest you take a look at IronPython. Not only can you do that, but you can also easily import things from .NET/C# in your python scripts if there is ever a need for that.

  • Related