[Translation] without having to install Python, you can call the Python library in .NET

Original Address: https://henon.wordpress.com/2019/06/05/using-python-libraries-in-net-without-a-python-installation/

pythonnet emergence of this Cock burst project, so we can use a new way for C # and can interoperate between Python. But it may be a bit of setup and deployment problems, is it really?

 

In this paper I will introduce Python.Included this project, it is not only elegant solution to this problem, and let .NET developers can easily make pleasant Python and .NET interoperability. As a proof of concept, I will use Numpy.Net on display, it is a .NET standard library, which is a Python Numpy provides a strongly typed API, and uses it does not need to install Python on Windows.

 

Developers benefit from Numpy.NET strongly typed API, dynamic API, which supports the Visual Studio IntelliSense feature, you can display the original document Numpy.

 

What's the question?

Everyone may have a different version of Python installed, some people use Python 2.7, others with Python 3.5,3.6 and even 3.7. When you use the pythonnet for each small version of Python, it must be compiled using a different configuration, and the version of Python must be installed so that the code can run. So if you work in a team, each person must be configured exactly the same Python environment. But take our SciSharp team, the situation is not like this. If you want to deploy your .NET application, you must first deploy Python, from a developer point of view, it is very suck.

However, if you are engaged in machine learning and artificial intelligence, although Microsoft and SciSharp have paid a lot of effort, but you still can not completely avoid the use of Python. If you look at are the list of items using pythonnet , you will find many areas of AI companies are currently in use to connect .NET and Python.

 

Python.Included come to the rescue

If you can simply reference a Nuget package, and without the need for manual modification, everything will be fine automatic configuration, if you can achieve this level, how would you feel like? This is what I created Python.Included vision, Python.Included packages python-3.7.3-embed-amd64.zip can be included in its program episode, so it allows you can Nuget be valid reference Python up. In order to prove that it can work, and can quickly provide all NumSharp still missing Numpy function, I created based on Python.Included of Numpy.NET this project.

 

Proof of Concept: Numpy.NET

Numpy.NET provides strongly typed wrapper function Numpy, which means that you absolutely do not need to use dynamic keyword, but this part I will discuss in depth in another article. Today's focus is on how to introduce Numpy.NET use Python.Included to automatically deploy Python and Numpy demand for calls to them.

This is the setup code Numpy will actually executed behind the scenes. These do not require you to operate. Once you've used one of its functions:

Numpy.dll It will set up an embedded Python distribution, and it is compressed from your native home directory of the assembly in solution out of (if not installed before).

The next step (if you have not completed prior to the operation) will decompress numpy pip wheel, while numpy pip wheel is packaged as an embedded resource to Numpy.dll and install it in the Python installation file.

installer.InstallWheel(typeof(NumPy).Assembly, "numpy-1.16.3-cp37-cp37m-win_amd64.whl").Wait();

 

Finally, pythonnet runtime is initialized, it is introduced Numpy also come, for subsequent use.

 

These are happening behind the scenes, using Numpy.dll users do not have to worry about local Python installation. In fact, Python, even if you have installed any version of it does not matter.

 

Performance Considerations

We all know that pythonnet relatively slow, so you may be asking yourself, use pythonnet the Python libraries and .NET together it really is a good idea. As always, it depends on the circumstances.

My test results show that compared with Numpy call directly from Python, using overhead .net call numpy about it four times. The need to clarify, this does not mean Numpy.NET four times slower than in python numpy, it just means that there will be additional overhead Numpy call by pythonnet. Of course, due to the Numpy.NET call is Numpy, Numpy the execution time of the function itself is exactly the same.

 

If cost is an issue entirely depends on the actual use cases. If you are in a nested loop constantly switch back and forth between CLR and Python, it may experience problems. But most Python libraries are designed is to improve efficiency, avoid data cycle. Numpy allows you to use only one call can operate on millions of array elements. Pytorch and Tensorflow allows you to fully perform operations on the GPU. Thus, if used correctly, as compared with the execution time of the operation processing large amounts of data, interoperability overhead is negligible.

 

route map

I know there are a lot Numpy transplanted to the programs and projects on .NET, for example, using IronPython. But the IronPython project is still only supports Python 2.7, and the project is progressing very slowly. This leads to depend on python libraries 3 of IronPython can not be obtained and used, and this situation would not be any change in the near future.

My focus is to provide more artificial intelligence and machine learning libraries for .NET by pythonnet. SciSharp team also discussing how to develop a faster version of pythonnet, thus avoiding the use of slow nature DynamicObject.

Please try Numpy.NET, and let me know what it has done for you and how to do. If you have any comments or suggestions, I would appreciate it, and I hope my work can help .NET machine learning communities grow and prosper.

Guess you like

Origin www.cnblogs.com/cgzl/p/11004708.html