Skip to content

Cannot load library libmonosgen-2.0.dylib on Apple M1 #2000

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

Closed
thomasnal opened this issue Nov 3, 2022 · 3 comments
Closed

Cannot load library libmonosgen-2.0.dylib on Apple M1 #2000

thomasnal opened this issue Nov 3, 2022 · 3 comments

Comments

@thomasnal
Copy link

Environment

  • Pythonnet version: 3.0.1
  • Python version: 3.9.13
  • Operating System: Darwin Kernel Version 21.6.0
  • .NET Runtime: 6.0.402
  • Mono: 6.12.0.182

Details

  • Describe what you were trying to get done.

I want Python to call a method in C# library that was built on MacOS M1 with VisualStudioPro. The first step is to import clr and that fails.

import clr

Fails with the following error:

E   OSError: cannot load library '/Library/Frameworks/Mono.framework/Versions/Current/lib/libmonosgen-2.0.dylib': dlopen(/Library/Frameworks/Mono.framework/Versions/Current/lib/libmonosgen-2.0.dylib, 0x000A): tried: '/Library/Frameworks/Mono.framework/Versions/Current/lib/libmonosgen-2.0.dylib' (fat file, but missing compatible architecture (have (i386,x86_64), need (arm64e))), '/Library/Frameworks/Mono.framework/Versions/6.12.0/lib/libmonosgen-2.0.1.dylib' (fat file, but missing compatible architecture (have (i386,x86_64), need (arm64e))).  Additionally, ctypes.util.find_library() did not manage to locate a library called '/Library/Frameworks/Mono.framework/Versions/Current/lib/libmonosgen-2.0.dylib'
  • If there was a crash, please include the traceback here.
../../../../.pyenv/versions/3.9.13/envs/my/lib/python3.9/site-packages/pythonnet/__init__.py:75: in _create_runtime_from_spec
    return clr_loader.get_mono(**params)
