可以在Virtualenv中安装其他版本的Python吗?
我在一个共享的网络主机上有个账户,那里安装的是Python 2.4,但我的代码不支持这个版本。请问可以直接在Virtualenv里安装Python 2.6吗?
注意:我没有权限在这个共享服务器上安装Python。
13 个回答
5
完整指南,使用 pyenv
如果还没有安装 pyenv,可以通过 pyenv-installer 来安装它:
$ curl https://pyenv.run | bash
要使用任何自定义的 Python 版本,比如 3.5.6
,可以使用以下命令:
pyenv install 3.5.6
pyenv virtualenv 3.5.6 NAME_OF_YOUR_ENV
cd YOUR_PROJECT_PATH
pyenv local NAME_OF_YOUR_ENV
20
前提条件:
sudo easy_install virtualenv
(安装一个叫做virtualenv的工具)sudo pip install virtualenvwrapper
(安装一个叫做virtualenvwrapper的工具)
使用Python2.6安装virtualenv:
你可以手动下载、构建并安装另一个版本的Python到
/usr/local
或者其他地方。如果你选择的地方不是
/usr/local
,记得把它添加到你的PATH中。重新加载你的命令行,这样就能使用更新后的PATH了。
从现在开始,你应该能在命令行中调用这两个Python版本:
python2.5
和python2.6
。用
python2.6
创建一个新的virtualenv实例:mkvirtualenv --python=python2.6 yournewenv
264
这里是关于virtualenv的选项
$ virtualenv
You must provide a DEST_DIR
Usage: virtualenv [OPTIONS] DEST_DIR
Options:
--version show program's version number and exit.
-h, --help show this help message and exit.
-v, --verbose Increase verbosity.
-q, --quiet Decrease verbosity.
-p PYTHON_EXE, --python=PYTHON_EXE
The Python interpreter to use, e.g.,
--python=python2.5 will use the python2.5 interpreter
to create the new environment. The default is the
interpreter that virtualenv was installed with
(/usr/bin/python)
--clear Clear out the non-root install and start from scratch
--no-site-packages Don't give access to the global site-packages dir to
the virtual environment
--unzip-setuptools Unzip Setuptools or Distribute when installing it
--relocatable Make an EXISTING virtualenv environment relocatable.
This fixes up scripts and makes all .pth files
relative
--distribute Use Distribute instead of Setuptools. Set environ
variable VIRTUALENV_USE_DISTRIBUTE to make it the
default
--prompt==PROMPT Provides an alternative prompt prefix for this
environment
1) 首先,你需要把Python安装到一个你有权限写入的目录。
你可以按照这里
的说明进行操作。
对于Python 2.7.1
Python源代码
mkdir ~/src
mkdir ~/.localpython
cd ~/src
wget http://www.python.org/ftp/python/2.7.1/Python-2.7.1.tgz
tar -zxvf Python-2.7.1.tgz
cd Python-2.7.1
make clean
./configure --prefix=/home/${USER}/.localpython
make
make install
2) 安装virtualenv
virtualenv源代码
cd ~/src
wget http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.5.2.tar.gz#md5=fbcefbd8520bb64bc24a560c6019a73c
tar -zxvf virtualenv-1.5.2.tar.gz
cd virtualenv-1.5.2/
~/.localpython/bin/python setup.py install
3) 使用你本地的Python创建一个virtualenv
virtualenv文档
mkdir /home/${USER}/virtualenvs
cd /home/${USER}/virtualenvs
~/.localpython/bin/virtualenv py2.7 --python=/home/${USER}/.localpython/bin/python2.7
4) 激活这个环境
cd ~/virtualenvs/py2.7/bin
source ./activate
5) 检查一下
(py2.7)$ python
Python 2.7.1 (r271:86832, Mar 31 2011, 15:31:37)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
(py2.7)$ deactivate
$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>