pythonnet 使用 System.Text.Json
pythonnet use System.Text.Json
我正在尝试使用依赖于 System.Text.Json
的 .NET6 库,该库无法使用 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'
在python中,默认添加了检查程序集pythonnet
,可以看出它没有加载System.Text.Json
命名空间:
import clr
print("[", ", ".join(clr.ListAssemblies(False)), "]")
输出:
[ mscorlib, clrmodule, Python.Runtime, System.Core, System.Configuration, System.Xml, System, __CodeGenerator_Assembly, e__NativeCall_Assembly ]
然后我尝试添加 System.Text.Json
,似乎成功了:
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)), "]")
输出:
[ ..., System, System.Text.Json, System.Runtime, ... ]
但是,尝试从命名空间导入 class:
from System.Text.Json import JsonDocument
继续加注:
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'
(我也尝试添加 .NET 6.0.1 附带的每个 .dll 但没有成功)
我是否需要利用其他机制才能从该命名空间成功导入? (和关联)
要使用 .NET Core/.NET 5+ 程序集,您需要 pythonnet 3.0.0 或更高版本(当前为预览版)。
您还需要显式加载 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
文件由 dotnet publish
创建
我正在尝试使用依赖于 System.Text.Json
的 .NET6 库,该库无法使用 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'
在python中,默认添加了检查程序集pythonnet
,可以看出它没有加载System.Text.Json
命名空间:
import clr
print("[", ", ".join(clr.ListAssemblies(False)), "]")
输出:
[ mscorlib, clrmodule, Python.Runtime, System.Core, System.Configuration, System.Xml, System, __CodeGenerator_Assembly, e__NativeCall_Assembly ]
然后我尝试添加 System.Text.Json
,似乎成功了:
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)), "]")
输出:
[ ..., System, System.Text.Json, System.Runtime, ... ]
但是,尝试从命名空间导入 class:
from System.Text.Json import JsonDocument
继续加注:
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'
(我也尝试添加 .NET 6.0.1 附带的每个 .dll 但没有成功)
我是否需要利用其他机制才能从该命名空间成功导入? (和关联)
要使用 .NET Core/.NET 5+ 程序集,您需要 pythonnet 3.0.0 或更高版本(当前为预览版)。
您还需要显式加载 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
文件由 dotnet publish