../../../../.pyenv/versions/3.9.13/envs/my/lib/python3.9/site-packages/clr_loader/__init__.py:58: in get_mono
    impl = Mono(
../../../../.pyenv/versions/3.9.13/envs/my/lib/python3.9/site-packages/clr_loader/mono.py:30: in __init__
    self._version = initialize(
../../../../.pyenv/versions/3.9.13/envs/my/lib/python3.9/site-packages/clr_loader/mono.py:127: in initialize
    _MONO = load_mono(libmono)
../../../../.pyenv/versions/3.9.13/envs/my/lib/python3.9/site-packages/clr_loader/ffi/__init__.py:46: in load_mono
    return ffi.dlopen(path_str, ffi.RTLD_GLOBAL)
../../../../.pyenv/versions/3.9.13/envs/my/lib/python3.9/site-packages/cffi/api.py:150: in dlopen
    lib, function_cache = _make_ffi_library(self, name, flags)
../../../../.pyenv/versions/3.9.13/envs/my/lib/python3.9/site-packages/cffi/api.py:832: in _make_ffi_library
    backendlib = _load_backend_lib(backend, libname, flags)
../../../../.pyenv/versions/3.9.13/envs/my/lib/python3.9/site-packages/cffi/api.py:827: in _load_backend_lib
    raise OSError(msg)
E   OSError: cannot load library '/Library/Frameworks/Mono.framework/Versions/Current/lib/libmonosgen-2.0.dylib': dlopen(/Library/Frameworks/Mono.framework/Versions/Current/lib/libmonosgen-2.0.dylib, 0x000A): tried: '/Library/Frameworks/Mono.framework/Versions/Current/lib/libmonosgen-2.0.dylib' (fat file, but missing compatible architecture (have (i386,x86_64), need (arm64e))), '/Library/Frameworks/Mono.framework/Versions/6.12.0/lib/libmonosgen-2.0.1.dylib' (fat file, but missing compatible architecture (have (i386,x86_64), need (arm64e))).  Additionally, ctypes.util.find_library() did not manage to locate a library called '/Library/Frameworks/Mono.framework/Versions/Current/lib/libmonosgen-2.0.dylib'```
@thomasnal
Copy link
Author

thomasnal commented Nov 3, 2022

I've learned that I can select a runtime that pythonnet will use. Since I compile .NET Core library I do not need mono. Asking pythonnet to use coreclr runtime however fails. Which hostfxr version is it looking for?

from pythonnet import load
load("coreclr", runtime_config="./app.runtime.json")
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
File ~/.pyenv/versions/3.9.13/envs/my/lib/python3.9/site-packages/pythonnet/__init__.py:77, in _create_runtime_from_spec(spec, params)
     76 elif spec == "coreclr":
---> 77     return clr_loader.get_coreclr(**params)
     78 else:

File ~/.pyenv/versions/3.9.13/envs/my/lib/python3.9/site-packages/clr_loader/__init__.py:122, in get_coreclr(runtime_config, dotnet_root, properties, runtime_spec)
    120         runtime_spec.write_config(f)
--> 122 impl = DotnetCoreRuntime(runtime_config=runtime_config, dotnet_root=dotnet_root)
    123 if properties:

File ~/.pyenv/versions/3.9.13/envs/my/lib/python3.9/site-packages/clr_loader/hostfxr.py:22, in DotnetCoreRuntime.__init__(self, runtime_config, dotnet_root, **params)
     21 self._dotnet_root = Path(dotnet_root)
---> 22 self._dll = load_hostfxr(self._dotnet_root)
     23 self._is_initialized = False

File ~/.pyenv/versions/3.9.13/envs/my/lib/python3.9/site-packages/clr_loader/ffi/__init__.py:37, in load_hostfxr(dotnet_root)
     35     pass
---> 37 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/x64

The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)
Cell In [6], line 1
----> 1 load('coreclr', runtime_config='app.runtime.json')

File ~/.pyenv/versions/3.9.13/envs/my/lib/python3.9/site-packages/pythonnet/__init__.py:133, in load(runtime, **params)
    131         set_runtime_from_env()
    132     else:
--> 133         set_runtime(runtime, **params)
    135 if _RUNTIME is None:
    136     raise RuntimeError("No valid runtime selected")

File ~/.pyenv/versions/3.9.13/envs/my/lib/python3.9/site-packages/pythonnet/__init__.py:29, in set_runtime(runtime, **params)
     26     raise RuntimeError(f"The runtime {_RUNTIME} has already been loaded")
     28 if isinstance(runtime, str):
---> 29     runtime = _create_runtime_from_spec(runtime, params)
     31 _RUNTIME = runtime

File ~/.pyenv/versions/3.9.13/envs/my/lib/python3.9/site-packages/pythonnet/__init__.py:90, in _create_runtime_from_spec(spec, params)
     82     raise RuntimeError(
     83         f"""Failed to create a default .NET runtime, which would
     84             have been "{spec}" on this system. Either install a
   (...)
     87             (see set_runtime_from_env)."""
     88     ) from exc
     89 else:
---> 90     raise RuntimeError(
     91         f"""Failed to create a .NET runtime ({spec}) using the
     92         parameters {params}."""
     93     ) from exc

RuntimeError: Failed to create a .NET runtime (coreclr) using the
                parameters {'runtime_config': 'app.runtime.json'}.
$ 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

@filmor
Copy link
Member

filmor commented Nov 3, 2022

The error is right there:

'.../libmonosgen-2.0.1.dylib' (fat file, but missing compatible architecture (have (i386,x86_64), need (arm64e))).

You will have the same problem with .NET Core.

The Python arch has to match the .NET/Mono arch.

@filmor filmor closed this as not planned Won't fix, can't repro, duplicate, stale Nov 3, 2022
@filmor
Copy link
Member

filmor commented Nov 3, 2022

See here: pythonnet/clr-loader#40

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

No branches or pull requests

2 participants