使用.sh将python模块安装到非默认版本的python

时间:2013-06-24 03:36:35

标签: python installation install centos python-install

我遇到类似这篇文章的问题:Install python module to non default version of python on Mac,所以我知道这些解决方案,但它们对我不起作用。

我在CentOS上安装M2Crypto,这意味着我要使用fedora_setup.sh build后跟fedora_setup.sh install以便在我的架构上安装。

不幸的是,默认的Python版本是2.6,但我使用2.7。如何执行构建和安装命令,以便它们构建并安装到Python2.7站点包?有一个我不知道的简单命令吗?我一直在这里搜索:Python文档中的http://docs.python.org/2/install/,但是我没有看到任何关于.sh脚本的内容?

2 个答案:

答案 0 :(得分:0)

您应该在为应用环境创建的virtualenv中运行脚本。这将创建一个隔离的环境,该环境使用您创建virtualenv的Python解释器,但具有自己的库集。

# create the virtualenv folder: M2Crypto-venv
python2.7 virtualenv.py --distribute M2Crypto-venv

# activate the virtualenv, changing environment variables to use its Python interpreter
. M2Crypto-venv/bin/activate

# see how the current python has changed
which python        # should be M2Crypto-venv/bin/python
python --version    # should be 2.7

# after activating, run your install scripts

如果您正在使用mod_wsgi或类似内容来提供内容,那么在执行任何其他操作之前,您需要修改WSGI文件以激活virtualenv(改编自mod_wsgi instructions):

import os.path

virtualenv_path = '/path/to/M2Crypto-venv'
activate_this = os.path.join(virtualenv_path, 'bin/activate_this.py')
execfile(activate_this, dict(__file__ = activate_this))

# rest of the WSGI file...

答案 1 :(得分:0)

这是一个难以置信的难以接受的答案,但是我所在的Webfaction的支持团队在协助我方面非常出色。直接来自我得到的支持:

首先构建swig,

wget http://prdownloads.sourceforge.net/swig/swig-2.0.8.tar.gz
tar -xf swig-2.0.8.tar.gz 
cd swig-2.0.8
./configure --prefix=$HOME
make
make install

比得到m2crypto,

svn checkout http://svn.osafoundation.org/m2crypto/tags/0.21/ m2crypto-0.21
cd m2crypto-0.21/

从此

编辑fedora_setup.sh
SWIG_FEATURES=-cpperraswarn python setup.py $*

到此,

SWIG_FEATURES=-cpperraswarn python2.7 setup.py $*

然后构建,然后安装,

./fedora_setup.sh build
./fedora_setup.sh install --prefix=$HOME

[me@web342 lib]$ python2.7
Python 2.7.5 (default, May 16 2013, 20:16:09) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import M2Crypto
>>> print M2Crypto
<module 'M2Crypto' from '/home/me/lib/python2.7/site-packages/M2Crypto-0.21-py2.7-linux-x86_64.egg/M2Crypto/__init__.pyc'>

显然,在整个过程中替换自己的细节。希望这有助于下一个尝试使用fedora_setup将M2Crytpo安装到非默认python版本的人。