Use different Python version with virtualenv

后端 未结 30 3261
自闭症患者
自闭症患者 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:19

    On the mac I use pyenv and virtualenvwrapper. I had to create a new virtualenv. You need homebrew which I'll assume you've installed if you're on a mac, but just for fun:

    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    
    
    brew install pyenv
    pyenv install 2.7.10
    pyenv global 2.7.10
    export PATH=/Users/{USERNAME}/.pyenv/versions/2.7.10/bin:$PATH
    mkvirtualenv -p ~/.pyenv/versions/2.7.10/bin/python  {virtual_env_name}
    

    I also froze my requirements first so i could simply reinstall in the new virtualenv with:

    pip install -r requirements.txt
    
    0 讨论(0)
  • 2020-11-21 05:21

    In windows subsystem for linux:

    1. Create environment for python3:

      virtualenv --python=/usr/bin/python3 env
      
    2. Activate it:

      source env/bin/activate
      
    0 讨论(0)
  • 2020-11-21 05:22

    Yes you just need to install the other version of python, and define the location of your other version of python in your command like :

    virtualenv /home/payroll/Documents/env -p /usr/bin/python3

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

    Since Python 3, the Python Docs suggest creating the virtual environment with the following command:

    python3 -m venv <myenvname>
    

    Please note that venv does not permit creating virtual environments with other versions of Python. For that, install and use the virtualenv package.


    Obsolete information

    The pyvenv script can be used to create a virtual environment

    pyvenv /path/to/new/virtual/environment
    

    but it has been deprecated since Python 3.6.

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

    This was a bug with virtualenv. Just upgrading your pip should be the fix.

    pip install --upgrade virtualenv

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

    As already mentioned in multiple answers, using virtualenv is a clean solution. However a small pitfall that everyone should be aware of is that if an alias for python is set in bash_aliases like:

    python=python3.6
    

    this alias will also be used inside the virtual environment. So in this scenario running python -V inside the virtual env will always output 3.6 regardless of what interpreter is used to create the environment:

    virtualenv venv --python=pythonX.X
    
    0 讨论(0)
提交回复
热议问题