Use different Python version with virtualenv

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

提交回复
热议问题