Skip to content
\n

and I simply use this code to load the .NET runtime:

\n
    import pythonnet\n\n    path = os.path.join(\"path\", \"to\", \"app\", \"lib\", \"dotnet\")\n    sys.path.insert(0, path)\n    config_path = os.path.join(path, \"MyApp.runtimeconfig.json\")\n    pythonnet.load(\"coreclr\", runtime_config=str(config_path))\n\n    import clr\n\n    clr.AddReference(\"MyApp\")
\n

This works fine mostly, however, I noticed that if my app overrides a framework dependency e.g. upgrading System.Collections.Immutable from version 8.0.0 (provided by the framework) to 9.0.0 (provided via NuGet, but still compatible with net8.0), my application starts crashing, because the SCI version 9.0.0 cannot be loaded.

\n

After a lot debugging, I realized that this is because the MyApp.deps.json is not considered when first initializing hostfxr therefore it only ever loads the TPA version provided by the framework. See the host.log attachment for details.

\n

Unfortunately, it seems that hostfxr is considering Python.Runtime.dll the \"main assembly\" as can be seen here

\n
CoreCLR path = 'C:\\path\\to\\app\\build\\dependencies\\dotnet\\shared\\Microsoft.NETCore.App\\8.0.4\\coreclr.dll', CoreCLR dir = 'C:\\path\\to\\app\\build\\dependencies\\dotnet\\shared\\Microsoft.NETCore.App\\8.0.4\\'\nLoaded library from C:\\path\\to\\app\\build\\dependencies\\dotnet\\shared\\Microsoft.NETCore.App\\8.0.4\\coreclr.dll\n--- Invoked hostpolicy [version: 8.0.4 @Commit: 2d7eea252964e69be94cb9c847b371b23e4dd470] corehost_resolve_component_dependencies = {\n  Component main assembly path: C:\\path\\to\\app\\lib\\scripts-python3.10\\pythonnet\\runtime\\Python.Runtime.dll\n}\n-- arguments_t: app_root='C:\\path\\to\\app\\lib\\scripts-python3.10\\pythonnet\\runtime\\' deps='C:\\path\\to\\app\\lib\\scripts-python3.10\\pythonnet\\runtime\\Python.Runtime.deps.json' mgd_app='C:\\path\\to\\app\\lib\\scripts-python3.10\\pythonnet\\runtime\\Python.Runtime.dll'\n
\n

I was able to work around this by doing the following:

\n
    import pythonnet\n\n    path = os.path.join(\"path\", \"to\", \"app\", \"lib\", \"dotnet\")\n    sys.path.insert(0, path)\n    config_path = os.path.join(path, \"MyApp.runtimeconfig.json\")\n\n   runtime = clr_loader.get_coreclr(runtime_config=str(config_path))\n    \n    pythonnet.set_runtime(runtime) \n\n    libPath = os.path.join(path, \"MyApp.dll\")\n    assembly = runtime.get_assembly(str(libPath))\n\n    print(\"Loaded \" + libPath)\n\n    # just invoke an empty function to load the library\n    func = assembly.get_function(\"MyApp.Interop.Host.Initialize\")\n\n    result = func(b\"\")\n    assert result == 0, f\"Failed to initialize MyApp.dll, error code {result}\"\n    \n    pythonnet.load(runtime)\n\n    import clr\n\n    clr.AddReference(\"Python.Runtime\")\n    clr.AddReference(\"MyApp\")
\n

now, hostfxr loads the \"correct\" deps.json file, but I get a Python error whenever I do something that involves list or dict types:

\n
someManagedObj.ListProperty = get_python_list()
\n
\n

TypeError: 'list' value cannot be converted to System.Collections.Generic.IReadOnlyList`1[System.String]

\n
\n

What am I missing here? Thank you very much!

","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"

You are not missing anything, this is simply tricky. .NET Framework didn't have this \"problem\", and Python.NET was developed against .NET Framework.

\n

I'm not sure why the conversions are messed up in your case.

\n

Could you see whether this pythonnet/clr-loader#66 helps? It is already merged on master, just not released, yet (as I want to integrate it back into the get_coreclr factory).

","upvoteCount":1,"url":"https://github.com/pythonnet/pythonnet/discussions/2613#discussioncomment-14219614"}}}
Discussion options

You must be logged in to vote

You are not missing anything, this is simply tricky. .NET Framework didn't have this "problem", and Python.NET was developed against .NET Framework.

I'm not sure why the conversions are messed up in your case.

Could you see whether this pythonnet/clr-loader#66 helps? It is already merged on master, just not released, yet (as I want to integrate it back into the get_coreclr factory).

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@siegfriedpammer-qt
Comment options

Answer selected by siegfriedpammer-qt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants