This is the sixth day of my participation in Gwen Challenge

There are many Versions of Python, Python2 and Python3 are very different, many projects need to run on the same server, we can choose to run directly, or choose to use Docker. If docker is used, there is no need to isolate the environment. If you want to run directly on the server, you must have a Python isolated environment. For example, some projects use Python 3.5 and some use Python 3.7, so pyEnv can help you isolate the environment so that multiple versions of Python can coexist without any conflicts.

1 installation

➜  ~ brew install pyenv
Copy the code

2 Configure environment variables

➜ ~ vim. ZSHRCexport PYENV_ROOT=/usr/local/var/pyenv
if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
Copy the code

3 pyenv use

  • View the Version of Python in use

    ➜  ~ pyenv version
    3.6.8 (set by /usr/local/var/pyenv/version)
    Copy the code
  • View the Python versions supported by the system

    ➜  ~ pyenv versions
      system
    * 3.6.8 (set by /usr/local/var/pyenv/version)
    Copy the code

    * indicates the Python version being used, and system indicates the python of the system

  • See what Python is available to install

    ➜  ~ pyenv install -l
    Available versions:
      2.1.3
      2.2.3
      2.3.7
      2.4.0
      2.4.1
      2.4.2
      2.4.3
      2.4.4
      2.4.5
      2.4.6
      2.5.0
      2.5.1
    Copy the code
  • Install the specified version of Pyton

    ➜  ~ pyenv install 3.6.8
    Copy the code
  • Python Version Switching

    1 Global switch ➜ ~ pyenv global 3.6.8 2 Directory switch ➜ ~ Pyenv local SystemCopy the code

4 virtualenv

4.1 installation

# Install with PIP2
➜  ~ pip install virtualenvwrapper
Copy the code

4.2 configuration

➜ ~ vim. ZSHRCexport WORKON_HOME=$HOME/.virtualenvs
exportVIRTUALENVWRAPPER_SCRIPT = / Users/mervinwang/Library/Python / 2.7 / bin/virtualenvwrapper. Shexport VIRTUALENVWRAPPER_PYTHON=/usr/bin/python
exportVIRTUALENVWRAPPER_VIRTUALENV = / Users/mervinwang/Library/Python / 2.7 / bin/virtualenvsource/ Users/mervinwang/Library/Python / 2.7 / bin/virtualenvwrapper. ShCopy the code

4.3 Example

4.3.1 Creating the PYTHon2 Virtual Environment

➜  ~ mkdir py{2,3}
➜  ~ cdPy2 ➜ py2 pyenvlocalSystem ➜ py2 mkvirtualenv -p python2 py2-env (py2-env) ➜ py2 python -v python 2.7.16Copy the code

4.3.2 Creating python3 Virtual Environment

➜  ~ cdPy3 ➜ py3 pyenvlocal 3.6.8             
➜  py3 rmvirtualenv py3-env 
Removing py3-env...
➜  py3 mkvirtualenv -p python3.6 py3-env
(py3-env) ➜  py3 python -V                        
Python 3.6.8
Copy the code