Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Does Python.Net 3.0 support self-contained .NET? #1634

@DareDevilDenis

Description

@DareDevilDenis

Environment

  • Pythonnet version: 3.0.0.dev1 (built from master on 10 Dec 2021)
  • Python version: 3.9 64 bit
  • Operating System: Windows 10
  • .NET Runtime: 5.0.11

Details

Does Python.Net 3.0 support self-contained .NET?
I'm able to successfully call from Python to .NET when publishing my dll as framework dependent however when I publish the same dll as self-contained I get:

I n i t i a l i z a t i o n   f o r   s e l f - c o n t a i n e d   c o m p o n e n t s   i s   n o t   s u p p o r t e d
 Traceback (most recent call last):
  File "C:\Protos\PythonToDotNetCoreSelfCont\my_python.py", line 7, in <module>
    rt = get_coreclr(os.path.join(dot_net_path, 'MyApp.runtimeconfig.json'))
  File "C:\Program Files\Python39\lib\site-packages\clr_loader\__init__.py", line 44, in get_coreclr
    impl = DotnetCoreRuntime(runtime_config=runtime_config, dotnet_root=dotnet_root)
  File "C:\Program Files\Python39\lib\site-packages\clr_loader\hostfxr.py", line 15, in __init__
    self._handle = _get_handle(self._dll, self._dotnet_root, runtime_config)
  File "C:\Program Files\Python39\lib\site-packages\clr_loader\hostfxr.py", line 104, in _get_handle
    check_result(res)
  File "C:\Program Files\Python39\lib\site-packages\clr_loader\util\__init__.py", line 26, in check_result
    raise error
clr_loader.util.clr_error.ClrError: 0x80008093: InvalidConfigFile
Exception ignored in: <function DotnetCoreRuntime.__del__ at 0x000001F6AA8305E0>
Traceback (most recent call last):
  File "C:\Program Files\Python39\lib\site-packages\clr_loader\hostfxr.py", line 88, in __del__
  File "C:\Program Files\Python39\lib\site-packages\clr_loader\hostfxr.py", line 83, in shutdown
AttributeError: 'DotnetCoreRuntime' object has no attribute '_handle'

My Python code:

import os
import sys
from clr_loader import get_coreclr
from pythonnet import set_runtime

dot_net_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), r'MyDll\publish')
rt = get_coreclr(os.path.join(dot_net_path, 'MyApp.runtimeconfig.json'))
set_runtime(rt)
import clr
sys.path.append(dot_net_path)
result = clr.FindAssembly("MyApp")
print('FindAssembly returned:', result)
result = clr.AddReference("MyApp")
print('AddReference returned:', result)

from MyApp import MyClass

my = MyClass()
result = my.PrintToConsole('Hello from Python')
print('Python finished')

My .NET code:

using System;

namespace MyApp
{
    public class MyClass
    {
        public int PrintToConsole(string message)
        {
            Console.WriteLine("C# .NET Core DLL says : " + message);
            return 123;
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions