在RHEL 7.2上安装Python 2.7.8和2.7.5的问题

时间:2016-05-20 11:03:31

标签: python linux python-2.7

我安装了Red Hat Linux 7.2的Amazon EC2实例。它随系统安装了Python 2.7.5。

我已经编译了Python 2.7.8的替代安装(这是我在另一个我尝试复制的环境中使用的)。我将使用它创建一个virtualenv来启动我的Django应用程序。

但是调用python2.7总是把我带到系统范围的python 2.7.5解释器。

我按照http://toomuchdata.com/2014/02/16/how-to-install-python-on-centos/

上的说明操作
$ sudo yum groupinstall 'development tools'
$ sudo yum install  zlib-dev openssl-devel sqlite-devel bzip2-devel
$ sudo yum install wget
$ sudo wget http://www.python.org/ftp/python/2.7.8/Python-2.7.8.tar.xz
$ sudo yum install xz-libs
$ xz -d Python-2.7.8.tar.xz
$ tar -xvf Python-2.7.8.tar
$ cd Python-2.7.8

编辑文件/etc/ld.so.conf以添加行

/usr/local/lib

$sudo /sbin/ldconfig --> make the dynamic linker aware of the change

$sudo ./configure --prefix=/usr/local  --enable-unicode=ucs4  --enable-shared
$make

我收到以下消息,但我不确定这是否与我的问题相关。

Python build finished, but the necessary bits to build these modules were not found:
_bsddb             _tkinter           bsddb185
dbm                dl                 gdbm
imageop            sunaudiodev
To find the necessary bits, look in setup.py in detect_modules() for the module's name.

$sudo make altinstall

$which python
/usr/bin/python ---> system wide Python 2.7.5
$which python2.7
/usr/local/bin/python2.7 ---> this should point to Python 2.7.8 that was compiled and installed.

但调用python2.7指向系统范围的Python 2.7.5解释器:

$python2.7
Python 2.7.5 (default, Oct 11 2015, 17:47:16)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2

为什么会这样?

仅供参考,我的PATH包含/ usr / local / bin。

$echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ec2-user/.local/bin:/home/ec2-user/bin

1 个答案:

答案 0 :(得分:0)

参考:http://koansys.com/tech/building-python-with-enable-shared-in-non-standard-location “我可以使用”ldd“将它们的库添加到系统缓存中,只要没有任何其他相同版本的pythons需要它,这就可以工作。”

我通过从/etc/ld.so.conf中删除/ usr / local / lib并使用-rpath选项指定LDFLAGS来解决我的问题,如下所示:

$sudo ./configure --prefix=/usr/local/python --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/python/lib"

注意它是-Wl而不是-W1(即小写字母L而不是数字1)。我花了好几个小时搞清楚这个错误!感谢gcc soname unrecognized command line 指出这一点。

$make
$sudo make altinstall

这很有用,从这条路径调用Python解释器工作正常:

$/usr/local/python/bin/python2.7 -- version
Python 2.7.8

Web上的大多数教程都说明了如何并排安装两个不同版本的Python,例如Python 2.6.x和2.7.x或Python 2.7.x和3.3.x。

但是当两个安装都是Python 2.7.x时,如果在/etc/ld.so.conf中指定了自定义安装的路径,则似乎存在问题 - 可能正在拾取的库属于系统默认值。 ..我还是不明白引擎盖下发生了什么。

我还在.bashrc中将/ usr / local / python / bin添加到了我的PATH。

但是,在安装setuptools,pip,virtualenv和virtualenvwrapper的后续步骤中,我仍然必须显式调用python2.7或pip2.7的完整路径,否则该包安装在/ usr / lib / python2中。 7 / site-packages /而不是/usr/local/python/lib/python2.7/site-packages。