Programming

Virtualenv에 다른 버전의 Python을 설치할 수 있습니까?

procodes 2020. 6. 15. 22:17
반응형

Virtualenv에 다른 버전의 Python을 설치할 수 있습니까?


Python 2.4가 설치된 웹 호스팅에 공유 계정이 있지만 코드가 2.4와 호환되지 않습니다. Python 2.6을 Virtualenv에 직접 설치할 수 있습니까?

참고 : 공유 서버에 설치할 권한이 없습니다.


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) 당신이하고 싶은 일은 파이썬도 당신이 쓸 수있는 디렉토리에 설치하는 것입니다.

지침을 따를 수 있습니다 here.

파이썬 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) 로컬 파이썬을 사용하여 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.
>>> 

전제 조건 :

  1. sudo easy_install virtualenv
  2. sudo pip install virtualenvwrapper

Python2.6으로 virtualenv 설치 :

  1. 다른 버전의 Python /usr/local또는 다른 위치에 수동으로 다운로드하여 빌드하고 설치할 수 있습니다 .
  2. 이 아닌 다른 위치 인 경우 /usr/localPATH에 추가하십시오.
  3. 셸을 다시로드하여 업데이트 된 PATH를 선택하십시오.
  4. 이 시점에서, 당신은 당신의 쉘에서 다음이 파이썬 바이너리를 호출 할 수 있어야 python2.5하고python2.6
  5. 다음을 사용하여 virtualenv의 새 인스턴스를 작성하십시오 python2.6.

    mkvirtualenv --python=python2.6 yournewenv


이 절차는 Python2.7을 어디에나 설치 하고env 폴더 내에서 절대 경로 참조를 제거 합니다 (virtuenv에서 관리) . virtualenv조차도 절대적으로 설치되지는 않습니다.

따라서 이론적으로 최상위 디렉토리를 타르볼에 드롭하고, 타르볼 내에 구성된 모든 것을 파이썬 (또는 의존성)이 설치 되지 않은 머신에서 배포하고 실행할 수 있습니다 .

질문이 있으면 저에게 연락하십시오. 이것은 내가 진행중인 진행중인 대규모 프로젝트의 일부입니다. 이제 드롭을 위해 ...

  1. 환경 폴더를 설정하십시오.

    $ mkdir env
    $ mkdir pyenv
    $ mkdir dep
    
  2. 어떤 형태의 루트 OS 설치 없이 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. pyenvdir에 Python-2.7.3을 추출하여 설치하십시오 . make clean두 번째, 세 번째, 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
    
  4. virtualenv 생성

    $ dep/virtualenv.py --python=/path/to/pyenv/bin/python --verbose env
    
  5. python2.7에 symlink를 수정하십시오. env/include/

    $ ls -l env/include/
    $ cd !$
    $ rm python2.7
    $ ln -s ../../pyenv/include/python2.7 python2.7
    $ cd ../../
    
  6. env에서 나머지 파이썬 심볼릭 링크를 수정하십시오. 위와 같이 심볼릭 링크 된 디렉토리를 삭제하고 다시 작성해야합니다. 또한 내부 심볼릭 링크 작성을 강제하는 구문이 있습니다.

    $ 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. 테스트

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

알로하


virtualenvwrapper를 사용하고 있으며 $ PATH를 수정하고 싶지 않습니다. 방법은 다음과 같습니다.

$ which python3
/usr/local/bin/python3

$ mkvirtualenv --python=/usr/local/bin/python3 env_name

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.


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>

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

...


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.


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)


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.


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.

참고URL : https://stackoverflow.com/questions/5506110/is-it-possible-to-install-another-version-of-python-to-virtualenv

반응형