标签归档:pycurl

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

问题:为什么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.


回答 0

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).

Update: 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.


回答 1

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


回答 2

最初编译pycurl时,还可以在用户环境中将LD_RUN_PATH设置为/ usr / local / lib。这会将/ usr / local / lib嵌入C扩展模块。的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.


回答 3

有完全相同的问题。我将curl 7.19安装到/ opt / curl /中,以确保不会影响生产服务器上的当前curl。将libcurl.so.4链接到/ usr / lib之后:

须藤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.


回答 4

作为上述答案的补充-我只是遇到了类似的问题,并且完全使用默认安装的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!


回答 5

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


回答 6

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


安装pycurl时出现“无法运行curl-config:[Errno 2]没有这样的文件或目录”

问题:安装pycurl时出现“无法运行curl-config:[Errno 2]没有这样的文件或目录”

我正在尝试通过以下方式安装pycurl:

sudo pip install pycurl

它下载得很好,但是当它运行setup.py时,我得到以下回溯:

Downloading/unpacking pycurl
  Running setup.py egg_info for package pycurl
    Traceback (most recent call last):
      File "<string>", line 16, in <module>
      File "/tmp/pip-build-root/pycurl/setup.py", line 563, in <module>
        ext = get_extension()
      File "/tmp/pip-build-root/pycurl/setup.py", line 368, in get_extension
        ext_config = ExtensionConfiguration()
      File "/tmp/pip-build-root/pycurl/setup.py", line 65, in __init__
        self.configure()
      File "/tmp/pip-build-root/pycurl/setup.py", line 100, in configure_unix
        raise ConfigurationError(msg)
    __main__.ConfigurationError: Could not run curl-config: [Errno 2] No such file or directory
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):

  File "<string>", line 16, in <module>

  File "/tmp/pip-build-root/pycurl/setup.py", line 563, in <module>

    ext = get_extension()

  File "/tmp/pip-build-root/pycurl/setup.py", line 368, in get_extension

    ext_config = ExtensionConfiguration()

  File "/tmp/pip-build-root/pycurl/setup.py", line 65, in __init__

    self.configure()

  File "/tmp/pip-build-root/pycurl/setup.py", line 100, in configure_unix

    raise ConfigurationError(msg)

__main__.ConfigurationError: Could not run curl-config: [Errno 2] No such file or directory

任何想法为什么会这样以及如何解决它

I’m trying to install pycurl via:

sudo pip install pycurl

It downloaded fine, but when when it runs setup.py I get the following traceback:

Downloading/unpacking pycurl
  Running setup.py egg_info for package pycurl
    Traceback (most recent call last):
      File "<string>", line 16, in <module>
      File "/tmp/pip-build-root/pycurl/setup.py", line 563, in <module>
        ext = get_extension()
      File "/tmp/pip-build-root/pycurl/setup.py", line 368, in get_extension
        ext_config = ExtensionConfiguration()
      File "/tmp/pip-build-root/pycurl/setup.py", line 65, in __init__
        self.configure()
      File "/tmp/pip-build-root/pycurl/setup.py", line 100, in configure_unix
        raise ConfigurationError(msg)
    __main__.ConfigurationError: Could not run curl-config: [Errno 2] No such file or directory
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):

  File "<string>", line 16, in <module>

  File "/tmp/pip-build-root/pycurl/setup.py", line 563, in <module>

    ext = get_extension()

  File "/tmp/pip-build-root/pycurl/setup.py", line 368, in get_extension

    ext_config = ExtensionConfiguration()

  File "/tmp/pip-build-root/pycurl/setup.py", line 65, in __init__

    self.configure()

  File "/tmp/pip-build-root/pycurl/setup.py", line 100, in configure_unix

    raise ConfigurationError(msg)

__main__.ConfigurationError: Could not run curl-config: [Errno 2] No such file or directory

Any idea why this is happening and how to get around it


回答 0

在Debian上,我需要以下软件包来解决此问题

sudo apt install libcurl4-openssl-dev libssl-dev

On Debian I needed the following packages to fix this

sudo apt install libcurl4-openssl-dev libssl-dev

回答 1

yum包管理器类似

yum install libcurl-devel

如果您使用dnf,请使用

dnf install libcurl-devel

