7

I am trying to use a .NET Core library inside a Jupyter Notebook python script by using PythonNet. Support for .NET Core was added recently (see https://github.com/pythonnet/pythonnet/issues/984#issuecomment-778786164) but I am still getting a No module named 'TestAppCore' error.

I don't have an issue using a .NET Framework library with PythonNet, only .NET Core. Any help with diagnosing and fixing the issue would be greatly appreciated.

The C# library I'm trying to get working is a simple class library project with no dependencies at all. Below is the entirety of the code:

namespace TestAppCore
{
  public class Foo
  {
    public int ID { get; set; }
    
    public Foo(int id)
    {
      ID = id;
    }
    
    public int Add(int a, int b)
    {
      return a + b;
    }
  }
}

Here is the python script:

from clr_loader import get_coreclr
from pythonnet import set_runtime

rt = get_coreclr("D:\src\Test.runtimeconfig.json")
set_runtime(rt)

import clr
import sys

sys.path.append(r"D:\src\TestAppCore")
clr.AddReference(r"TestAppCore")

from TestAppCore import Foo

foo = Foo(5)
print(foo.ID)

res = foo.Add(1, 2)
print(res)

Here is the output:

enter image description here

Finally, here is the runtime config I am using:

{
  "runtimeOptions": {
    "tfm": "netcoreapp3.1",
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "3.1.0"
    }
  }
}
  • .NET Core: 3.1
  • python version: 3.7
  • pythonnet: 3.0.0.dev1
  • clr-loader: 0.1.6
JChristen
  • 588
  • 4
  • 21

3 Answers3

0

I suspect that you are getting the DLL path wrong. This worked for me:

from clr_loader import get_coreclr
from pythonnet import set_runtime
set_runtime(get_coreclr("pythonnetconfig.json"))
import clr
clr.AddReference("C:/Path/To/Interface.dll")

from Interface import Foo
foo = Foo()

Using

Python 3.8.10
pythonnet 3.0.0a1
clr-loader 0.1.7

C# DLL (Class Library) targeting .NET Core 3.1

pythonnetconfig.json exactly as you posted.

Paamand
  • 626
  • 1
  • 6
  • 17
0

I never got it to work with .NET Core 3.1. For me it worked with .NET Framework 4.8 and pythonnet 2.5.2. See my other answer for more details.

Roald
  • 2,459
  • 16
  • 43
0

I have a similar problem and found out that if the DLL and the namespace are the same, then it fails. In my case, following pythonnet tutorial:

Calculate.sln contains Calc.cs, with

namespace Calculate;  // recommended by VS
class Calc { ... }

Then in Python

clr.AddReference("Calculate") # Assuming sys.path correct setting
from Calculate import Calc

Then: ImportError: cannot import name 'Calc' from 'Calculate' (unknown location)

But with:

namespace CalculateNS;  // different name than Calculate.dll
class Calc { ... }

Then in Python

clr.AddReference("Calculate") # Assuming sys.path correct setting
from CalculateNS import Calc

it works... side effects, pylance known Calculate module but not CalculateNS :-(

Anyone experimented this? I saw lot of answers that have never been tested I guess, only thoughts

Using .NET 6.0 Framework.