在 MacOS 上使用 Python ctypes 加载“libmpich.dylib”时出错

问题描述 投票:0回答:1

我正在尝试使用

mpich
在 python 中加载
ctypes
库。但出现以下错误。
OSError: dlopen(/opt/homebrew/lib/libmpich.dylib, 0x0006): symbol not found in flat namespace '_ADIOI_Datarep_head'

Python 3.12.3(主要,2024 年 4 月 9 日,08:09:14)[Clang 15.0.0 (clang-1500.3.9.4)] 达尔文

>>> from ctypes import cdll
>>> cdll.LoadLibrary("/opt/homebrew/lib/libmpich.dylib")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/homebrew/Cellar/[email protected]/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ctypes/__init__.py", line 460, in LoadLibrary
    return self._dlltype(name)
           ^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ctypes/__init__.py", line 379, in __init__
    self._handle = _dlopen(self._name, mode)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: dlopen(/opt/homebrew/lib/libmpich.dylib, 0x0006): symbol not found in flat namespace '_ADIOI_Datarep_head'

otools 显示 dylib 依赖于“/usr/lib/libSystem.B.dylib”。 libSystem.B.dylib 文件不是那个目录,我在新的 MacOS 中读到,这个动态库已被滚动到动态链接器共享缓存中。知道如何解决这个问题吗?

user@machine: otool -L libmpich.dylib  
libmpich.dylib:
    /opt/homebrew/opt/mpich/lib/libmpi.12.dylib (compatibility version 17.0.0, current version 17.0.0)
    /opt/homebrew/Cellar/mpich/4.2.0/lib/libpmpi.12.dylib (compatibility version 17.0.0, current version 17.0.0)
    /opt/homebrew/opt/hwloc/lib/libhwloc.15.dylib (compatibility version 23.0.0, current version 23.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1336.61.1)

user@machine: ll /usr/lib/libSystem.B.dylib
ls: /usr/lib/libSystem.B.dylib: No such file or directory
python ctypes dylib mpich
1个回答
0
投票

看起来可以使用

ctypes.CDLL
加载 C 库而不会出现错误,并将
ctypes.RTLD_GLOBAL
作为附加参数。

>>> import ctypes
>>> a = ctypes.CDLL("/opt/homebrew/lib/libmpich.dylib", ctypes.RTLD_GLOBAL)
>>> a
<CDLL '/opt/homebrew/lib/libmpich.dylib', handle 9b12d7c0 at 0x100a65a00>
© www.soinside.com 2019 - 2024. All rights reserved.