Use different Python version with virtualenv

后端 未结 30 3327
自闭症患者
自闭症患者 2020-11-21 05:03

I have a Debian system currently running with python 2.5.4. I got virtualenv properly installed, everything is working fine. Is there a possibility that I can use a virtuale

相关标签:
30条回答
  • 2020-11-21 05:25

    There is an easier way,

    virtualenv venv --python=python2.7
    

    Thanks to a comment, this only works if you have python2.7 installed at the system level (e.g. /usr/bin/python2.7).

    Otherwise, if you are using homebrew you can use the path to give you what you want.

    virtualenv venv --python=/usr/local/bin/python
    

    You can find the path to your python installation with

    which python
    

    This will also work with python 3.

    which python3
    >> /usr/local/bin/python3
    virtualenv venv --python=/usr/local/bin/python3
    

    Ultimately condensing to:

    virtualenv venv -p `which python`
    virtualenv venv -p `which python3`
    
    0 讨论(0)
  • 2020-11-21 05:25

    The -p approach works well, but you do have to remember to use it every time. If your goal is to switch to a newer version of Python generally, that's a pain and can also lead to mistakes.

    Your other option is to set an environment variable that does the same thing as -p. Set this via your ~/.bashrc file or wherever you manage environment variables for your login sessions:

    export VIRTUALENV_PYTHON=/path/to/desired/version
    

    Then virtualenv will use that any time you don't specify -p on the command line.

    0 讨论(0)
  • 2020-11-21 05:26

    Just use the --python (or short -p) option when creating your virtualenv instance to specify the Python executable you want to use, e.g.:

    virtualenv --python=/usr/bin/python2.6 <path/to/new/virtualenv/>
    

    N.B. For Python 3.3 or later, refer to The Aelfinn's answer below.

    0 讨论(0)
  • 2020-11-21 05:26

    It worked for me

    sudo apt-get install python3-minimal
    
    virtualenv --no-site-packages --distribute -p /usr/bin/python3 ~/.virtualenvs/py3
    
    0 讨论(0)
  • 2020-11-21 05:27

    These are the steps you can follow when you are on a shared hosting environment and need to install & compile Python from source and then create venv from your Python version. For Python 2.7.9. you would do something along these lines:

    mkdir ~/src
    wget http://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
    tar -zxvf Python-2.7.9.tgz
    cd Python-2.7.9
    mkdir ~/.localpython
    ./configure --prefix=$HOME/.localpython
    make
    make install
    

    virtual env

    cd ~/src
    wget https://pypi.python.org/packages/5c/79/5dae7494b9f5ed061cff9a8ab8d6e1f02db352f3facf907d9eb614fb80e9/virtualenv-15.0.2.tar.gz#md5=0ed59863994daf1292827ffdbba80a63
    tar -zxvf virtualenv-15.0.2.tar.gz
    cd virtualenv-15.0.2/
    ~/.localpython/bin/python setup.py install
    virtualenv ve -p $HOME/.localpython/bin/python2.7
    source ve/bin/activate   
    

    Naturally, this can be applicable to any situation where you want to replicate the exact environment you work and deploy on.

    0 讨论(0)
  • 2020-11-21 05:28

    Under Windows for me this works:

    virtualenv --python=c:\Python25\python.exe envname
    

    without the python.exe I got WindowsError: [Error 5] Access is denied I have Python2.7.1 installed with virtualenv 1.6.1, and I wanted python 2.5.2.

    0 讨论(0)
提交回复
热议问题