是否可以在 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
完整指南 pyenv
如果未安装 pyenv,则使用 pyenv-installer:
要使用任何自定义 python 版本,例如
3.5.6
使用以下命令:Full guide with pyenv
If pyenv is not installed then install it with pyenv-installer:
To use any custom python version, e.g.
3.5.6
use the following:现在,我发现获得更新版本的 Python 的最简单方法是通过 conda 将其安装到 conda 环境中。
安装 conda(您可能需要一个 virtualenv)
在 conda 环境中安装新的 Python 版本
我在这里添加这个答案,因为不需要手动下载。
conda
会为你做到这一点。现在为您想要的 Python 版本创建一个环境。在此示例中,我将使用
3.5.2
,因为它是撰写本文时的最新版本(2016 年 8 月)。将为 conda 创建一个安装软件包的环境
要激活此环境(我假设 linux 否则请检查 conda 文档):
现在通过 pip 或 conda 在环境中安装您需要的内容(conda 具有更好的二进制包支持)。
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)
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).Will create a environment for conda to install packages
To activate this environment(I'm assuming linux otherwise check the conda docs):
Now install what you need either via pip or conda in the environemnt(conda has better binary package support).
通常的方法是下载源代码并在本地构建和安装(但不是直接在 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.
此过程将 Python2.7 安装在任何位置,并消除
env
文件夹(由 virtualenv 管理)内的任何绝对路径引用。甚至 virtualenv 也没有绝对安装。因此,理论上,您可以将顶级目录放入 tarball 中,在未安装 Python(或任何依赖项)的计算机上分发并运行 tarball 中配置的任何内容。
有任何问题请联系我。这只是我正在进行的一个更大项目的一部分。现在,对于放置...
设置环境文件夹。
获取 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
目录中。如果您第二次、第三次、第 N 次执行此操作,则make clean
是可选的...<预><代码>$ tar -xzvf Python-2.7.3.tgz
$ cd Python-2.7.3
$ 清理干净
$ ./configure --prefix=/path/to/pyenv
$ 制作 &&进行安装
$ cd ../../
$ls
依赖环境 pyenv
创建您的 virtualenv
在
env/include/
中修复 python2.7 的符号链接
<前><代码>$ 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
[...重复直到所有符号链接都是相对的...]
$ cd ../../../
测试
<前><代码>$ python --版本
Python 2.7.1
$ 源环境/bin/activate
(环境)
$ python --版本
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.
Get Python-2.7.3, and virtualenv without any form of root OS installation.
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...Create your virtualenv
Fix the symlink to python2.7 within
env/include/
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.
Test
Aloha.
我正在使用 virtualenvwrapper 并且不想修改 $PATH,方法如下:
I'm using virtualenvwrapper and don't want to modify $PATH, here's how:
虽然这个问题具体描述了安装 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
已安装。使用 apt-get 或 yum 安装它们,然后重新构建 Python(如果已经安装,则无需删除任何内容,请在顶部执行此操作)。
get-pip
抱怨这一点,您可以通过在 py shell 上运行import ssl
来检查。最后,不要忘记声明
.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 oropenssl-dev
for CentOS is installed. Install them withapt-get
oryum
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 runningimport ssl
on a py shell.Last, don't forget to declare
.local/bin
and local python to path, check withwhich pip
andwhich python
.您可以使用 pyenv。
有很多不同的版本anaconda、jython、pypy等等...
https://github.com/ yyuu/pyenv
安装就像
pyenv install 3.2.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
...
否,但您可以在
$HOME< 下安装独立的 Python 版本(例如 ActivePython) /代码> 目录。
这种方法是最快的,并且不需要您自己编译 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)
我还没有找到合适的答案,所以这是我的看法,它建立在@toszter答案的基础上,但是不使用系统Python(你可能知道,安装setuptools和virtualenv并不总是好主意在处理许多Python配置时的系统级别):
这里解决先有鸡还是先有蛋问题的技巧是先在没有setuptools的情况下创建virtualenv,否则它会失败(无法找到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):
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.
首先感谢DTing的精彩回答。这几乎是完美的。
对于那些在共享主机中无法访问 GCC 的人来说,请使用 ActivePython 而不是像 Scott Stafford 提到的普通 Python。以下是相关命令。
它会询问你 python 目录的路径。 在 DTing 的答案中输入
“Just replacement above”作为“步骤 1”,然后继续执行“步骤 2”。请注意,ActivePython 包 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.
It will ask you path to python directory. Enter
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.
virtualenv --python=".localpython/bin/python2.7" 环境
virtualenv --python=".localpython/bin/python2.7" env
以下是 virtualenv 的选项
1) 您想要做的是将 python 安装到您也可以写入的目录。
您可以按照说明进行操作 < a href="https://stackoverflow.com/questions/622744/unable-to-install-python-without-sudo-access">
此处
。对于Python 2.7.1
Python 源码
2) 安装 virtualenv
virtualenv source
3) 使用本地 python 创建 virtualenv
virtualenv docs
4) 激活环境
5) 检查
Here are the options for virtualenv
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
2) Install virtualenv
virtualenv source
3) Create a virtualenv using your local python
virtualenv docs
4) Activate the environment
5) Check
先决条件:
sudo easy_install virtualenv
sudo pip install virtualenvwrapper
使用 Python2.6 安装 virtualenv:
您可以手动下载、构建另一个版本的 Python 并将其安装到
/usr/local
或其他位置。如果它是
/usr/local
之外的其他位置,请将其添加到您的 PATH 中。重新加载 shell 以获取更新的 PATH。
从现在起,您应该能够从 shell
python2.5
和python2.6
调用以下 2 个 Python 二进制文件使用以下命令创建一个新的 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
andpython2.6
Create a new instance of virtualenv with
python2.6
:mkvirtualenv --python=python2.6 yournewenv