是否可以在 Virtualenv 中安装另一个版本的 Python?

发布于 2024-10-29 07:29:32 字数 112 浏览 5 评论 0原文

我在安装了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

_失温 2024-11-05 07:29:33

完整指南 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

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
cd YOUR_PROJECT_PATH
pyenv local NAME_OF_YOUR_ENV
嘦怹 2024-11-05 07:29:33

现在,我发现获得更新版本的 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 文档):

source activate py35

现在通过 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>
沒落の蓅哖 2024-11-05 07:29:33

通常的方法是下载源代码并在本地构建和安装(但不是直接在 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.

于我来说 2024-11-05 07:29:33

此过程将 Python2.7 安装在任何位置,并消除 env 文件夹(由 virtualenv 管理)内的任何绝对路径引用。甚至 virtualenv 也没有绝对安装。

因此,理论上,您可以将顶级目录放入 tarball 中,在未安装 Python(或任何依赖项)的计算机上分发并运行 tarball 中配置的任何内容。

有任何问题请联系我。这只是我正在进行的一个更大项目的一部分。现在,对于放置...

  1. 设置环境文件夹。

    $ mkdir 环境
    $ mkdir pyenv
    $ mkdir 依赖
    
  2. 获取 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

  3. 解压 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

  4. 创建您的 virtualenv

    $ dep/virtualenv.py --python=/path/to/pyenv/bin/python --verbose env
    
  5. env/include/

    中修复 python2.7 的符号链接

    <前><代码>$ ls -l env/include/
    $ cd !$
    $rm python2.7
    $ ln -s ../../pyenv/include/python2.7 python2.7
    $ cd ../../

  6. 修复 env 中剩余的 python 符号链接。您必须删除符号链接的目录并重新创建它们,如上所述。另外,这是强制就地符号链接创建的语法。

    <前><代码>$ ls -l env/lib/python2.7/
    $ cd !$
    $ ln -sf ../../../pyenv/lib/python2.7/UserDict.py UserDict.py
    [...重复直到所有符号链接都是相对的...]
    $ cd ../../../

  7. 测试

    <前><代码>$ 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...

  1. Set up environment folders.

    $ mkdir env
    $ mkdir pyenv
    $ mkdir dep
    
  2. 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
    
  3. 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
    
  4. Create your virtualenv

    $ dep/virtualenv.py --python=/path/to/pyenv/bin/python --verbose env
    
  5. 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 ../../
    
  6. 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 ../../../
    
  7. Test

    $ python --version
    Python 2.7.1
    $ source env/bin/activate
    (env)
    $ python --version
    Python 2.7.3
    

Aloha.

这个俗人 2024-11-05 07:29:33

我正在使用 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
断桥再见 2024-11-05 07:29:33

虽然这个问题具体描述了安装 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 pipwhich 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.

青萝楚歌 2024-11-05 07:29:33

您可以使用 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

...

剩一世无双 2024-11-05 07:29:33

否,但您可以在 $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)

强辩 2024-11-05 07:29:33

我还没有找到合适的答案,所以这是我的看法,它建立在@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"

这里解决先有鸡还是先有蛋问题的技巧是先在没有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):

#!/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.

流绪微梦 2024-11-05 07:29:33

首先感谢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 目录的路径。 在 DTing 的答案中输入

../../.localpython

“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.

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.

你好,陌生人 2024-11-05 07:29:33

virtualenv --python=".localpython/bin/python2.7" 环境

virtualenv --python=".localpython/bin/python2.7" env

同尘 2024-11-05 07:29:32

以下是 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 安装到您也可以写入的目录。

您可以按照说明进行操作 < a href="https://stackoverflow.com/questions/622744/unable-to-install-python-without-sudo-access">此处

对于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 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.
>>> 
人海汹涌 2024-11-05 07:29:32

先决条件:

  1. sudo easy_install virtualenv
  2. sudo pip install virtualenvwrapper

使用 Python2.6 安装 virtualenv:

  1. 您可以手动下载、构建另一个版本的 Python 并将其安装到 /usr/local 或其他位置。

  2. 如果它是 /usr/local 之外的其他位置,请将其添加到您的 PATH 中。

  3. 重新加载 shell 以获取更新的 PATH。

  4. 从现在起,您应该能够从 shell python2.5python2.6 调用以下 2 个 Python 二进制文件

  5. 使用以下命令创建一个新的 virtualenv 实例python2.6:

    mkvirtualenv --python=python2.6 yournewenv

Pre-requisites:

  1. sudo easy_install virtualenv
  2. sudo pip install virtualenvwrapper

Installing virtualenv with Python2.6:

  1. You could manually download, build and install another version of Python to /usr/local or another location.

  2. If it's another location other than /usr/local, add it to your PATH.

  3. Reload your shell to pick up the updated PATH.

  4. From this point on, you should be able to call the following 2 python binaries from your shell python2.5 and python2.6

  5. Create a new instance of virtualenv with python2.6:

    mkvirtualenv --python=python2.6 yournewenv

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文