Skip to main content
 首页 » 编程设计

python之为什么我不能在Centos 5.5下安装Python 2.7

2025年05月04日10shangdawei

Centos 5.5 安装了 python 2.4,而我的项目需要 python 2.7。我下载了源代码,运行,删除,并再次尝试使用几个替代版本:

./configure && make && make install 
./configure && make && make altinstall 
./configure --prefix=/opt/python2.7 && make && make install  

我继续安装 setuptools 和 virtualenv(确保引用正确版本的 python,没有符号链接(symbolic link)或任何其他奇怪的东西)。为该项目构建了一个 virtualenv,为我的服务器正确配置了所有内容,我的最后一步是从 apache 2.2.1 的源代码构建 mod_wsgi 3.3,使用

./configure --with-apxs=/path/to/apxs --with-python=/path/to/py2.7 && make && make install 

全部三次,运行 python,在本地测试项目等,都运行良好。

但是在安装 mod_wsgi 之后,我的 wsgi 应用程序一直出现愚蠢的导入错误(无法导入 os、sys、站点等)。

我在官方焦点 https://code.google.com/p/modwsgi/wiki/InstallationIssues 的页面中间运行了简短的 sys.path 测试脚本。我在我的日志文件中得到以下输出:

sys.path = [ '/correct/path/to/virtualenv/py2.7/lib64/python2.4/', ... ] 

从 code.google.com/p/modwsgi 的建议开始,我尝试了几种不同的 WSGIPythonHome 和 WSGIPythonPath 外观……

WSGIPythonHome=/path/to/virtualenv/ and ../bin/ and ../bin/python2.7, 
WSGIPythonPath=../lib and ../lib/python2.7 and ../lib/python2.7/site-packages.   

WSGI 和 Apache 似乎不是这里的问题。这是我编译 python2.7 的错误,还是我的 easy_install 和 virtualenv 配置的错误?

请您参考如下方法:

首先,您并没有表明您已经使用 --enable-shared 编译了 Python。如果这不是该 Python 版本的默认设置,那是不可取的。根据:

http://code.google.com/p/modwsgi/wiki/InstallationIssues#Lack_Of_Python_Shared_Library

您可以检查 Python 是否安装了共享库。

编译 mod_wsgi 后,查看 mod_wsgi.so 从哪里获取 libpython2.7.so。

接下来,请确保您的系统中没有多个不同的 libpython2.7.so,因为您多次尝试安装 Python。你真的应该只有一个。

如果 libpython2.7.so 不在标准库搜索路径中,则该目录的位置应该在编译时嵌入到 mod_wsgi 中。如果安装在/opt/python2.7 下,你会这样做:

./configure --with-python=/opt/python2.7/bin/python --with-apxs=/path/to/apxs 
LD_RUN_PATH=/opt/python2.7/lib make 
make install 

然后,您应该通过在命令行 Python 调用中运行来确定该 Python 安装的 sys.prefix。

import sys 
print sys.prefix 

然后应使用 WSGIPythonHome 设置它所说的值。该位置应该是/opt/python2.7。 WSGIPythonHome 是必需的,因为您已经安装了系统范围 Python 的备用前缀。

您不应将 Python 安装目录位置直接添加到 sys.path 或 WSGIPythonPath。

使用 hello world WSGI 程序进行大量工作。没有虚拟环境,没有 Django 或更高级别的框架。

请注意,Python 安装必须对运行 Apache 的用户可读。