How do I use different Python version in venv from standard library? (Not virtualenv!)

后端 未结 3 1594
耶瑟儿~
耶瑟儿~ 2020-12-14 02:06

I have installed Python 3.4.0 and created virtual environment with python -m venv myenv. How can I change Python version in my virtual environment? Documentati

3条回答
  •  孤城傲影
    2020-12-14 02:27

    This is a very good question as there are several python modules / libraries (built-in & third party) with similar names and purposes. Can completely sympathise with OP's confusion.

    There are really two different behaviours / responsibilities:

    1). The ability to switch between different versions of (System) Python Interpreter eg. 2.7.10 or 3.5.0 etc

    2). The ability to create virtual environments (which is just a local folder containing all the plumbing (binaries and libs) for a particular version of python. Can sort of think of this as a frozen local instance of a particular python version. Essentially it is a self-contained, light-weight python installation.

    A module like pyvenv provides 2) above. It will allow you to create a virtual environment that is set at the version of Python that was used to create it.

    $ python --version
    Python 3.5.0
    $ pyvenv myenv   # myenv is now a local environment using Python 3.5.0
    

    For further infoormation on pyvenv, see library/venv

    A module like pyenv (the names are confusing, right? Notice, pyenv, and not pyvenv) on the other hand, controls which VERSION of python your system is basically running. This provides 1) above. So, when not running a particular virtual env via pyvenv etc, this is the "global" version in use. In fact, it is slightly more convoluted than that (as you can also setup local configuration etc), but essentially that is enough for this discussion.

    For further information on pyenv see github.com/yyuu/pyenv

    Suppose I want to run Python versions 2.7.10 and 3.5.0, then I would use pyenv to install these two versions (here, I chose as globals), and can view this using:

    $ pyenv versions
      system
    * 2.7.10 (set by ~/.pyenv/version)
    * 3.5.0 (set by ~/.pyenv/version)
    
    $ python --version
    Python 3.5.0
    
    $ which python
    ~/.pyenv/shims/python
    
    $ python2.7 --version
    Python 2.7.10
    

    Yes, there are several prominant alternatives to each of the above referenced modules / libs. Heated discussions on Reddit / SOF etc detailing and arguing which is best. Many of them do very similar things...

提交回复
热议问题