Python and .NET: Why use both?

By denis.akhiyarov@gmail.com

Elevator Pitch

Python for .NET (pythonnet, Python.NET) allows two-way inter-operability between Python and CLR runtimes on Windows, Linux, OSX, and even Linux subsystem on Windows! This presentation is going to show how to use Python code from .NET and .NET assemblies from Python.

Description

Python for .NET (pythonnet, Python.NET) allows two-way inter-operability between Python and CLR runtimes on Windows, Linux, OSX, and even Linux subsystem on Windows! This presentation is going to show how to use Python code from .NET and .NET assemblies from Python. Particular importance in Python.NET is given to installation options: Python wheels, conda and nuget binaries, docker images, and even distribution with WinPython. The deployment is also simplified with tools such as PyInstaller and cx_freeze. This presentation will show how to install and deploy Python.NET apps using these tools. In this talk we are going to show how to call numpy, matplotlib, sympy, and tensorflow from .NET without much boilerplate code. The second part will show how to use .NET from Python, particularly C# magic cells (clrmagic) in ipython kernel with Jupyter Notebooks. The Python.NET tutorial was converted to Jupyter Notebook and C# code cells that are embedded within the same notebook. This presentation will show few demos with REPL experience both from C# and Python using IPython, scriptcs, and Visual Studio. All libraries used in this presentation are open-source and available on all major platforms. Python for .NET is a library developed since 2003, which “graduated” from Zope, moved to SourceForge and eventually to GitHub, where it became widely used and adopted. This talk will also demonstrate clrmagic - Jupyter extension, built on top of pythonnet and developed by authors of this presentation. Python.NET is built with a number of open-source technologies. It uses pycparser and clang for parsing internal CPython structures. .NET types are exposed to Python with Unmanaged Exports (DllExport) open-source “compiler” on Windows. On other platforms, C-API of Mono and CPython are used to “bootstrap” both runtimes. Python.NET is used by financial algorithmic trading platforms, engineering companies, and few open-source projects: QuantConnect Lean, pywebview, Pybee Beeware Toga cross-platform UI toolkit, and DWSIM process simulator. Python.NET enables large .NET applications to embed scripting Python interface and numerical Python libraries without boilerplate code and without sacrificing for performance.

Notes

PyPy and .NET Core (CoreCLR) support in pythonnet are in active development. The project uses extensively Visual Studio and PTVS for development and debugging. Python.NET has 3 active core developers but also more than 40 contributors. The PyPI downloads are about 100000 since 2016, but there are many other distribution channels such as anaconda.org, nuget.org, WinPython, docker, github, chocolatey, and even sourceforge. The project has a mailing list, stackoverflow tag, and even slack and gitter channels for developers and users respectively.

The author of this talk is experienced user and core developer of pythonnet since 2014 for numerical and engineering applications in real-time modeling. This talk was given on PyData conference in Seattle and practiced as part of Python Houston and .NET Houston meetups in May 2017 and June 2017 at Decisio Health and Microsoft Tech Center respectively.