使用.sh安装python模块到非默认版本的python
问题描述:
我有一个类似于这篇文章的问题:Install python module to non default version of python on Mac,所以我意识到这些解决方案,但它们不适用于我。使用.sh安装python模块到非默认版本的python
我在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脚本的任何内容?
答
这是一个非常难以接受的答案,但我所在的Webfaction的支持团队在帮助我时非常出色。直接从支持我得到:
首先构建痛饮,
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,从这个
SWIG_FEATURES=-cpperraswarn python setup.py $*
这个
svn checkout http://svn.osafoundation.org/m2crypto/tags/0.21/ m2crypto-0.21
cd m2crypto-0.21/
编辑fedora_setup.sh,
SWIG_FEATURES=-cpperraswarn python2.7 setup.py $*
然后建造,然后安装,
./fedora_setup.sh build
./fedora_setup.sh install --prefix=$HOME
[[email protected] 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版本的人。
答
您应该在为应用程序环境创建的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文件做其他事情(改编自mod_wsgi instructions)之前激活的virtualenv:
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...
我的项目的其余部分能够访问安装在virtualenv中的资源吗?如果Django无法与M2Crypto交谈,那么它会打败我的目的。我能将virtualenv添加到Python Path吗? – fildred13
您应该在virtualenv中安装Django和其他项目组件。激活之后,virtualenv'pip'可以为你做到这一点。 – SteelPangolin
如果你使用mod_wsgi或类似的东西来提供内容,你需要修改你的WSGI文件来在做任何事情之前激活virtualenv(配置添加到原始文章) – SteelPangolin