Python可执行程序没有找到libpython共享库。

时间:2022-02-23 22:07:29

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?

7 个解决方案

#1


169  

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中,则将/usr/local/lib替换为已安装libpython2.7.so.1.0的文件夹。

If this works and you want to make the changes permanent, you have two options:

如果这是可行的,并且你想要永久改变,你有两个选择:

  1. 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). This setting will affect your user only.

    在您的主目录中添加export LD_LIBRARY_PATH=/usr/local/lib到您的.profile文件(只有当您使用一个shell时,当一个新的shell实例启动时才加载该文件)。此设置只会影响您的用户。

  2. Add /usr/local/lib to /etc/ld.so.conf and run ldconfig. This is a system-wide setting of course.

    添加/usr/local/lib /etc/ld.so.配置和运行ldconfig。这是一个系统范围的设置。

#2


58  

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

“rpath”标志告诉python它有在特定路径中需要的运行时库。您可以进一步将此思想用于处理安装到不同位置的依赖项,而不是标准的系统位置。例如,在我的系统中,由于我没有根访问权,并且需要创建几乎完全独立的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. 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). 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使用的库(如ffi、readline等),在python目录树本身的extlib目录中。通过这种方式,我可以将python-${PYTHON_VERSION}目录设置为tar,并将其放置到任何地方,它将“工作”(前提是您不会遇到libc或libm冲突)。当您尝试在同一个盒子上运行多个版本的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, e.g., 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}…

#3


14  

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,看看它是否有效。

#4


0  

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:

现在,作为根用户:

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

在加载共享库时出现错误:libpython2.7.so.1.0:不能打开共享对象文件:没有这样的文件或目录。

Then, I logged out from root user and again tried to execute the Python and it worked successfully.

然后,我从根用户中退出,再次尝试执行Python并成功地工作。

#5


0  

I installed Python 3.5 by Software Collections on CentOS 7 minimal. 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:

我在CentOS 7最小值上安装了Python 3.5。这一切都很正常,但是当我尝试运行一个简单的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. 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:

我想要一个对所有用户都有效的系统永久解决方案,这样就排除了向.profile或.bashrc文件添加导出语句。我在这里看到了解决方案,然后意识到它实际上也在这里的一个答案中提到过!总之,在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/

After a reboot, the following step was not necessary, but 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. 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

v (verbose)选项只是为了看看发生了什么。我看到它做了:/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 chmod the user to apache to get rid of a permission error after that.

这个特殊的错误消失了。顺便说一下,在此之后,我必须对用户进行chmod以消除权限错误。

Note that I used find to locate the directory for the library. You could also do:

注意,我使用find查找库的目录。你也可以做的事:

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的路径,如上所示。

#6


0  

This worked for me...

这工作对我来说…

$ sudo apt-get install python2.7-dev

#7


-1  

just install python-lib. (python27-lib). It will install libpython2.7.so1.0. We don't require to manually set anything.

只是安装python-lib。(python27-lib)。它将安装libpython2.7.so1.0。我们不需要手动设置任何东西。

#1


169  

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中,则将/usr/local/lib替换为已安装libpython2.7.so.1.0的文件夹。

If this works and you want to make the changes permanent, you have two options:

如果这是可行的,并且你想要永久改变,你有两个选择:

  1. 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). This setting will affect your user only.

    在您的主目录中添加export LD_LIBRARY_PATH=/usr/local/lib到您的.profile文件(只有当您使用一个shell时,当一个新的shell实例启动时才加载该文件)。此设置只会影响您的用户。

  2. Add /usr/local/lib to /etc/ld.so.conf and run ldconfig. This is a system-wide setting of course.

    添加/usr/local/lib /etc/ld.so.配置和运行ldconfig。这是一个系统范围的设置。

#2


58  

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

“rpath”标志告诉python它有在特定路径中需要的运行时库。您可以进一步将此思想用于处理安装到不同位置的依赖项,而不是标准的系统位置。例如,在我的系统中,由于我没有根访问权,并且需要创建几乎完全独立的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. 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). 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使用的库(如ffi、readline等),在python目录树本身的extlib目录中。通过这种方式,我可以将python-${PYTHON_VERSION}目录设置为tar,并将其放置到任何地方,它将“工作”(前提是您不会遇到libc或libm冲突)。当您尝试在同一个盒子上运行多个版本的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, e.g., 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}…

#3


14  

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,看看它是否有效。

#4


0  

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:

现在,作为根用户:

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

在加载共享库时出现错误:libpython2.7.so.1.0:不能打开共享对象文件:没有这样的文件或目录。

Then, I logged out from root user and again tried to execute the Python and it worked successfully.

然后,我从根用户中退出,再次尝试执行Python并成功地工作。

#5


0  

I installed Python 3.5 by Software Collections on CentOS 7 minimal. 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:

我在CentOS 7最小值上安装了Python 3.5。这一切都很正常,但是当我尝试运行一个简单的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. 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:

我想要一个对所有用户都有效的系统永久解决方案,这样就排除了向.profile或.bashrc文件添加导出语句。我在这里看到了解决方案,然后意识到它实际上也在这里的一个答案中提到过!总之,在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/

After a reboot, the following step was not necessary, but 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. 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

v (verbose)选项只是为了看看发生了什么。我看到它做了:/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 chmod the user to apache to get rid of a permission error after that.

这个特殊的错误消失了。顺便说一下,在此之后,我必须对用户进行chmod以消除权限错误。

Note that I used find to locate the directory for the library. You could also do:

注意,我使用find查找库的目录。你也可以做的事:

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的路径,如上所示。

#6


0  

This worked for me...

这工作对我来说…

$ sudo apt-get install python2.7-dev

#7


-1  

just install python-lib. (python27-lib). It will install libpython2.7.so1.0. We don't require to manually set anything.

只是安装python-lib。(python27-lib)。它将安装libpython2.7.so1.0。我们不需要手动设置任何东西。