top of page

Summary of how to execute a function written in python in C #


Overview


Sometimes you want to incorporate a function written in python into a windows application made using C#. Specifically, machine learning and image recognition. There are several methods that can be used in such cases, so I've summarized them.



1. Execute .py file by Process.start


You can execute an external file using the start method of the Process class. (reference)

Dim psi As New System.Diagnostics.ProcessStartInfo()

psi.FileName = "python.exe"
psi.Arguments = "pyfile.py"

System.Diagnostics.Process.Start(psi)

Specify the path of python.exe in FileName of ProcessStartInfo and the path of the py file you want to execute in Arguments.

If you need arguments, you can pass them to Arguments separated by spaces.

If you need a return value, you can receive it on standard output.


Merit


・No module installation required

There is no need to install and configure modules like ironpython or pythonnet.


・Maintainability

If you want to change your python code, you only have to replace the .py file, so it is easy to maintain.


Demerit


・Requires python environment at distribution destination

It is assumed that python is installed on your PC. Be careful when passing it to a third party.


・Slow

It will be slow because it calls the external file and receives the result again.


2. Make it into an exe with PyInstaller and execute it with Process.start


You can use a module called pyInstaller to exe a .py file and run it with Process.start.


Merit


・No python environment required for distribution

Once converted to an exe, it can be executed even if python is not installed on the distribution destination PC.


Demerit


・Usability

This module was a bit quirky and I couldn't run it on my PC with an error (maybe I'm just poor at such things ...).

Also, the output exe can be hundreds of megabytes.


・Slow

This is also as slow as 1.


3. IronPython


IronPython is a python environment for running python on .net. (reference)


Merit


・No python environment required for distribution

This can also make exe file.


・Easy to work with

Since you can embed a python program in C #, you can easily pass arguments and results.


・Faster (than the two above)

Since it does not call external files, the execution speed is faster than the above two.


Demerit


・Requires a separate python environment

An Ironpython environment is required separately from the existing python environment.


・Stable version is python2 series

Be careful if you want to execute a program that has already been written in python3 series.


・Maintainability

If you include python code, you will have to rebuild the entire app every time you change the python code. This is not the case when calling .py files, but it will slow down execution.


4. pythonnet


This is also a library that allows you to use python on .net. (reference) I haven't used it, so I wrote the following with reference to other articles.


Merit


・Easy to work with

Since you can embed a python program in C #, you can easily pass arguments and results.


・Faster (than 1 or 2)

Since it does not call external files, the execution speed is faster than 1 and 2.


Demerit


・Environment construction is difficult (reference


・Requires python environment at distribution destination

Passing through a python path should require a python environment (maybe)


・Maintainability

If you include python code, you will have to rebuild the entire app every time you change the python code. This is not the case when calling .py files, but it will slow down execution.



5. Wrap with cython and build as dll


If you write python code in cython, you can use the code from C / C ++. If you make the cython code into a dll with VC ++ etc., you can also call it from C #.


Merit


・Fast

Since it is loaded as a dll, I think the execution speed is faster than others (although I have not measured it).


Demerit


・Setting is difficult

It's especially hard if you're not used to building in C ++.


・Requires python environment at distribution destination

This also requires a python environment.


・Maintainability

If you want to change the python code, you have to modify both the python code and the dll, which is not maintainable.



6. Make a dll in C ++


It overturns the premise of "calling python code", but libraries such as machine learning and image recognition also have APIs for C ++ (pytorch, tensorflow, opencv have).


Merit


・Fast

I think that the execution speed is fast because it is loaded as a dll.


・No python environment required for distribution

Obviously, the problem of "a python environment is required at the distribution destination" that many of the above methods have does not occur.


Demerit


・Requires API for C ++

This method cannot be used in scikit-learn because it does not have an API for C ++.


・Need to rewrite code

If you have already written the code in python, it will be troublesome twice.



Summary


The above can be summarized as follows.

(※Execution speed was not measured and compared)

(※Difficulty in building an environment depends on individual feelings)



By the way, if you copy the virtual environment of anaconda together with the folder and distribute it together, you can execute it even if python is not included in the distribution destination (I do not recommend it very much). (reference)



Which one is better after all?


If the library have an API for C ++ and you have experience writing C ++, you should make a dll in C ++. If not, if it is a desktop application distributed as an exe, execute the exe with Ironpython or Process that does not require a python environment at the distribution destination, and if it is a web application, Ironpython or pythonnnet is good.

After that, please consider things such as whether it is difficult to build the environment, how often you change the python code, and how fast the execution speed is required.




Recent Posts

See All

[C#] Inconsistent accessibility

Phenomenon In C #, the following code causes a title compilation error. This error occurs when, for example, the class is private but the...

Comments


category

Let's do our best with our partner:​ ChatReminder

iphone6.5p2.png

It is an application that achieves goals in a chat format with partners.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Let's do our best with our partner:​ ChatReminder

納品:iPhone6.5①.png

It is an application that achieves goals in a chat format with partners.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Theme diary: Decide the theme and record for each genre

It is a diary application that allows you to post and record with themes and sub-themes for each genre.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png
bottom of page