为什么 Python 找不到 sys.path 目录中的共享对象?
我正在尝试导入 pycurl
:
$ python -c "import pycurl"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: libcurl.so.4: cannot open shared object file: No such file or directory
现在,libcurl.so.4
位于 /usr/local/lib
中。 正如您所看到的,它位于 sys.path 中:
$ python -c "import sys; print(sys.path)"
['', '/usr/local/lib/python2.5/site-packages/setuptools-0.6c9-py2.5.egg',
'/usr/local/lib/python25.zip', '/usr/local/lib/python2.5',
'/usr/local/lib/python2.5/plat-linux2', '/usr/local/lib/python2.5/lib-tk',
'/usr/local/lib/python2.5/lib-dynload',
'/usr/local/lib/python2.5/sitepackages', '/usr/local/lib',
'/usr/local/lib/python2.5/site-packages']
任何帮助将不胜感激。
I'm trying to import pycurl
:
$ python -c "import pycurl"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: libcurl.so.4: cannot open shared object file: No such file or directory
Now, libcurl.so.4
is in /usr/local/lib
. As you can see, this is in sys.path
:
$ python -c "import sys; print(sys.path)"
['', '/usr/local/lib/python2.5/site-packages/setuptools-0.6c9-py2.5.egg',
'/usr/local/lib/python25.zip', '/usr/local/lib/python2.5',
'/usr/local/lib/python2.5/plat-linux2', '/usr/local/lib/python2.5/lib-tk',
'/usr/local/lib/python2.5/lib-dynload',
'/usr/local/lib/python2.5/sitepackages', '/usr/local/lib',
'/usr/local/lib/python2.5/site-packages']
Any help will be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
sys.path
仅搜索 Python 模块。 对于动态链接库,搜索的路径必须在LD_LIBRARY_PATH
中。 检查您的LD_LIBRARY_PATH
是否包含/usr/local/lib
,如果不包含,请添加它并重试。更多信息(来源):
要设置
LD_LIBRARY_PATH
,请使用以下其中一项,最好在您的~/.bashrc
中或等效文件:
或
如果第一种形式为空(相当于空字符串,或根本不存在),则使用第一种形式,如果不是,则使用第二种形式。 请注意导出的使用。
sys.path
is only searched for Python modules. For dynamic linked libraries, the paths searched must be inLD_LIBRARY_PATH
. Check if yourLD_LIBRARY_PATH
includes/usr/local/lib
, and if it doesn't, add it and try again.Some more information (source):
To set
LD_LIBRARY_PATH
, use one of the following, ideally in your~/.bashrc
or equivalent file:
or
Use the first form if it's empty (equivalent to the empty string, or not present at all), and the second form if it isn't. Note the use of export.
确保您的 libcurl.so 模块位于系统库路径中,该路径与 python 库路径不同且独立。
“快速修复”是将此路径添加到 LD_LIBRARY_PATH 变量中。 然而,在系统范围内(甚至帐户范围内)进行设置是一个坏主意,因为设置它的方式可能会使某些程序找到不应该找到的库,甚至更糟糕的是,会打开安全漏洞。
例如,如果您的“本地安装的库”安装在
/usr/local/lib
中,请将此目录添加到/etc/ld.so.conf
(它是一个文本文件)并运行 ldconfig该命令将运行缓存实用程序,但还将创建加载器系统运行所需的所有必要的“符号链接”。 令人惊讶的是 libcurl 的
make install
尚未执行此操作,但如果/usr/local/lib
不在/etc 中,则可能无法执行此操作/ld.so.conf 已经。
PS:您的
/etc/ld.so.conf
可能只包含include ld.so.conf.d/*.conf
。 您仍然可以在其后面添加目录路径,或者只是在包含该文件的目录中创建一个新文件。 不要忘记在它之后运行ldconfig
。当心。 犯这个错误可能会毁掉你的系统。
另外:确保你的 python 模块是针对那个版本的 libcurl 编译的。 如果您只是从另一个系统复制一些文件,这并不总是有效。 如果有疑问,请在要运行模块的系统上编译模块。
Ensure your
libcurl.so
module is in the system library path, which is distinct and separate from the python library path.A "quick fix" is to add this path to a
LD_LIBRARY_PATH
variable. However, setting that system wide (or even account wide) is a BAD IDEA, as it is possible to set it in such a way that some programs will find a library it shouldn't, or even worse, open up security holes.If your "locally installed libraries" are installed in, for example,
/usr/local/lib
, add this directory to/etc/ld.so.conf
(it's a text file) and runldconfig
The command will run a caching utility, but will also create all the necessary "symbolic links" required for the loader system to function. It is surprising that the
make install
for libcurl did not do this already, but it's possible it could not if/usr/local/lib
is not in/etc/ld.so.conf
already.PS: it's possible that your
/etc/ld.so.conf
contains nothing butinclude ld.so.conf.d/*.conf
. You can still add a directory path after it, or just create a new file inside the directory it's being included from. Dont forget to runldconfig
after it.Be careful. Getting this wrong can screw up your system.
Additionally: make sure your python module is compiled against THAT version of libcurl. If you just copied some files over from another system, this wont always work. If in doubt, compile your modules on the system you intend to run them on.
当您首先编译 pycurl 时,您还可以在用户环境中将 LD_RUN_PATH 设置为 /usr/local/lib 。 这会将 /usr/local/lib 嵌入到 C 扩展模块 .so 的 RPATH 属性中,以便它在运行时自动知道在哪里找到该库,而无需在运行时设置 LD_LIBRARY_PATH。
You can also set LD_RUN_PATH to /usr/local/lib in your user environment when you compile pycurl in the first place. This will embed /usr/local/lib in the RPATH attribute of the C extension module .so so that it automatically knows where to find the library at run time without having to have LD_LIBRARY_PATH set at run time.
有完全相同的问题。 我将curl 7.19安装到/opt/curl/以确保不会影响生产服务器上当前的curl。
一旦我将 libcurl.so.4 链接到 /usr/lib:
sudo ln -s /opt/curl/lib/libcurl.so /usr/lib/libcurl.so.4
我仍然遇到同样的错误! 杜尔夫。
但运行 ldconfig 为我建立了链接并且有效。 根本不需要设置 LD_RUN_PATH 或 LD_LIBRARY_PATH。 只需运行 ldconfig。
Had the exact same issue. I installed curl 7.19 to /opt/curl/ to make sure that I would not affect current curl on our production servers.
Once I linked libcurl.so.4 to /usr/lib:
sudo ln -s /opt/curl/lib/libcurl.so /usr/lib/libcurl.so.4
I still got the same error! Durf.
But running ldconfig make the linkage for me and that worked. No need to set the LD_RUN_PATH or LD_LIBRARY_PATH at all. Just needed to run ldconfig.
作为上述答案的补充 - 我只是遇到了类似的问题,并且完全使用默认安装的 python 工作。
当我使用 LD_LIBRARY_PATH 调用我正在寻找的共享对象库的示例时,我得到如下信息:
值得注意的是,它甚至不抱怨导入 - 它抱怨源文件!
但是,如果我使用 LD_PRELOAD 强制加载对象:
...我立即收到一条更有意义的错误消息 - 关于缺少依赖项!
只是想我会把这个记在这里 - 干杯!
As a supplement to above answers - I'm just bumping into a similar problem, and working completely of the default installed python.
When I call the example of the shared object library I'm looking for with
LD_LIBRARY_PATH
, I get something like this:Notably, it doesn't even complain about the import - it complains about the source file!
But if I force loading of the object using
LD_PRELOAD
:... I immediately get a more meaningful error message - about a missing dependency!
Just thought I'd jot this down here - cheers!
我使用 python setup.py build_ext -R/usr/local/lib -I/usr/local/include/libcalg-1.0 ,编译后的 .so 文件位于 build 文件夹下。
你可以输入 python setup.py --help build_ext 来查看 -R 和 -I 的解释
I use
python setup.py build_ext -R/usr/local/lib -I/usr/local/include/libcalg-1.0
and the compiled .so file is under the build folder.you can type
python setup.py --help build_ext
to see the explanations of -R and -I对我来说,这里有效的是使用版本管理器,例如 pyenv,我强烈建议您获取项目环境和软件包版本得到良好管理,并与操作系统分开。
操作系统更新后我遇到了同样的错误,但可以使用 pyenv install 3.7-dev (我使用的版本)轻松修复。
For me what works here is to using a version manager such as pyenv, which I strongly recommend to get your project environments and package versions well managed and separate from that of the operative system.
I had this same error after an OS update, but was easily fixed with
pyenv install 3.7-dev
(the version I use).以下内容在 linux-ubuntu 平台上进行了测试 -
尝试找到您的 pyhton dist-packages 文件夹。 就我而言,它是
/usr/local/local/lib/python3.10/dist-packages/
。之后执行以下步骤:
/home/
中打开终端。 就我而言/home/sdebnath
。.bashrc
。 例如。gedit .bashrc
。export PYTHONPATH=$PYTHONPATH:
。 就我而言导出 PYTHONPATH=$PYTHONPATH:/usr/local/local/lib/python3.10/dist-packages/
。.bashrc
文件中的更改生效,请注销当前用户并重新登录。The following were tested in linux-ubuntu platform-
Try to locate your pyhton dist-packages folder. In my case it was
/usr/local/local/lib/python3.10/dist-packages/
.After that do the following steps:
/home/<user>
. In my case/home/sdebnath
..bashrc
in ypur preferred editor. Eg.gedit .bashrc
.export PYTHONPATH=$PYTHONPATH:<path-to-your-python-dist-packages>
. In my caseexport PYTHONPATH=$PYTHONPATH:/usr/local/local/lib/python3.10/dist-packages/
..bashrc
file to take effect, logout from current user and login again.