Use different Python version with virtualenv

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

    Mac OSX 10.6.8 (Snow Leopard):

    1) When you do pip install virtualenv, the pip command is associated with one of your python versions, and virtualenv gets installed into that version of python. You can do

     $ which pip   
    

    to see what version of python that is. If you see something like:

     $ which pip
     /usr/local/bin/pip
    

    then do:

    $ ls -al /usr/local/bin/pip
    lrwxrwxr-x  1 root  admin  65 Apr 10  2015 /usr/local/bin/pip ->
    ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/pip
    

    You can see the python version in the output.

    By default, that will be the version of python that is used for any new environment you create. However, you can specify any version of python installed on your computer to use inside a new environment with the -p flag:

    $ virtualenv -p python3.2 my_env  
    Running virtualenv with interpreter /usr/local/bin/python3.2  
    New python executable in my_env/bin/python  
    Installing setuptools, pip...done.  
    

    virtualenv my_env will create a folder in the current directory which will contain the Python executable files, and a copy of the pip [command] which you can use to install other packages.

    http://docs.python-guide.org/en/latest/dev/virtualenvs/

    virtualenv just copies python from a location on your computer into the newly created my_env/bin/ directory.

    2) The system python is in /usr/bin, while the various python versions I installed were, by default, installed into:

     /usr/local/bin
    

    3) The various pythons I installed have names like python2.7 or python3.2, and I can use those names rather than full paths.

    ========VIRTUALENVWRAPPER=========

    1) I had some problems getting virtualenvwrapper to work. This is what I ended up putting in ~/.bash_profile:

    export WORKON_HOME=$HOME/.virtualenvs
    export PROJECT_HOME=$HOME/django_projects  #Not very important -- mkproject command uses this
    #Added the following based on: 
    #http://stackoverflow.com/questions/19665327/virtualenvwrapper-installation-snow-leopard-python
    export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python2.7 
    #source /usr/local/bin/virtualenvwrapper.sh
    source /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenvwrapper.sh
    

    2) The -p option works differently with virtualenvwrapper: I have to specify the full path to the python interpreter to be used in the new environment(when I do not want to use the default python version):

    $ mkvirtualenv -p /usr/local/bin/python3.2 my_env
    Running virtualenv with interpreter /usr/local/bin/python3
    New python executable in my_env/bin/python
    Installing setuptools, pip...done.
    Usage: source deactivate
    
    removes the 'bin' directory of the environment activated with 'source
    activate' from PATH. 
    

    Unlike virtualenv, virtualenvwrapper will create the environment at the location specified by the $WORKON_HOME environment variable. That keeps all your environments in one place.

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

    On windows:

    py -3.4x32 -m venv venv34
    

    or

    py -2.6.2 -m venv venv26
    

    This uses the py launcher which will find the right python executable for you (assuming you have it installed).

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

    Yes, the above answers are correct and works fine on Unix based systems like Linux & MAC OS X.

    I tried to create virtualenv for Python2 & Python3 with the following commands.

    Here I have used venv2 & venv3 as their names for Python2 & Python3 respectively.

    Python2 »

    MacBook-Pro-2:~ admin$ virtualenv venv2 --python=`which python2`
    Running virtualenv with interpreter /usr/local/bin/python2
    New python executable in /Users/admin/venv2/bin/python
    Installing setuptools, pip, wheel...done.
    MacBook-Pro-2:~ admin$ 
    MacBook-Pro-2:~ admin$ ls venv2/bin/
    activate        easy_install        pip2.7          python2.7
    activate.csh        easy_install-2.7    python          wheel
    activate.fish       pip         python-config
    activate_this.py    pip2            python2
    MacBook-Pro-2:~ admin$ 
    

    Python3 »

    MacBook-Pro-2:~ admin$ virtualenv venv3 --python=`which python3`
    Running virtualenv with interpreter /usr/local/bin/python3
    Using base prefix '/Library/Frameworks/Python.framework/Versions/3.6'
    New python executable in /Users/admin/venv3/bin/python3
    Also creating executable in /Users/admin/venv3/bin/python
    Installing setuptools, pip, wheel...done.
    MacBook-Pro-2:~ admin$ 
    MacBook-Pro-2:~ admin$ ls venv3/bin/
    activate        easy_install        pip3.6          python3.6
    activate.csh        easy_install-3.6    python          wheel
    activate.fish       pip         python-config
    activate_this.py    pip3            python3
    MacBook-Pro-2:~ admin$ 
    

    Checking Python installation locations

    MacBook-Pro-2:~ admin$ which python2
    /usr/local/bin/python2
    MacBook-Pro-2:~ admin$ 
    MacBook-Pro-2:~ admin$ which python3
    /usr/local/bin/python3
    MacBook-Pro-2:~ admin$ 
    
    0 讨论(0)
  • 2020-11-21 05:14

    I use pyenv to manage my python version.

    pyenv install 3.7.3
    pyenv local 3.7.3
    

    Check your python version:

    $ python --version
    Python 3.7.3
    

    Create the virtual environment with venv:

    python -m venv .
    

    Then activate the Virtual Environment:

    source bin/activate
    

    Check your python version:

    $ python --version
    Python 3.7.3
    

    You may need to remove the previous virtual environment

    rm -rf bin
    
    0 讨论(0)
  • 2020-11-21 05:15

    Suppose you currently have python 2.7 installed in your virtualenv. But want to make use of python3.2, You would have to update this with:

    $ virtualenv --python=/usr/bin/python3.2 name_of_your_virtualenv
    

    Then activate your virtualenv by:

    $ source activate name_of_your_virtualenv
    

    and then do: python --version in shell to check whether your version is now updated.

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

    It worked for me on windows with python 2 installation :

    1. Step 1: Install python 3 version .
    2. Step 2: create a env folder for the virtual environment.
    3. Step 3 : c:\Python37\python -m venv c:\path\to\env.

    This is how i created Python 3 virtual environment on my existing python 2 installation.

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