Mac 上的 Python3 加载 libssh2 时遇到问题
更新到 Monterey 12.2.1 并将 Xcode 更新到 13.2.1 后,当我使用安装了 Python 3.10.2 的系统时,我在 venv 环境中遇到了问题。我的应用程序使用 ssh2-python,它需要链接到 libssh2。我相信这说明了一切:(跳过烦人的水平滚动,带走文本是'/usr/lib/libssh2.1.dylib'(没有这样的文件)
File "/Users/mnelson1/3m-install/venv-3.10.2/lib/python3.10/site-packages/pssh/clients/native/single.py", line 25, in <module>
from ssh2.error_codes import LIBSSH2_ERROR_EAGAIN
ImportError: dlopen(/Users/mnelson1/3m-install/venv-3.10.2/lib/python3.10/site-packages/ssh2/error_codes.cpython-310-darwin.so, 0x0002): Library not loaded: /private/var/folders/6s/q3hjdt5s00b5p_cpjbbxp6680000gr/T/pip-install-lasr65mk/ssh2-python_46cfb4509bee4529bbe0e4eacbb00674/src/src/libssh2.1.dylib
Referenced from: /Users/mnelson1/3m-install/venv-3.10.2/lib/python3.10/site-packages/ssh2/error_codes.cpython-310-darwin.so
Reason: tried: '/private/var/folders/6s/q3hjdt5s00b5p_cpjbbxp6680000gr/T/pip-install-lasr65mk/ssh2-python_46cfb4509bee4529bbe0e4eacbb00674/src/src/libssh2.1.dylib' (no such file), '/usr/lib/libssh2.1.dylib' (no such file)
当然,libssh2是实际安装:(
❯ brew info libssh2
libssh2: stable 1.10.0 (bottled), HEAD
C library implementing the SSH2 protocol
https://www.libssh2.org/
/usr/local/Cellar/libssh2/1.10.0 (184 files, 999.8KB) *
Poured from bottle on 2022-03-03 at 15:00:10
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/libssh2.rb
License: BSD-3-Clause
==> Dependencies
Required: [email protected] ✔
==> Options
--HEAD
Install HEAD version
==> Analytics
install: 101,845 (30 days), 301,986 (90 days), 1,138,668 (365 days)
install-on-request: 1,683 (30 days), 6,573 (90 days), 137,389 (365 days)
build-error: 2 (30 days)
❯ ls -l /usr/local/lib/libssh*
lrwxr-xr-x 1 mnelson1 staff 44 Mar 3 15:01 /usr/local/lib/libssh2.1.dylib@ -> ../Cellar/libssh2/1.10.0/lib/libssh2.1.dylib
lrwxr-xr-x 1 mnelson1 staff 38 Mar 3 15:01 /usr/local/lib/libssh2.a@ -> ../Cellar/libssh2/1.10.0/lib/libssh2.a
lrwxr-xr-x 1 mnelson1 staff 42 Mar 3 15:01 /usr/local/lib/libssh2.dylib@ -> ../Cellar/libssh2/1.10.0/lib/libssh2.dylib
虽然我担心那个构建错误 - 我在这里很弱,不知道这是否是一个真正的问题。)
无论如何,我可以通过设置 DYLD_LIBRARY_PATH (当前)来解决这个问题undefined)到 /usr/local/lib 。
我对 Mac 上的安装管理没有经验,所以我想知道如果我使用 3.10.2,为什么该库的brew 安装不起作用? (或任何其他版本)来自 pyeval,我没有问题。为什么 Python 的系统安装显然与 pyeval 构建有不同的动态库路径
?地点,所以我不认为这是我所做的其他事情:
❯ pip3 list installed
Package Version
---------- -------
pip 22.0.3
setuptools 58.1.0
wheel 0.37.1
我认为旧的缓存 pip 安装可能需要重建,所以我在完全删除 pip3 缓存后运行我的安装进行了测试,但无济于事。
好消息是,我的大部分目标安装都是在 Linux 系统上,而且他们似乎对 libssh2 没有这种善变,所以问题仅限于我的开发环境。但我想更好地了解我做错了什么。有一些应用程序用户可能容易受到此影响,我希望能够对任何必要的解决方法给出连贯的解释。
搜索有关此问题的信息我意识到这不是一个罕见的问题,像 this 分散在各处,但我找到的解决方案对我来说并没有多大启发。
After getting updated to Monterey 12.2.1 and having Xcode updated to 13.2.1, I'm having trouble in a venv environment when I use the system installed Python, 3.10.2. My app is using ssh2-python, which needs to link to libssh2. I believe this spells it all out: (to skipg over the annoying horizontal scroll, the takeway text is '/usr/lib/libssh2.1.dylib' (no such file)
File "/Users/mnelson1/3m-install/venv-3.10.2/lib/python3.10/site-packages/pssh/clients/native/single.py", line 25, in <module>
from ssh2.error_codes import LIBSSH2_ERROR_EAGAIN
ImportError: dlopen(/Users/mnelson1/3m-install/venv-3.10.2/lib/python3.10/site-packages/ssh2/error_codes.cpython-310-darwin.so, 0x0002): Library not loaded: /private/var/folders/6s/q3hjdt5s00b5p_cpjbbxp6680000gr/T/pip-install-lasr65mk/ssh2-python_46cfb4509bee4529bbe0e4eacbb00674/src/src/libssh2.1.dylib
Referenced from: /Users/mnelson1/3m-install/venv-3.10.2/lib/python3.10/site-packages/ssh2/error_codes.cpython-310-darwin.so
Reason: tried: '/private/var/folders/6s/q3hjdt5s00b5p_cpjbbxp6680000gr/T/pip-install-lasr65mk/ssh2-python_46cfb4509bee4529bbe0e4eacbb00674/src/src/libssh2.1.dylib' (no such file), '/usr/lib/libssh2.1.dylib' (no such file)
Of course, libssh2 is actually installed:
❯ brew info libssh2
libssh2: stable 1.10.0 (bottled), HEAD
C library implementing the SSH2 protocol
https://www.libssh2.org/
/usr/local/Cellar/libssh2/1.10.0 (184 files, 999.8KB) *
Poured from bottle on 2022-03-03 at 15:00:10
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/libssh2.rb
License: BSD-3-Clause
==> Dependencies
Required: [email protected] ✔
==> Options
--HEAD
Install HEAD version
==> Analytics
install: 101,845 (30 days), 301,986 (90 days), 1,138,668 (365 days)
install-on-request: 1,683 (30 days), 6,573 (90 days), 137,389 (365 days)
build-error: 2 (30 days)
❯ ls -l /usr/local/lib/libssh*
lrwxr-xr-x 1 mnelson1 staff 44 Mar 3 15:01 /usr/local/lib/libssh2.1.dylib@ -> ../Cellar/libssh2/1.10.0/lib/libssh2.1.dylib
lrwxr-xr-x 1 mnelson1 staff 38 Mar 3 15:01 /usr/local/lib/libssh2.a@ -> ../Cellar/libssh2/1.10.0/lib/libssh2.a
lrwxr-xr-x 1 mnelson1 staff 42 Mar 3 15:01 /usr/local/lib/libssh2.dylib@ -> ../Cellar/libssh2/1.10.0/lib/libssh2.dylib
(Although I'm concerned about that build-error - I'm weak here and don't know if this is a real problem or not.)
Anyway, I can work around this by setting DYLD_LIBRARY_PATH (currently undefined) to /usr/local/lib.
I'm not experienced at installation management on the Mac, so I'm wondering why a brew install of the library doesn't just work? Even more confusing, if I use 3.10.2 (or any other version) from pyeval, I don't have the problem. Why does the system install of Python apparently have a different dynamic library path from the pyeval builds?
I don't have any python modules installed in the global or local locations, so I don't think it's anything else I've done:
❯ pip3 list installed
Package Version
---------- -------
pip 22.0.3
setuptools 58.1.0
wheel 0.37.1
I thought that it's possible that old cached pip installs might need to be rebuilt, so I've tested by running my install after completely deleting the pip3 cache, to no avail.
The good news is that most of my target installs are on Linux systems, and they don't seem to have this fickleness about libssh2, so the problem is limited to my development environment. But I'd like to have a better idea of what I'm doing wrong. And there are a few app users who are likely vulnerable to this, and I'd like to be able to give a coherent explanation of any necessary workarounds.
Searching for info on this problem I realize it's not an uncommon problem, discussions like this are scattered around, but the resolutions I've found don't enlighten things much for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
libssh2 已安装,但 Apple 已放弃在较新的 MacOS 版本中使用它,转而使用自己的库,因此找不到它。在您的错误中,该包正在
/usr/lib/libssh2.1.dylib
中查找 libssh2,其中不包含 dylib。您需要找到它们的安装位置(在 Monterey 12.6.2 上,brew 3.6.19 的默认安装路径为/opt/homebrew/Cellar/
)以下命令对我有用:
对于我的系统homebrew 在以下路径中安装了 libssh2 和 .dylib 文件...
您还可以将此导出语句放在 .bashrc 或 .zshrc 中,这样您就不必在每次重新启动 shell 时都定义它。
我发现其他论坛上针对此问题的许多解决方案都已过时/不起作用,这可能在未来版本中不起作用。
libssh2 is installed, but Apple has depreciated its use in newer MacOS versions in favor of its own libraries, hence why it cannot be found. In your error, the package is looking for libssh2 in
/usr/lib/libssh2.1.dylib
, which does not contain the dylibs. You will need to find where they were installed (with brew 3.6.19 the default install path is/opt/homebrew/Cellar/
on Monterey 12.6.2)The following command worked for me:
For my system homebrew installed libssh2 and the .dylib files in the following path...
You can also place this export statement in your .bashrc or .zshrc so that you won't have to define it each time you restart the shell.
I found many of the solutions on other forums for this issue are outdated/do no work, it is possible this may not work in a future version.