Run c# function from python(jupyter notebook)

378 Views Asked by At

I have this file chello.cs

using System;

namespace HelloWorldApp {
 
    class Geeks {
  
        static void Main(string[] args) { 
              
            // statement 
            // printing Hello World! 
            Console.WriteLine("Hello World!"); 
              
            // To prevents the screen from  
            // running and closing quickly 
            Console.ReadKey(); 
        } 
    } 
} 

I want to call it from python in jupyter notebook. I am using pythonnet and trying something like this

import clr

clr.AddReference(r'chello')

from HelloWorldApp import Geeks

my = Geeks()

my.Main()

But it won't work, it shows error:

FileNotFoundException: Unable to find assembly 'chello'.
   at Python.Runtime.CLRModule.AddReference(String name)
1

There are 1 best solutions below

0
On

Zeroth, just to clarify, did you actually compile C#?

First, the assembly name is probably chello.exe, considering it has Main in it.

Second, depending on your file system layout, you need to either add the directory to PATH environment variable, or pass full path to AddReference.