Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not find a suitable hostfxr library #40

Closed
gabloe opened this issue Oct 14, 2022 · 13 comments
Closed

Could not find a suitable hostfxr library #40

gabloe opened this issue Oct 14, 2022 · 13 comments

Comments

@gabloe
Copy link

gabloe commented Oct 14, 2022

Environment

  • Pythonnet version: 3.0.0.post1
  • Python version: 3.8
  • Operating System: macOS 12.6
  • .NET Runtime: .NETCore 6
  • Architecture: arm64 (Apple M1)

Details

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

runtime_config = os.path.join("/Users/gabloe/dotnetconfig", "pythonnettest.runtimeconfig.json")
rt = get_coreclr(runtime_config=runtime_config)
set_runtime(rt)

import clr

Traceback

Traceback (most recent call last):
...
    rt = get_coreclr(runtime_config=runtime_config)
  File "/Users/gabloe/Library/Caches/pypoetry/virtualenvs/test-lQu4lGUY-py3.8/lib/python3.8/site-packages/clr_loader/__init__.py", line 122, in get_coreclr
    impl = DotnetCoreRuntime(runtime_config=runtime_config, dotnet_root=dotnet_root)
  File "/Users/gabloe/Library/Caches/pypoetry/virtualenvs/test-lQu4lGUY-py3.8/lib/python3.8/site-packages/clr_loader/hostfxr.py", line 20, in __init__
    self._dll = load_hostfxr(self._dotnet_root)
  File "/Users/gabloe/Library/Caches/pypoetry/virtualenvs/test-lQu4lGUY-py3.8/lib/python3.8/site-packages/clr_loader/ffi/__init__.py", line 31, in load_hostfxr
    raise RuntimeError(f"Could not find a suitable hostfxr library in {dotnet_root}")
RuntimeError: Could not find a suitable hostfxr library in /usr/local/share/dotnet
Exception ignored in: <function Runtime.__del__ at 0x7f8609306820>
Traceback (most recent call last):
  File "/Users/gabloe/Library/Caches/pypoetry/virtualenvs/test-lQu4lGUY-py3.8/lib/python3.8/site-packages/clr_loader/types.py", line 137, in __del__
    self.shutdown()
  File "/Users/gabloe/Library/Caches/pypoetry/virtualenvs/test-lQu4lGUY-py3.8/lib/python3.8/site-packages/clr_loader/hostfxr.py", line 111, in shutdown
    if self._handle is not None:
AttributeError: 'DotnetCoreRuntime' object has no attribute '_handle'
@filmor filmor transferred this issue from pythonnet/pythonnet Oct 15, 2022
@filmor
Copy link
Member

filmor commented Oct 15, 2022

I don't have a mac to check this, but could it be that the issue is along the lines of #36, that the dotnet path is not looked up correctly? What's the output of which dotnet in a terminal?

@gabloe
Copy link
Author

gabloe commented Oct 17, 2022

I don't have a mac to check this, but could it be that the issue is along the lines of #36, that the dotnet path is not looked up correctly? What's the output of which dotnet in a terminal?

gabloe ~> which dotnet
/usr/local/share/dotnet/dotnet

Seems to be correct, at least it is the same path referenced in the exception

@gabloe
Copy link
Author

gabloe commented Oct 17, 2022

Also if it helps, here is the .NET information from my system:

 gabloe /usr/local/share/dotnet> dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.402
 Commit:    6862418796

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  12.6
 OS Platform: Darwin
 RID:         osx.12-arm64
 Base Path:   /usr/local/share/dotnet/sdk/6.0.402/

global.json file:
  Not found

Host:
  Version:      6.0.10
  Architecture: arm64
  Commit:       5a400c212a