Similarly with yum package manager

yum install libcurl-devel

If you use dnf, use

dnf install libcurl-devel

回答 2

就我而言,这解决了问题:

sudo apt-get install libssl-dev libcurl4-openssl-dev python-dev

这里解释

in my case this fixed the problem:

sudo apt-get install libssl-dev libcurl4-openssl-dev python-dev

as explained here


回答 3

在Alpine linux中,您应该执行以下操作:

apk add curl-dev python3-dev libressl-dev

In Alpine linux you should do:

apk add curl-dev python3-dev libressl-dev

回答 4

我在尝试使Shinken 2.0.3在Ubuntu上启动时遇到了相同的问题。最终,我进行了完全卸载,然后使用重新安装了Shinken pip -v。在清理过程中,它提到:

Warning: missing python-pycurl lib, you MUST install it before launch the shinken daemons

安装了apt-get,所有经纪人都按预期启动了:-)

I encountered the same problem whilst trying to get Shinken 2.0.3 to fire up on Ubuntu. Eventually I did a full uninstall then reinstalled Shinken with pip -v. As it cleaned up, it mentioned:

Warning: missing python-pycurl lib, you MUST install it before launch the shinken daemons

Installed that with apt-get, and all the brokers fired up as expected :-)


回答 5

那解决了我在Ubuntu 14.04上的问题:

apt-get安装libcurl4-gnutls-dev

That solved my problem on Ubuntu 14.04:

apt-get install libcurl4-gnutls-dev


回答 6

在OpenSUSE上:

zypper in libcurl-devel 

On OpenSUSE:

zypper in libcurl-devel 

回答 7

就我而言,我一直收到相同的错误消息。我使用软呢帽。我通过以下方法解决了它:

sudo dnf install pycurl

这安装了我需要它运行的一切。

In my case i kept getting the same error message. I use fedora. I solved it by doing:

sudo dnf install pycurl

This installed eveything that I needed for it to work.


回答 8

我在Mac上遇到了这个问题,这与该openssl软件包是所需的旧版本有关pycurlpycurl根据我的理解,可以使用其他SSL库而不是openssl。验证您所使用的SSL库并进行更新,因为它很可能解决了该问题。

我通过以下方式解决了这个问题:

  • 跑步 brew upgrade
  • pycurl-x.y.z.tar.gzhttp://pycurl.io/下载了最新的
  • 提取上面的包并在其中更改目录
  • python setup.py --with-openssl install如OpenSSL是我已经安装了库。如果你是SSL库或者是gnutls还是nss那么将不得不使用--with-gnutls--with-nss相应。您将可以在其github存储库中找到更多安装信息

I had this issue on Mac and it was related to the openssl package being an older version of what it was required by pycurl. pycurl can use other ssl libraries rather than openssl as per my understanding of it. Verify which ssl library you’re using and update as it is very likely to fix the issue.

I fixed this by:

  • running brew upgrade
  • downloaded the latest pycurl-x.y.z.tar.gz from http://pycurl.io/
  • extracted the package above and change directory into it
  • ran python setup.py --with-openssl install as openssl is the library I have installed. If you’re ssl library is either gnutls or nss then will have to use --with-gnutls or --with-nss accordingly. You’ll be able to find more installation info in their github repository.

回答 9

除了eldos的答案外,我gcc在CentOS 7中还需要:

yum install libcurl-devel gcc

In addition to the answer of eldos I also needed gcc in CentOS 7:

yum install libcurl-devel gcc

回答 10

请注意,如果您使用的是nodejs,则在编写本文时,它会依赖libssl 1.0。*-因此,安装备用SSL库将破坏您的nodejs安装。

安装不同SSL库的另一种解决方案是此答案中的此处发布:https : //stackoverflow.com/a/59927568/13564342来代替安装libcurl4-gnutls-dev

sudo apt install libcurl4-gnutls-dev

Be advised that if you’re using nodejs there’s (at the time of writing) a dependency on libssl 1.0.* – so installing an alternative SSL library will break your nodejs installation.

An alternative solution to installing a different SSL library is that posted in this answer here: https://stackoverflow.com/a/59927568/13564342 to instead install libcurl4-gnutls-dev

sudo apt install libcurl4-gnutls-dev