Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Python and .NET

Python .NET

IronPython

Python running on the DLR that is on top of the CLR of Microsoft.

  • [IronPython](https://ironpython.net/

  • GitHub

  • Only supports Python 2

  • Iron Python 3

  • Not ready for production

Use .NET libraries from Python

pip install pythonnet

The latest Visual Studio is supposed to include Nuget, but if you don't have it, you can download it from Nuget downloads

Make sure nuget.exe is somewhere in your PATH:

For example I've created C:\Bin, put the nuget.exe in this directory and added C:\Bin to the PATH.

Then install the compilers using nuget install Microsoft.Net.Compilers as suggested on Roslyn This created the Microsoft.Net.Compilers.3.4.0 directory in my home directory

Make sure csc.exe is somewhere in your PATH or use the full path to it:

"\Users\Gabor Szabo\Microsoft.Net.Compilers.3.4.0\tools\csc.exe" /t:library MyMath.cs

Python and .NET console

import clr
from System import Console

Console.WriteLine("Hello My World!")
python net_console.py

Python and .NET examples

namespace MyMath
{
    public static class MyMathClass
    {
        public static int addInts(int a, int b)
        {
            return a+b;
        }

        public static double addDouble(double a, double b)
        {
            return a+b;
        }

        public static string addString(string a, string b)
        {
            return a+" "+b;
        }

        public static bool andBool(bool a, bool b)
        {
            return a && b;
        }

        public static string str_by_index(string[] a, int b)
        {
            return a[b];
        }
        public static int int_by_index(int[] a, int b)
        {
            return a[b];
        }

    }
}
import clr
dll = clr.FindAssembly('MyMath')  # returns path to dll
assembly = clr.AddReference('MyMath')
#print(type(assembly)) # <class 'System.Reflection.RuntimeAssembly'>
#print(dir(assembly))
from MyMath import MyMathClass
from MyMath import MyMathClass as My


assert My.addInts(2, 3)         == 5
assert My.addInts(2.7, 7.8)     == 9
assert My.addDouble(11.2, 23.3) == 34.5
assert My.addString("hello", "world") == "hello world"

assert My.andBool(1, 1) is True
assert My.andBool(1, 0) is False
assert My.andBool(True, True) is True
assert My.andBool(False, True) is False

assert My.str_by_index(["apple", "banana", "peach"], 0) == "apple"
assert My.str_by_index(["apple", "banana", "peach"], 1) == "banana"
assert My.int_by_index([17, 19, 42], 1) == 19
# Mixed list cannot be passed

# tuple can be passed
assert My.int_by_index((17, 21, 42), 2) == 42

# TODO: string, char, float
# TODO strings, lists, dicts,
# TODO complex data structures in C#
# TODO Async
csc /t:library MyMath.cs
python myapp.py

C:\Windows\Microsoft.NET\Framework\v4.0.30319
C:\Program Files\dotnet\

Exercise Python and .NET

  • Take a .NET class that you would like to use, try that.