.NET SDKs installed:
  6.0.302 [/usr/local/share/dotnet/sdk]
  6.0.401 [/usr/local/share/dotnet/sdk]
  6.0.402 [/usr/local/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.7 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.9 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.10 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.7 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.9 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.10 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

Download .NET:
  https://aka.ms/dotnet-download

Learn about .NET Runtimes and SDKs:
  https://aka.ms/dotnet/runtimes-sdk-info

@gabloe
Copy link
Author

gabloe commented Oct 17, 2022

Also, a screenshot of my /usr/local/share/dotnet directory struture, showing libhostfxr:
image

@gabloe
Copy link
Author

gabloe commented Oct 17, 2022

After stepping through this with debugger, it seems to be a machine architecture mismatch.

"cannot load library '/usr/local/share/dotnet/host/fxr/6.0.9/libhostfxr.dylib': dlopen(/usr/local/share/dotnet/host/fxr/6.0.9/libhostfxr.dylib, 0x0002): tried: '/usr/local/share/dotnet/host/fxr/6.0.9/libhostfxr.dylib' (mach-o file, but is an incompatible architecture (have (arm64), need (x86_64)))"

But I am unsure why it is expecting x86_64 here.

@filmor
Copy link
Member

filmor commented Oct 17, 2022

Because your Python is x86-64. The architectures have to match.

@gabloe
Copy link
Author

gabloe commented Oct 17, 2022

Ahh, that is right. After re-installing python from the universal package it is now working properly. Thanks @filmor.

@thomasnal
Copy link

thomasnal commented Nov 3, 2022

It makes sense and gives me the insight. Thanks.

In my case, I have python architecture arm64, .net architecture arm64, but the host/fxr folder has only these versions:

$ ls -l /usr/local/share/dotnet/x64/host/fxr/
total 0
drwxr-xr-x  3 root  wheel  96 16 Jun 00:09 3.1.27
drwxr-xr-x  3 root  wheel  96 26 Sep 21:30 3.1.30

I need to find out how to make dotnet install hostfxr 6.10.

EDITED: because of the /usr/local/share/dotnet/x64. The arm64e is in /usr/local/share/dotnet/host. Why pythonnet looks for x86 when in the case of mono it looks for arm64e? My Python is arm64 version.

@thomasnal
Copy link

thomasnal commented Nov 3, 2022

I copied dotnet/host/fxr/6.0.10 to dotnet/x64/host/fxr leading to this error,

You must install or update .NET to run this application.

App: /Users/thomasnal/.pyenv/versions/3.9.13/bin/python3.9
Architecture: arm64
Framework: 'Microsoft.NETCore.App', version '6.0.10' (arm64)
.NET location: /usr/local/share/dotnet/x64

The following frameworks were found:
  3.1.27 at [/usr/local/share/dotnet/x64/shared/Microsoft.NETCore.App]
  3.1.30 at [/usr/local/share/dotnet/x64/shared/Microsoft.NETCore.App]

How can I tell to pythonnet that the correct dotnet path /usr/local/share/dotnet (without x64)?

@filmor can you advise?

@thomasnal
Copy link

When I override the dotnet path that pythonnet uses then runtime loads up,

from pythonnet import set_runtime
from clr_loader import get_coreclr

rt = get_coreclr(dotnet_root=Path('/usr/local/share/dotnet'))
set_runtime(rt)

import clr

@filmor
Copy link
Member

filmor commented Nov 3, 2022

The lookup code is contained in https://github.com/pythonnet/clr-loader/blob/master/clr_loader/util/find.py, PRs are welcome. I don't have a Mac (and don't plan on getting one anytime soon), so I can't test this behaviour. It looks to me like this previous PR is the culprit: #37

It adjusted the lookup logic to find the dotnet root for 64bit macOS, but it didn't take the architecture into account.

@LeeDongGeon1996
Copy link
Contributor

@filmor Sorry, I also think my previous PR is wrong..
To check system architecture correctly, we need the codes below:

if "ARM" in platform.machine().upper():

If you don't mind, can I send fixed PR?

@zarkin404
Copy link

When I override the dotnet path that pythonnet uses then runtime loads up,

from pythonnet import set_runtime
from clr_loader import get_coreclr

rt = get_coreclr(dotnet_root=Path('/usr/local/share/dotnet'))
set_runtime(rt)

import clr

dude, you save my time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants