[英]Python executable not finding libpython shared library
I am installing Python 2.7 on CentOS 5. I built and installed Python as follows我在 CentOS 5 上安装 Python 2.7。我构建和安装 Python 如下
./configure --enable-shared --prefix=/usr/local
make
make install
When I try to run /usr/local/bin/python, I get this error message当我尝试运行 /usr/local/bin/python 时,我收到此错误消息
/usr/local/bin/python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory
When I run ldd on /usr/local/bin/python, I get当我在 /usr/local/bin/python 上运行 ldd 时,我得到
ldd /usr/local/bin/python
libpython2.7.so.1.0 => not found
libpthread.so.0 => /lib64/libpthread.so.0 (0x00000030e9a00000)
libdl.so.2 => /lib64/libdl.so.2 (0x00000030e9200000)
libutil.so.1 => /lib64/libutil.so.1 (0x00000030fa200000)
libm.so.6 => /lib64/libm.so.6 (0x00000030e9600000)
libc.so.6 => /lib64/libc.so.6 (0x00000030e8e00000)
/lib64/ld-linux-x86-64.so.2 (0x00000030e8a00000)
How do I tell Python where to find libpython?我如何告诉 Python 在哪里可以找到 libpython?
Try the following:请尝试以下操作:
LD_LIBRARY_PATH=/usr/local/lib /usr/local/bin/python
Replace /usr/local/lib
with the folder where you have installed libpython2.7.so.1.0
if it is not in /usr/local/lib
.将/usr/local/lib
替换为您安装libpython2.7.so.1.0
的文件夹,如果它不在/usr/local/lib
。
If this works and you want to make the changes permanent, you have two options:如果这有效并且您希望更改永久有效,您有两个选择:
Add export LD_LIBRARY_PATH=/usr/local/lib
to your .profile
in your home directory (this works only if you are using a shell which loads this file when a new shell instance is started).将export LD_LIBRARY_PATH=/usr/local/lib
到您的主目录中的.profile
(这仅在您使用在启动新 shell 实例时加载此文件的 shell 时才有效)。 This setting will affect your user only.此设置只会影响您的用户。
Add /usr/local/lib
to /etc/ld.so.conf
and run ldconfig
.将/usr/local/lib
添加到/etc/ld.so.conf
并运行ldconfig
。 This is a system-wide setting of course.这当然是系统范围的设置。
Putting on my gravedigger hat...戴上我的掘墓人帽子...
The best way I've found to address this is at compile time.我发现解决这个问题的最好方法是在编译时。 Since you're the one setting prefix anyway might as well tell the executable explicitly where to find its shared libraries.由于您是唯一的设置前缀,因此不妨明确告诉可执行文件在哪里可以找到其共享库。 Unlike OpenSSL and other software packages, Python doesn't give you nice configure directives to handle alternate library paths (not everyone is root you know...) In the simplest case all you need is the following:与 OpenSSL 和其他软件包不同,Python 没有为您提供很好的配置指令来处理备用库路径(您知道不是每个人都是 root...)在最简单的情况下,您只需要以下内容:
./configure --enable-shared \
--prefix=/usr/local \
LDFLAGS="-Wl,--rpath=/usr/local/lib"
Or if you prefer the non-linux version:或者,如果您更喜欢非 linux 版本:
./configure --enable-shared \
--prefix=/usr/local \
LDFLAGS="-R/usr/local/lib"
The " rpath
" flag tells python it has runtime libraries it needs in that particular path. “ rpath
”标志告诉python它在该特定路径中有它需要的运行时库。 You can take this idea further to handle dependencies installed to a different location than the standard system locations.您可以进一步采用这个想法来处理安装到与标准系统位置不同的位置的依赖项。 For example, on my systems since I don't have root access and need to make almost completely self-contained Python installs, my configure line looks like this:例如,在我的系统上,由于我没有 root 访问权限并且需要进行几乎完全独立的 Python 安装,我的配置行如下所示:
./configure --enable-shared \
--with-system-ffi \
--with-system-expat \
--enable-unicode=ucs4 \
--prefix=/apps/python-${PYTHON_VERSION} \
LDFLAGS="-L/apps/python-${PYTHON_VERSION}/extlib/lib -Wl,--rpath=/apps/python-${PYTHON_VERSION}/lib -Wl,--rpath=/apps/python-${PYTHON_VERSION}/extlib/lib" \
CPPFLAGS="-I/apps/python-${PYTHON_VERSION}/extlib/include"
In this case I am compiling the libraries that python uses (like ffi
, readline
, etc) into an extlib
directory within the python directory tree itself.在这种情况下,我将 python 使用的库(如ffi
、 readline
等) extlib
到 python 目录树本身内的extlib
目录中。 This way I can tar the python-${PYTHON_VERSION} directory and land it anywhere and it will "work" (provided you don't run into libc
or libm
conflicts).这样我就可以对 python-${PYTHON_VERSION} 目录进行 tar 并将它放在任何地方,它就会“工作”(前提是你没有遇到libc
或libm
冲突)。 This also helps when trying to run multiple versions of Python on the same box, as you don't need to keep changing your LD_LIBRARY_PATH
or worry about picking up the wrong version of the Python library.这也有助于在同一台机器上运行多个版本的 Python,因为您无需不断更改LD_LIBRARY_PATH
或担心选择错误版本的 Python 库。
Edit: Forgot to mention, the compile will complain if you don't set the PYTHONPATH
environment variable to what you use as your prefix and fail to compile some modules, eg, to extend the above example, set the PYTHONPATH
to the prefix used in the above example with export PYTHONPATH=/apps/python-${PYTHON_VERSION}
...编辑:忘了说,如果你没有将PYTHONPATH
环境变量设置为你用作前缀的环境变量并且编译某些模块失败,编译会报错,例如,为了扩展上面的例子,将PYTHONPATH
设置为使用的前缀上面的示例带有export PYTHONPATH=/apps/python-${PYTHON_VERSION}
...
I had the same problem and I solved it this way:我遇到了同样的问题,我是这样解决的:
If you know where libpython resides at, I supposed it would be /usr/local/lib/libpython2.7.so.1.0
in your case, you can just create a symbolic link to it:如果您知道 libpython 所在的位置,我想在您的情况下应该是/usr/local/lib/libpython2.7.so.1.0
,您只需创建一个符号链接即可:
sudo ln -s /usr/local/lib/libpython2.7.so.1.0 /usr/lib/libpython2.7.so.1.0
Then try running ldd
again and see if it worked.然后再次尝试运行ldd
并查看它是否有效。
I installed Python 3.5 by Software Collections on CentOS 7 minimal.我在最小的 CentOS 7 上通过Software Collections安装了 Python 3.5。 It all worked fine on its own, but I saw the shared library error mentioned in this question when I tried running a simple CGI script:它本身一切正常,但是当我尝试运行一个简单的 CGI 脚本时,我看到了这个问题中提到的共享库错误:
tail /var/log/httpd/error_log
AH01215: /opt/rh/rh-python35/root/usr/bin/python: error while loading shared libraries: libpython3.5m.so.rh-python35-1.0: cannot open shared object file: No such file or directory
I wanted a systemwide permanent solution that works for all users, so that excluded adding export statements to .profile or .bashrc files.我想要一个适用于所有用户的系统范围的永久解决方案,以便排除向 .profile 或 .bashrc 文件添加导出语句。 There is a one-line solution, based on the Red Hat solutions page.有一种基于Red Hat 解决方案页面的单行解决方案。 Thanks for the comment that points it out:感谢您指出这一点的评论:
echo 'source scl_source enable rh-python35' | sudo tee --append /etc/profile.d/python35.sh
After a restart, it's all good on the shell, but sometimes my web server still complains.重新启动后,在 shell 上一切正常,但有时我的 Web 服务器仍然会抱怨。 There's another approach that always worked for both the shell and the server, and is more generic.还有另一种方法始终适用于外壳程序和服务器,并且更通用。 I saw the solution here and then realized it's actually mentioned in one of the answers here as well!我在这里看到了解决方案,然后意识到它实际上也在此处的其中一个答案中提到过! Anyway, on CentOS 7, these are the steps:无论如何,在 CentOS 7 上,这些是步骤:
vim /etc/ld.so.conf
Which on my machine just had:我的机器上只有:
include ld.so.conf.d/*.conf
So I created a new file:所以我创建了一个新文件:
vim /etc/ld.so.conf.d/rh-python35.conf
And added:并补充说:
/opt/rh/rh-python35/root/usr/lib64/
And to manually rebuild the cache:并手动重建缓存:
sudo ldconfig
That's it, scripts work fine!就是这样,脚本工作正常!
This was a temporary solution, which didn't work across reboots:这是一个临时解决方案,在重新启动后不起作用:
sudo ldconfig /opt/rh/rh-python35/root/usr/lib64/ -v
The -v (verbose) option was just to see what was going on. -v(详细)选项只是为了看看发生了什么。 I saw that it did: /opt/rh/rh-python35/root/usr/lib64: libpython3.so.rh-python35 -> libpython3.so.rh-python35 libpython3.5m.so.rh-python35-1.0 -> libpython3.5m.so.rh-python35-1.0我看到它做了:/opt/rh/rh-python35/root/usr/lib64: libpython3.so.rh-python35 -> libpython3.so.rh-python35 libpython3.5m.so.rh-python35-1.0 -> libpython3.5m.so.rh-python35-1.0
This particular error went away.这个特殊的错误消失了。 Incidentally, I had to chown
the user to apache to get rid of a permission error after that.顺便说一句,在那之后,我不得不将用户chown
到 apache 以摆脱权限错误。
Note that I used find to locate the directory for the library.请注意,我使用find来定位库的目录。 You could also do:你也可以这样做:
sudo yum install mlocate
sudo updatedb
locate libpython3.5m.so.rh-python35-1.0
Which on my VM returns:在我的 VM 上返回:
/opt/rh/rh-python35/root/usr/lib64/libpython3.5m.so.rh-python35-1.0
Which is the path I need to give to ldconfig, as shown above.这是我需要给 ldconfig 的路径,如上所示。
On Solaris 11在 Solaris 11 上
Use LD_LIBRARY_PATH_64
to resolve symlink to python libs.使用LD_LIBRARY_PATH_64
将符号链接解析为 python 库。
In my case for python3.6 LD_LIBRARY_PATH
didn't work but LD_LIBRARY_PATH_64
did.在我的情况下,python3.6 LD_LIBRARY_PATH
不起作用,但LD_LIBRARY_PATH_64
起作用了。
Hope this helps.希望这可以帮助。
Regards问候
这对我有用...
$ sudo apt-get install python2.7-dev
I installed using the command:我使用以下命令安装:
./configure --prefix=/usr \
--enable-shared \
--with-system-expat \
--with-system-ffi \
--enable-unicode=ucs4 &&
make
Now, as the root user:现在,作为 root 用户:
make install &&
chmod -v 755 /usr/lib/libpython2.7.so.1.0
Then I tried to execute python and got the error:然后我尝试执行python并得到错误:
/usr/local/bin/python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory /usr/local/bin/python:加载共享库时出错:libpython2.7.so.1.0:无法打开共享对象文件:没有这样的文件或目录
Then, I logged out from root user and again tried to execute the Python and it worked successfully.然后,我从 root 用户注销并再次尝试执行 Python 并成功运行。
它所需要的只是安装 libpython [3 或 2] 开发文件安装。
This answer would be helpful to those who have limited auth access on the server.此答案对那些在服务器上具有有限身份验证访问权限的人会有所帮助。
I had a similar problem for python3.5
in HostGator's shared hosting.我在 HostGator 的共享托管中遇到了类似的python3.5
问题。 Python3.5
had to be enabled every single damn time after login.每次登录后都必须启用Python3.5
。 Here are my 10 steps for resolution:以下是我的 10 个解决步骤:
Enable the python through scl script python_enable_3.5
or scl enable rh-python35 bash
.通过 scl 脚本python_enable_3.5
或scl enable rh-python35 bash
启用 python。
Verify that it's enabled by executing python3.5 --version
.通过执行python3.5 --version
验证它是否已启用。 This should give you your python version.这应该给你你的python版本。
Execute which python3.5
to get its path.执行which python3.5
得到它的路径。 In my case, it was /opt/rh/rh-python35/root/usr/bin/python3.5
.就我而言,它是/opt/rh/rh-python35/root/usr/bin/python3.5
。 You can use this path get the version again (just to verify that this path is working for you.)您可以使用此路径再次获取版本(只是为了验证此路径是否适合您。)
Awesome, now please exit out of current shell by scl
.太棒了,现在请通过scl
退出当前的 shell。
Now, lets get the version again through this complete python3.5 path /opt/rh/rh-python35/root/usr/bin/python3.5 --version
.现在,让我们通过这个完整的 python3.5 路径/opt/rh/rh-python35/root/usr/bin/python3.5 --version
再次/opt/rh/rh-python35/root/usr/bin/python3.5 --version
。
It won't give you the version but an error.它不会给你版本,而是一个错误。 In my case, it was就我而言,它是
/opt/rh/rh-python35/root/usr/bin/python3.5: error while loading shared libraries: libpython3.5m.so.rh-python35-1.0: cannot open shared object file: No such file or directory
As mentioned in Tamas' answer , we gotta find that so
file.正如提到塔马斯的回答,我们得找到so
的文件。 locate
doesn't work in shared hosting and you can't install that too. locate
在共享主机中不起作用,您也无法安装它。
Use the following command to find where that file is located:使用以下命令查找该文件所在的位置:
find /opt/rh/rh-python35 -name "libpython3.5m.so.rh-python35-1.0"
find: `/opt/rh/rh-python35/root/root': Permission denied
/opt/rh/rh-python35/root/usr/lib64/libpython3.5m.so.rh-python35-1.0
LD_LIBRARY_PATH=/opt/rh/rh-python35/root/usr/lib64 /opt/rh/rh-python35/root/usr/bin/python3.5 --version
alias python351='LD_LIBRARY_PATH=/opt/rh/rh-python35/root/usr/lib64 /opt/rh/rh-python35/root/usr/bin/python3.5'
.bashrc
by source ~/.bashrc
and execute python351 --version
.为了进行验证,重新加载.bashrc
通过source ~/.bashrc
并执行python351 --version
。 Well, there you go, now whenever you login again, you have got python351
to welcome you.好了,好了,现在每当您再次登录时,都会有python351
来欢迎您。
This is not just limited to python3.5
, but can be helpful in case of other scl
installed softwares.这不仅限于python3.5
,而且在其他scl
安装软件的情况下也很有帮助。
just install python-lib.只需安装python-lib。 (python27-lib). (python27-lib)。 It will install libpython2.7.so1.0.它将安装 libpython2.7.so1.0。 We don't require to manually set anything.我们不需要手动设置任何东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.