0

I am trying to utilise a .NET6 library that depends on System.Text.Json which is failing to import using pythonnet:

Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
ModuleNotFoundError: No module named 'System'

In python, inspecting the assemblies pythonnet has added by default, it can be seen that it does not ship with the System.Text.Json namespace loaded:

import clr
print("[", ", ".join(clr.ListAssemblies(False)), "]")

Outputs:

[ mscorlib, clrmodule, Python.Runtime, System.Core, System.Configuration, System.Xml, System, __CodeGenerator_Assembly, e__NativeCall_Assembly ]

I then try to add System.Text.Json, which appears to succeed:

import clr
import sys

DOTNET_PATH: str = {YOUR PATH TO .NET6 DLLs}
sys.path.append(DOTNET_PATH)

clr.AddReference("System.Text.Json")
print("[", ", ".join(clr.ListAssemblies(False)), "]")

Outputs:

[ ..., System, System.Text.Json, System.Runtime, ... ]

However, trying to import a class from the namespace:

from System.Text.Json import JsonDocument

Continues to raise:

Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
ModuleNotFoundError: No module named 'System'

(I have also tried adding every .dll that ships with .NET 6.0.1 with no success)

Is there some other mechanism I need to utilise to successfully import from this namespace? (And associated)

Zander Fick
  • 106
  • 1
  • 9
  • Does this answer your question? [Python - No module named 'System'](https://stackoverflow.com/questions/62394857/python-no-module-named-system) – Jesse Jan 27 '22 at 17:24
  • 'System' itself is not the problem. `from System import String` works fine – Zander Fick Jan 27 '22 at 17:50
  • 1
    The error you posted says otherwise. Does importing other namespaces under `System` work? `String` is a type so I'm not sure that's an accurate test. I also don't really know Python so excuse my ignorance if this assumption is completely wrong. – Jesse Jan 27 '22 at 17:53
  • No worries. Python.Net hooks into the python import machinery to load CLR types but it's hidden away from the python runtime and the way in which it raises module not found errors is not exactly as python reports it. I was hoping for an answer here before needing to dredge through the pythonnet source for answers. – Zander Fick Jan 27 '22 at 17:57

1 Answers1

1

To use .NET Core/.NET 5+ assemblies you need pythonnet 3.0.0 or later (currently in preview).

You also need to explicitly load coreclr:

from clr_loader import get_coreclr
from pythonnet import set_runtime

coreclr = get_coreclr("/full/path/to/app.runtimeconfig.json")
set_runtime(coreclr)

// here goes the rest of your code

runtimeconfig.json file is created by dotnet publish

LOST
  • 2,956
  • 3
  • 25
  • 40