Use different Python version with virtualenv

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

    These two commands should work fine.

    virtualenv -p python2 myenv (For python2)

    virtualenv -p python3 myenv (For python3)

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

    I use Windows so I should use .exe on the pthon path

    virtualenv -p=C:\Python27\python2.exe <envname>
    
    0 讨论(0)
  • 2020-11-21 05:38

    [November 2019] I needed to install a Python 3.7 environment (env) on my Python 3.8-based Arch Linux system. Python 3.7 was no longer on the system, so I could not downgrade Python, to install a package that I needed.

    Furthermore, I wanted to use that package / Python 3.7 inside a virtual environment (venv). This is how I did it.


    Download Python version source files:

    I downloaded the Python 3.7.4 source files from

    https://www.python.org/downloads/source/

    to

    /mnt/Vancouver/apps/python_versions/src/Python-3.7.4.tgz

    I then extracted that archive (source files) to

    /mnt/Vancouver/apps/python_versions/src/Python-3.7.4/


    Installation:

    [Note: in my system env, not a venv.]

    cd /mnt/Vancouver/apps/python_versions/src/Python-3.7.4/
    time ./configure                 ## 17 sec
    time make                        ## 1 min 51 sec
    time sudo make install           ## 18 sec
    time make clean                  ## 0.3 sec
    

    Examine installed Python versions:

    $ which python
    /usr/bin/python
    
    $ python --version
    Python 3.8.0
    
    $ which python3.7
    /usr/local/bin/python3.7
    
    $ python    ## Python 3.8 [system / env]
    Python 3.8.0 (default, Oct 23 2019, 18:51:26) 
    [GCC 9.2.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    
    $ python3.7    ## newly-installed Python 3.7 package
    Python 3.7.4 (default, Nov 20 2019, 11:36:53) 
    [GCC 9.2.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys
    >>> print(sys.version)
    3.7.4 (default, Nov 20 2019, 11:36:53) 
    [GCC 9.2.0]
    >>>
    
    $ python3.7 --version                                                                                                 
    Python 3.7.4
    

    How to create a venv for a specific Python version:

    https://docs.python.org/3/tutorial/venv.html

    12.2. CREATING VIRTUAL ENVIRONMENTS

    The module used to create and manage virtual environments is called venv. venv will usually install the most recent version of Python that you have available. If you have multiple versions of Python on your system, you can select a specific Python version by running python3 or whichever version you want.

    To create a virtual environment, decide upon a directory where you want to place it, and run the venv module as a script with the directory path:

    python3 -m venv tutorial-env

    This will create the tutorial-env directory if it doesn’t exist, and also create directories inside it containing a copy of the Python interpreter, the standard library, and various supporting files. ...


    Create Python 3.7 venv [on a Python 3.8 operating env / system]:

    python3.7 -m venv ~/venv/py3.7      ## create Python 3.7-based venv
    source ~/venv/py3.7/bin/activate    ## activate that venv
    deactivate                          ## deactivate that venv (when done, there)
    

    Added to ~/.bashrc:

    alias p37='echo "   [Python 3.7 venv (source ~/venv/py3.7/bin/activate)]" && source ~/venv/py3.7/bin/activate'
    

    Test Python 3.7 venv:

    $ p37                                                                                                                 
    [Python 3.7 venv (source ~/venv/py3.7/bin/activate)]
    
    (py3.7)$ python --version
    Python 3.7.4
    
    (py3.7)$ python
    Python 3.7.4 (default, Nov 20 2019, 11:36:53) 
    [GCC 9.2.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys
    >>> print(sys.version)
    3.7.4 (default, Nov 20 2019, 11:36:53) 
    [GCC 9.2.0] 
    >>>
    
    0 讨论(0)
  • 2020-11-21 05:38
    virtualenv -p python3 myenv
    

    Link to Creating virtualenv

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

    These seem a little overcomplicated for Windows. If you're on Windows running python 3.3 or later, you can use the python launcher py to do this much more easily. Simply install the different python version, then run:

    py -[my version] -m venv env
    

    This will create a virtual environment called env in your current directory, using python [my version]. As an example:

    py -3.7 -m venv env
    ./env/Scripts/activate
    

    This creates a virtual environment called env using python3.7 and activates it. No paths or other complex stuff required.

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

    UBUNTU 19.04 / Global Python 3.7.

    This worked for me, enabling a Python 3.8 environment using the recommended venv for python 3 development.

    Install 3.8 and 3.8 venv module

    $ sudo apt install python3.8 python3.8-venv ## plus any other modules you need

    Create your Virtual Env using the python version you want in that env

    $ /usr/bin/python3.8 -m venv python38-env

    switch into your virtual env

    $ source python38-env/bin/activate

    python -V = python 3.8

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