为什么 Python 找不到 sys.path 目录中的共享对象?

发布于 2024-07-27 01:12:13 字数 796 浏览 11 评论 0原文

我正在尝试导入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

凯凯我们等你回来 2024-08-03 01:12:13

sys.path 仅搜索 Python 模块。 对于动态链接库,搜索的路径必须在LD_LIBRARY_PATH中。 检查您的 LD_LIBRARY_PATH 是否包含 /usr/local/lib,如果不包含,请添加它并重试。

更多信息(来源):

在Linux中,环境变量
LD_LIBRARY_PATH 是一个冒号分隔的
库所在的目录集
应该首先搜索,之前
标准目录集; 这
在调试新库时很有用
或使用非标准库
特殊目的。 环境
变量 LD_PRELOAD 列出共享
具有覆盖函数的库
标准集,就像
/etc/ld.so.preload 确实如此。 这些都是
由加载器实现
/lib/ld-linux.so。 我应该注意的是,
而 LD_LIBRARY_PATH 适用于许多
类 Unix 系统,它不起作用
全部; 例如,这个功能
在 HP-UX 上可用,但作为
环境变量 SHLIB_PATH 和
在 AIX 上,此功能是通过
变量 LIBPATH (具有相同的
语法,冒号分隔的列表)。

要设置LD_LIBRARY_PATH,请使用以下其中一项,最好在您的~/.bashrc
或等效文件:

export LD_LIBRARY_PATH=/usr/local/lib

export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

如果第一种形式为空(相当于空字符串,或根本不存在),则使用第一种形式,如果不是,则使用第二种形式。 请注意导出的使用。

sys.path is only searched for Python modules. For dynamic linked libraries, the paths searched must be in LD_LIBRARY_PATH. Check if your LD_LIBRARY_PATH includes /usr/local/lib, and if it doesn't, add it and try again.

Some more information (source):

In Linux, the environment variable
LD_LIBRARY_PATH is a colon-separated
set of directories where libraries
should be searched for first, before
the standard set of directories; this
is useful when debugging a new library
or using a nonstandard library for
special purposes. The environment
variable LD_PRELOAD lists shared
libraries with functions that override
the standard set, just as
/etc/ld.so.preload does. These are
implemented by the loader
/lib/ld-linux.so. I should note that,
while LD_LIBRARY_PATH works on many
Unix-like systems, it doesn't work on
all; for example, this functionality
is available on HP-UX but as the
environment variable SHLIB_PATH, and
on AIX this functionality is through
the variable LIBPATH (with the same
syntax, a colon-separated list).

To set LD_LIBRARY_PATH, use one of the following, ideally in your ~/.bashrc
or equivalent file:

export LD_LIBRARY_PATH=/usr/local/lib

or

export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

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.

守护在此方 2024-08-03 01:12:13

确保您的 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 run ldconfig

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 but include 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 run ldconfig 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.

小巷里的女流氓 2024-08-03 01:12:13

当您首先编译 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.

空宴 2024-08-03 01:12:13

有完全相同的问题。 我将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.

黑色毁心梦 2024-08-03 01:12:13

作为上述答案的补充 - 我只是遇到了类似的问题,并且完全使用默认安装的 python 工作。

当我使用 LD_LIBRARY_PATH 调用我正在寻找的共享对象库的示例时,我得到如下信息:

$ LD_LIBRARY_PATH=/path/to/mysodir:$LD_LIBRARY_PATH python example-so-user.py
python: can't open file 'example-so-user.py': [Errno 2] No such file or directory

值得注意的是,它甚至不抱怨导入 - 它抱怨源文件!

但是,如果我使用 LD_PRELOAD 强制加载对象:

$ LD_PRELOAD=/path/to/mysodir/mypyobj.so python example-so-user.py
python: error while loading shared libraries: libtiff.so.5: cannot open shared object file: No such file or directory

...我立即收到一条更有意义的错误消息 - 关于缺少依赖项!

只是想我会把这个记在这里 - 干杯!

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:

$ LD_LIBRARY_PATH=/path/to/mysodir:$LD_LIBRARY_PATH python example-so-user.py
python: can't open file 'example-so-user.py': [Errno 2] No such file or directory

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:

$ LD_PRELOAD=/path/to/mysodir/mypyobj.so python example-so-user.py
python: error while loading shared libraries: libtiff.so.5: cannot open shared object file: No such file or directory

... I immediately get a more meaningful error message - about a missing dependency!

Just thought I'd jot this down here - cheers!

尤怨 2024-08-03 01:12:13

我使用 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

倥絔 2024-08-03 01:12:13

对我来说,这里有效的是使用版本管理器,例如 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).

聚集的泪 2024-08-03 01:12:13

以下内容在 linux-ubuntu 平台上进行了测试 -

尝试找到您的 pyhton dist-packages 文件夹。 就我而言,它是 /usr/local/local/lib/python3.10/dist-packages/

之后执行以下步骤:

  1. /home/ 中打开终端。 就我而言 /home/sdebnath
  2. 在您首选的编辑器中打开文件 .bashrc。 例如。 gedit .bashrc
  3. 在文件末尾添加此行:export PYTHONPATH=$PYTHONPATH:。 就我而言
    导出 PYTHONPATH=$PYTHONPATH:/usr/local/local/lib/python3.10/dist-packages/
  4. 保存文件并退出。
  5. 要使 .bashrc 文件中的更改生效,请注销当前用户并重新登录。
  6. 尝试执行您的代码并查看问题是否存在。

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:

  1. Open terminal in /home/<user>. In my case /home/sdebnath.
  2. Open file .bashrc in ypur preferred editor. Eg. gedit .bashrc.
  3. Add this line at the end of the file: export PYTHONPATH=$PYTHONPATH:<path-to-your-python-dist-packages>. In my case
    export PYTHONPATH=$PYTHONPATH:/usr/local/local/lib/python3.10/dist-packages/.
  4. Save the file and exit.
  5. For the chnages in the .bashrc file to take effect, logout from current user and login again.
  6. Try executing your code and see problem exists or not.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文