库 (dylib) 未加载 - 未找到图像 - Python IDE

Library (dylib) not loaded - image not found - Python IDE

基本上我正在尝试 运行 来自 savReaderWriter 模块的一些 Python 代码,以便创建一个可以在 IBM SPSS 中打开的 .sav 文件。作为 macOS 用户,我需要先在终端中 运行 这两行,模块才能工作:

echo 'export DYLD_LIBRARY_PATH=/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/savReaderWriter/spssio/macos'  >> ~/.bash_profile
echo 'export LC_ALL=en_US.UTF-8'  >> ~/.bash_profile

下面你可以看到一段代码,我试图在 Python 中 运行:

import savReaderWriter

savFileName = "someFile.sav"
records = [['Test1', 1, 1], ['Test2', 2, 1]]
varNames = ['var1', 'v2', 'v3']
varTypes = {'var1': 5, 'v2': 0, 'v3': 0}
with savReaderWriter.SavWriter(savFileName, varNames, varTypes, ioUtf8=True) as writer:
    for record in records:
        writer.writerow(record)

我的问题是,虽然 运行 将 Python 中的代码通过 terminal.app 工作得很好,并且出现了一个新的 .sav 文件,但试图在IDE(试过 PyCharm 和 Spyder)给我一个错误:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2847, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-94007b092d47>", line 7, in <module>
    with savReaderWriter.SavWriter(savFileName, varNames, varTypes, ioUtf8=True) as writer:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/savReaderWriter/savWriter.py", line 198, in __init__
    super(Header, self).__init__(savFileName, ioUtf8, ioLocale)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/savReaderWriter/generic.py", line 29, in __init__
    self.spssio = self.loadLibrary()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/savReaderWriter/generic.py", line 117, in loadLibrary
    spssio = self._loadLibs("macos")
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/savReaderWriter/generic.py", line 89, in _loadLibs
    return [load(os.path.join(path, lib)) for lib in libs][-1]
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/savReaderWriter/generic.py", line 89, in <listcomp>
    return [load(os.path.join(path, lib)) for lib in libs][-1]
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ctypes/__init__.py", line 348, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: dlopen(/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/savReaderWriter/spssio/macos/libicuuc48.1.dylib, 6): Library not loaded: @executable_path/../lib/libicudata48.1.dylib
  Referenced from: /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/savReaderWriter/spssio/macos/libicuuc48.1.dylib
  Reason: image not found

模块作者无法在这件事上帮助我,因此我很高兴收到这个社区的任何建议。

编辑(添加 sys.path):

来自终端:

 ['',
 '/Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip',
 '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
 '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload',
 '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages',
 '/Users/mg/mne-python']

来自IDE:

['/Applications/PyCharm.app/Contents/helpers/pydev',
 '/Users/mg/Documents/Python/Projects/MD',
 '/Applications/PyCharm.app/Contents/helpers/pydev',
 '/Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip',
 '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
 '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload',
 '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages',
 '/Users/mg/mne-python',
 '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/IPython/extensions',
 '/Users/mg/Documents/Python/Projects/MD']

此致,

毫克

我不是Mac人,但你真的在用Python 3.6吗? savReaderWriter 是否支持该版本?我怀疑它使用的 I/o 模块是为该版本构建的。

如果这通过终端而不是 IDE 有效,请检查 Python 搜索路径是否相同。

找到解决方案!

基本上我需要为错误中出现的每个 dylib 创建符号链接,示例如下:

sudo ln -s /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/savReaderWriter/spssio/macos/libicudata48.1.dylib /usr/local/lib/libicudata48.1.dylib
sudo ln -s /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/savReaderWriter/spssio/macos/libicui18n48.1.dylib /usr/local/lib/libicui18n48.1.dylib
sudo ln -s /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/savReaderWriter/spssio/macos/libspssdio.dylib /usr/local/lib/libspssdio.dylib
sudo ln -s /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/savReaderWriter/spssio/macos/libzlib123spss.dylib /usr/local/lib/libzlib123spss.dylib

所以从理论上讲:

sudo ln -s /path/to/original /path/to/symbolic/link

此致,

毫克

我将添加这个问题的答案只是为了让它更具有普遍性。本来希望发表评论,但没有代表!虽然 Maciej 是完全正确的,并且他的回答帮助我找到了我的问题,但 savReaderWriter 已经更新了。从版本 3.4.2 开始,现在有 6 个 .dlyb 文件要复制。

获得文件夹路径后(路径是错误消息的位置),确保在创建您的文件夹之前列出(ls 在终端 window)该文件夹中的所有文件符号链接。然后为每个 .dylib

创建符号链接

再次感谢 Maciej 的精彩回答!

对于其他懒惰的人:

ls /Users/your_user/anaconda/envs/quattro8/lib/python2.7/site-packages/savReaderWriter/spssio/macos/ | xargs -I {} sudo ln -s /Users/your_user/anaconda/envs/quattro8/lib/python2.7/site-packages/savReaderWriter/spssio/macos/{} /usr/local/lib/{}

这会链接该存储库中的所有包

您还可以在 PyCharm 中指定库的路径,这样就无需创建符号链接:

从菜单栏中单击 "Run" -> "Edit Configurations"。在左窗格 select 中,您的 python 脚本的配置(通常 PyCharm 为每个 .py 文件创建一个配置)。将库的路径(显示在错误消息中)插入 "Environment Variables".

示例: DYLD_LIBRARY_PATH=/Users/username/miniconda3/envs/savreader/lib/python3.6/site-packages/savReaderWriter/spssio/macos;

Screenshot of PyCharm Run Configuration

升级到 Mac OS Catalina 后,将所有六个 dylib 文件复制到 /usr/local/lib/ 不再有效(至少对我而言)。为了找出正确的目的地是什么,我做了以下操作:

  1. 打开错误堆栈树中提到的ctypes/__init__.py
  2. 添加print(os.getcwd())获取工作目录。对我来说,工作目录是 /Users/Username/GitHub/myproject
  3. 由于 dylib 文件中的路径是相对于此路径 (@executable_path/../lib/),因此 dylib 文件的正确目标是 /Users/Username/lib