问题:是否可以在Virtualenv中安装另一个版本的Python?
我在安装了Python 2.4的虚拟主机中有一个共享帐户,但是我的代码与2.4不兼容。是否可以将Python 2.6直接安装到Virtualenv?
注意:我无权在共享服务器上安装它。
I have a shared account in a web-hosting that has Python 2.4 installed, but my code is not compatible with 2.4. Is it possible to install Python 2.6 directly to Virtualenv?
Note: I don´t have permission to install it in the shared server.
回答 0
这是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安装到您也可以编写的目录中。
您可以按照说明进行操作here
。
对于Python 2.7.1
Python source
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 source
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 docs
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.
>>>
Here are the options for 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) What you want to do is install python to a directory that you are able to write too.
You can follow the instructions here
.
For Python 2.7.1
Python source
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) Install virtualenv
virtualenv source
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) Create a virtualenv using your local python
virtualenv docs
mkdir /home/${USER}/virtualenvs
cd /home/${USER}/virtualenvs
~/.localpython/bin/virtualenv py2.7 --python=/home/${USER}/.localpython/bin/python2.7
4) Activate the environment
cd ~/virtualenvs/py2.7/bin
source ./activate
5) Check
(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.
>>>
回答 1
先决条件:
sudo easy_install virtualenv
sudo pip install virtualenvwrapper
使用Python2.6安装virtualenv:
您可以手动下载,构建另一个Python版本并将其安装到/usr/local
另一个位置。
如果除以外的其他位置/usr/local
,请将其添加到PATH中。
重新加载您的外壳以选择更新的PATH。
从这一点开始,您应该能够从shell中调用以下2个python二进制文件python2.5
,python2.6
使用以下命令创建一个virtualenv的新实例python2.6
:
mkvirtualenv --python=python2.6 yournewenv
Pre-requisites:
sudo easy_install virtualenv
sudo pip install virtualenvwrapper
Installing virtualenv with Python2.6:
You could manually download, build and install another version of Python to /usr/local
or another location.
If it’s another location other than /usr/local
, add it to your PATH.
Reload your shell to pick up the updated PATH.
From this point on, you should be able to call the following 2 python binaries from your shell python2.5
and python2.6
Create a new instance of virtualenv with python2.6
:
mkvirtualenv --python=python2.6 yournewenv
回答 2
此过程安装Python2.7 任何地方,消除了任何绝对路径引用您的内部env
文件夹(由virtualenv中管理)。甚至没有完全安装virtualenv。
因此,从理论上讲,您可以将顶级目录放到tarball中,在没有安装Python (或任何依赖项)的机器上分发并运行tarball中配置的任何内容。
如有任何问题请联系我。这只是我正在进行的一个较大的项目的一部分。现在,为下降…
设置环境文件夹。
$ mkdir env
$ mkdir pyenv
$ mkdir dep
无需任何形式的root操作系统安装即可获取Python-2.7.3和virtualenv 。
$ cd dep
$ wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz
$ wget https://raw.github.com/pypa/virtualenv/master/virtualenv.py
解压缩并将Python-2.7.3安装到该pyenv
目录中。make clean
如果您是第2、3,N次执行此操作,则是可选的…
$ tar -xzvf Python-2.7.3.tgz
$ cd Python-2.7.3
$ make clean
$ ./configure --prefix=/path/to/pyenv
$ make && make install
$ cd ../../
$ ls
dep env pyenv
创建您的virtualenv
$ dep/virtualenv.py --python=/path/to/pyenv/bin/python --verbose env
修复符号链接到python2.7 env/include/
$ ls -l env/include/
$ cd !$
$ rm python2.7
$ ln -s ../../pyenv/include/python2.7 python2.7
$ cd ../../
修复env中剩余的python符号链接。如上所述,您必须删除符号链接的目录并重新创建它们。另外,这是强制就地符号链接创建的语法。
$ ls -l env/lib/python2.7/
$ cd !$
$ ln -sf ../../../pyenv/lib/python2.7/UserDict.py UserDict.py
[...repeat until all symbolic links are relative...]
$ cd ../../../
测试
$ python --version
Python 2.7.1
$ source env/bin/activate
(env)
$ python --version
Python 2.7.3
阿罗哈
This procedure installs Python2.7 anywhere and eliminates any absolute path references within your env
folder (managed by virtualenv). Even virtualenv isn’t installed absolutely.
Thus, theoretically, you can drop the top level directory into a tarball, distribute, and run anything configured within the tarball on a machine that doesn’t have Python (or any dependencies) installed.
Contact me with any questions. This is just part of an ongoing, larger project I am engineering. Now, for the drop…
Set up environment folders.
$ mkdir env
$ mkdir pyenv
$ mkdir dep
Get Python-2.7.3, and virtualenv without any form of root OS installation.
$ cd dep
$ wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz
$ wget https://raw.github.com/pypa/virtualenv/master/virtualenv.py
Extract and install Python-2.7.3 into the pyenv
dir. make clean
is optional if you are doing this a 2nd, 3rd, Nth time…
$ tar -xzvf Python-2.7.3.tgz
$ cd Python-2.7.3
$ make clean
$ ./configure --prefix=/path/to/pyenv
$ make && make install
$ cd ../../
$ ls
dep env pyenv
Create your virtualenv
$ dep/virtualenv.py --python=/path/to/pyenv/bin/python --verbose env
Fix the symlink to python2.7 within env/include/
$ ls -l env/include/
$ cd !$
$ rm python2.7
$ ln -s ../../pyenv/include/python2.7 python2.7
$ cd ../../
Fix the remaining python symlinks in env. You’ll have to delete the symbolically linked directories and recreate them, as above. Also, here’s the syntax to force in-place symbolic link creation.
$ ls -l env/lib/python2.7/
$ cd !$
$ ln -sf ../../../pyenv/lib/python2.7/UserDict.py UserDict.py
[...repeat until all symbolic links are relative...]
$ cd ../../../
Test
$ python --version
Python 2.7.1
$ source env/bin/activate
(env)
$ python --version
Python 2.7.3
Aloha.
回答 3
我正在使用virtualenvwrapper,并且不想修改$ PATH,方法如下:
$ which python3
/usr/local/bin/python3
$ mkvirtualenv --python=/usr/local/bin/python3 env_name
I’m using virtualenvwrapper and don’t want to modify $PATH, here’s how:
$ which python3
/usr/local/bin/python3
$ mkvirtualenv --python=/usr/local/bin/python3 env_name
回答 4
通常的方法是下载源代码并在本地构建和安装(但不能直接在virtualenv中安装),然后使用该本地Python安装程序创建新的virtualenv。在某些系统上,有可能下载并安装预构建的python,而不是从源代码构建。
The usual approach is to download the source and build and install locally (but not directly in virtualenv), and then create a new virtualenv using that local Python install. On some systems, it may be possible to download and install a prebuilt python, rather than building from source.
回答 5
如今,我发现拥有更新版本的Python的最简单方法是通过conda将其安装到conda环境中。
安装conda(为此可能需要virtualenv)
pip install conda
在conda环境中安装新的Python版本
我在这里添加此答案,因为不需要手动下载。conda
将为您做到这一点。
现在为所需的Python版本创建一个环境。在此示例中,我将使用3.5.2
,因为它是撰写本文时(2016年8月)的最新版本。
conda create -n py35 python=3.5.2
将为conda创建安装包的环境
要激活此环境(我假设linux否则请检查conda docs):
source activate py35
现在,通过environemnt中的pip或conda安装所需的内容(conda具有更好的二进制软件包支持)。
conda install <package_name>
Now a days, the easiest way I found to have a more updated version of Python is to install it via conda into a conda environment.
Install conda(you may need a virtualenv for this)
pip install conda
Installing a new Python version inside a conda environent
I’m adding this answer here because no manual download is needed. conda
will do that for you.
Now create an environment for the Python version you want. In this example I will use 3.5.2
, because it it the latest version at this time of writing (Aug 2016).
conda create -n py35 python=3.5.2
Will create a environment for conda to install packages
To activate this environment(I’m assuming linux otherwise check the conda docs):
source activate py35
Now install what you need either via pip or conda in the environemnt(conda has better binary package support).
conda install <package_name>
回答 6
您可以使用pyenv。
有很多不同版本的anaconda,jython,pypy等…
https://github.com/yyuu/pyenv
安装简单 pyenv install 3.2.6
pyenv install --list
Available versions:
2.1.3
2.2.3
2.3.7
2.4
2.4.1
2.4.2
2.4.3
2.4.4
2.4.5
2.4.6
2.5
2.5.1
2.5.2
2.5.3
2.5.4
2.5.5
2.5.6
2.6.6
…
You may use pyenv.
There are a lot of different versions anaconda, jython, pypy and so on…
https://github.com/yyuu/pyenv
Installation as simple as pyenv install 3.2.6
pyenv install --list
Available versions:
2.1.3
2.2.3
2.3.7
2.4
2.4.1
2.4.2
2.4.3
2.4.4
2.4.5
2.4.6
2.5
2.5.1
2.5.2
2.5.3
2.5.4
2.5.5
2.5.6
2.6.6
…
回答 7
尽管该问题专门描述了安装2.6,但我想在上面的出色回答中添加一些重要点,以防有人遇到此问题。作为记录,我的案例是我试图在ubuntu 10.04机器上安装2.7。
首先,我对这里所有答案中所述方法的追求是,从Deadsnake的ppa安装Python 完全失败。因此,构建本地Python是必经之路。
尝试过之后,我认为依靠默认的pip安装(带有sudo apt-get install pip
)就足够了。不幸的是,这是错误的。原来,我的所有烦恼都是空缺,最终无法创建virtualenv。
因此,我强烈建议本地安装PIP用wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py && python get-pip.py --user
。这个相关的问题给了我这个提示。
现在,如果这不起作用,请确保已安装libssl-dev
Ubuntu或openssl-dev
CentOS。使用apt-get
或进行安装yum
,然后重新构建Python(如果已安装,则无需删除任何内容,请先删除)。get-pip
抱怨,您可以通过import ssl
在py shell上运行进行检查。
最后,不要忘了声明.local/bin
和本地python到路径,并使用which pip
和进行检查which python
。
Although the question specifically describes installing 2.6, I would like to add some importants points to the excellent answers above in case someone comes across this. For the record, my case was that I was trying to install 2.7 on an ubuntu 10.04 box.
First, my motivation towards the methods described in all the answers here is that installing Python from deadsnake’s ppa’s has been a total failure. So building a local Python is the way to go.
Having tried so, I thought relying to the default installation of pip (with sudo apt-get install pip
) would be adequate. This unfortunately is wrong. It turned out that I was getting all shorts of nasty issues and eventually not being able to create a virtualenv.
Therefore, I highly recommend to install pip locally with wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py && python get-pip.py --user
. This related question gave me this hint.
Now if this doesn’t work, make sure that libssl-dev
for Ubuntu or openssl-dev
for CentOS is installed. Install them with apt-get
or yum
and then re-build Python (no need to remove anything if already installed, do so on top). get-pip
complains about that, you can check so by running import ssl
on a py shell.
Last, don’t forget to declare .local/bin
and local python to path, check with which pip
and which python
.
回答 8
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
pyenv activate name_of_your_env
Full guide with pyenv
If pyenv is not installed then install it with pyenv-installer:
$ curl https://pyenv.run | bash
To use any custom python version, e.g. 3.5.6
use the following:
pyenv install 3.5.6
pyenv virtualenv 3.5.6 name_of_your_env
pyenv activate name_of_your_env
回答 9
否,但是您可以在目录下安装隔离的Python构建(例如ActivePython)$HOME
。
这种方法是最快的,并且不需要您自己编译Python。
(此外,您还可以使用ActiveState的二进制包管理器)
No, but you can install an isolated Python build (such as ActivePython) under your $HOME
directory.
This approach is the fastest, and doesn’t require you to compile Python yourself.
(as a bonus, you also get to use ActiveState’s binary package manager)
回答 10
我没有找到合适的答案,所以我来看看,它基于@toszter答案,但不使用系统Python(而且您可能知道,在处理许多问题时,在系统级别安装setuptools和virtualenv并不总是一个好主意Python配置):
#!/bin/sh
mkdir python_ve
cd python_ve
MYROOT=`pwd`
mkdir env pyenv dep
cd ${MYROOT}/dep
wget https://pypi.python.org/packages/source/s/setuptools/setuptools-15.2.tar.gz#md5=a9028a9794fc7ae02320d32e2d7e12ee
wget https://raw.github.com/pypa/virtualenv/master/virtualenv.py
wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tar.xz
xz -d Python-2.7.9.tar.xz
cd ${MYROOT}/pyenv
tar xf ../dep/Python-2.7.9.tar
cd Python-2.7.9
./configure --prefix=${MYROOT}/pyenv && make -j 4 && make install
cd ${MYROOT}/pyenv
tar xzf ../dep/setuptools-15.2.tar.gz
cd ${MYROOT}
pyenv/bin/python dep/virtualenv.py --no-setuptools --python=${MYROOT}/pyenv/bin/python --verbose env
env/bin/python pyenv/setuptools-15.2/setup.py install
env/bin/easy_install pip
echo "virtualenv in ${MYROOT}/env"
打破鸡蛋问题的技巧是首先使virtualenv不使用setuptools,因为否则它会失败(找不到pip)。可能可以直接安装pip / wheel,但是以某种方式easy_install是我想到的第一件事。同样,可以通过考虑具体版本来改进脚本。
注意 在脚本中使用xz。
I have not found suitable answer, so here goes my take, which builds upon @toszter answer, but does not use system Python (and you may know, it is not always good idea to install setuptools and virtualenv at system level when dealing with many Python configurations):
#!/bin/sh
mkdir python_ve
cd python_ve
MYROOT=`pwd`
mkdir env pyenv dep
cd ${MYROOT}/dep
wget https://pypi.python.org/packages/source/s/setuptools/setuptools-15.2.tar.gz#md5=a9028a9794fc7ae02320d32e2d7e12ee
wget https://raw.github.com/pypa/virtualenv/master/virtualenv.py
wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tar.xz
xz -d Python-2.7.9.tar.xz
cd ${MYROOT}/pyenv
tar xf ../dep/Python-2.7.9.tar
cd Python-2.7.9
./configure --prefix=${MYROOT}/pyenv && make -j 4 && make install
cd ${MYROOT}/pyenv
tar xzf ../dep/setuptools-15.2.tar.gz
cd ${MYROOT}
pyenv/bin/python dep/virtualenv.py --no-setuptools --python=${MYROOT}/pyenv/bin/python --verbose env
env/bin/python pyenv/setuptools-15.2/setup.py install
env/bin/easy_install pip
echo "virtualenv in ${MYROOT}/env"
The trick of breaking chicken-egg problem here is to make virtualenv without setuptools first, because it otherwise fails (pip can not be found). It may be possible to install pip / wheel directly, but somehow easy_install was the first thing which came to my mind. Also, the script can be improved by factoring out concrete versions.
NB. Using xz in the script.
回答 11
首先,谢谢DTing的出色回答。非常完美。
对于那些在共享主机中没有GCC访问权的人,请使用ActivePython而不是像Scott Stafford所述的普通python。这是命令。
wget http://downloads.activestate.com/ActivePython/releases/2.7.13.2713/ActivePython-2.7.13.2713-linux-x86_64-glibc-2.3.6-401785.tar.gz
tar -zxvf ActivePython-2.7.13.2713-linux-x86_64-glibc-2.3.6-401785.tar.gz
cd ActivePython-2.7.13.2713-linux-x86_64-glibc-2.3.6-401785
./install.sh
它将询问您到python目录的路径。输入
../../.localpython
只需将上面的内容替换为DTing回答中的步骤1,然后继续执行步骤2。请注意,ActivePython包URL可能会随新版本而更改。您随时可以从此处获取新的URL:http : //www.activestate.com/activepython/downloads
根据URL,您需要根据收到的文件更改tar和cd命令的名称。
First of all, Thank you DTing for awesome answer. It’s pretty much perfect.
For those who are suffering from not having GCC access in shared hosting, Go for ActivePython instead of normal python like Scott Stafford mentioned. Here are the commands for that.
wget http://downloads.activestate.com/ActivePython/releases/2.7.13.2713/ActivePython-2.7.13.2713-linux-x86_64-glibc-2.3.6-401785.tar.gz
tar -zxvf ActivePython-2.7.13.2713-linux-x86_64-glibc-2.3.6-401785.tar.gz
cd ActivePython-2.7.13.2713-linux-x86_64-glibc-2.3.6-401785
./install.sh
It will ask you path to python directory. Enter
../../.localpython
Just replace above as Step 1 in DTing’s answer and go ahead with Step 2 after that. Please note that ActivePython package URL may change with new release. You can always get new URL from here : http://www.activestate.com/activepython/downloads
Based on URL you need to change the name of tar and cd command based on file received.