美文网首页
Python 虚拟环境 virtualenv

Python 虚拟环境 virtualenv

作者: 华阳_3bcf | 来源:发表于2018-10-25 16:27 被阅读0次

    virtualenv 用于 Python 的版本管理和环境隔离。

    practice on MacOS

    安装

    $ pip install virtualenv virtualenvwrapper
    Collecting virtualenv
      Downloading https://files.pythonhosted.org/packages/b6/30/96a02b2287098b23b875bc8c2f58071c35d2efe84f747b64d523721dc2b5/virtualenv-16.0.0-py2.py3-none-any.whl (1.9MB)
        100% |████████████████████████████████| 1.9MB 3.4MB/s
    Collecting virtualenvwrapper
      Downloading https://files.pythonhosted.org/packages/2b/8c/3192e10913ad945c0f0fcb17e9b2679434a28ad58ee31ce0104cba3b1154/virtualenvwrapper-4.8.2-py2.py3-none-any.whl
      ....
      ....
    
      Requirement already satisfied: six>=1.10.0 in /usr/local/lib/python2.7/site-packages (from stevedore->virtualenvwrapper) (1.11.0)
    Installing collected packages: virtualenv, pbr, stevedore, virtualenv-clone, virtualenvwrapper
    Successfully installed pbr-5.1.0 stevedore-1.30.0 virtualenv-16.0.0 virtualenv-clone-0.4.0 virtualenvwrapper-4.8.2
    

    使用

    $ virtualenv [-p <PYTHON_INTERPRETER_PATH>] venv # 创建环境,venv是目录名,自定义

    $ source venv/bin/activate # 激活虚拟环境

    $ virtualenv -p /usr/local/bin/python3 venv
    Running virtualenv with interpreter /usr/local/bin/python3
    Using base prefix '/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6'
    New python executable in /Users/royzeng/venv/bin/python3.6
    Also creating executable in /Users/royzeng/venv/bin/python
    Installing setuptools, pip, wheel...done.
    

    激活使用

    $ cd venv
    $ source bin/activate
    (venv)
    $ which python
    /Users/royzeng/venv/bin/python
    (venv)
    $ python --version
    Python 3.6.5
    (venv)
    $ pip --version
    pip 18.1 from /Users/royzeng/venv/lib/python3.6/site-packages/pip (python 3.6)
    (venv)
    
    

    默认的python2 已经变成了python3.

    virtualenvwrapper 用法

    virtualenvwrapper 是用来管理 virtualenv 的。当你有多个虚拟环境,用它就比较方便了

    配置:

    # ~/.zshrc
    export WORKON_HOME=$HOME/.virtualenvs
    source /usr/local/bin/virtualenvwrapper.sh
    

    使用:

    • 创建虚拟环境 $ mkvirtualenv [-p <PYTHON_PATH>] <VENV_NAME>
    • 查看现有的环境: $ workon
    • 切换到指定的环境: $ workon <VENV_NAME>

    要删除环境的话,到 ~/.virtualenvs/ 里删除相应的目录就行了。

    e.g.

    配置文件生效

    $ source ~/.zshrc
    virtualenvwrapper.user_scripts creating /Users/royzeng/.virtualenvs/premkproject
    virtualenvwrapper.user_scripts creating /Users/royzeng/.virtualenvs/postmkproject
    virtualenvwrapper.user_scripts creating /Users/royzeng/.virtualenvs/initialize
    virtualenvwrapper.user_scripts creating /Users/royzeng/.virtualenvs/premkvirtualenv
    virtualenvwrapper.user_scripts creating /Users/royzeng/.virtualenvs/postmkvirtualenv
    virtualenvwrapper.user_scripts creating /Users/royzeng/.virtualenvs/prermvirtualenv
    virtualenvwrapper.user_scripts creating /Users/royzeng/.virtualenvs/postrmvirtualenv
    virtualenvwrapper.user_scripts creating /Users/royzeng/.virtualenvs/predeactivate
    virtualenvwrapper.user_scripts creating /Users/royzeng/.virtualenvs/postdeactivate
    virtualenvwrapper.user_scripts creating /Users/royzeng/.virtualenvs/preactivate
    virtualenvwrapper.user_scripts creating /Users/royzeng/.virtualenvs/postactivate
    virtualenvwrapper.user_scripts creating /Users/royzeng/.virtualenvs/get_env_details
    
    

    创建虚拟环境

    $ mkvirtualenv -p /usr/local/bin/python3 myp3
    Running virtualenv with interpreter /usr/local/bin/python3
    Using base prefix '/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6'
    New python executable in /Users/royzeng/.virtualenvs/myp3/bin/python3.6
    Also creating executable in /Users/royzeng/.virtualenvs/myp3/bin/python
    Installing setuptools, pip, wheel...done.
    virtualenvwrapper.user_scripts creating /Users/royzeng/.virtualenvs/myp3/bin/predeactivate
    virtualenvwrapper.user_scripts creating /Users/royzeng/.virtualenvs/myp3/bin/postdeactivate
    virtualenvwrapper.user_scripts creating /Users/royzeng/.virtualenvs/myp3/bin/preactivate
    virtualenvwrapper.user_scripts creating /Users/royzeng/.virtualenvs/myp3/bin/postactivate
    virtualenvwrapper.user_scripts creating /Users/royzeng/.virtualenvs/myp3/bin/get_env_details
    (myp3)
    $ mkvirtualenv -p /usr/local/bin/python myp2
    Running virtualenv with interpreter /usr/local/bin/python
    New python executable in /Users/royzeng/.virtualenvs/myp2/bin/python2.7
    Also creating executable in /Users/royzeng/.virtualenvs/myp2/bin/python
    Installing setuptools, pip, wheel...done.
    virtualenvwrapper.user_scripts creating /Users/royzeng/.virtualenvs/myp2/bin/predeactivate
    virtualenvwrapper.user_scripts creating /Users/royzeng/.virtualenvs/myp2/bin/postdeactivate
    virtualenvwrapper.user_scripts creating /Users/royzeng/.virtualenvs/myp2/bin/preactivate
    virtualenvwrapper.user_scripts creating /Users/royzeng/.virtualenvs/myp2/bin/postactivate
    virtualenvwrapper.user_scripts creating /Users/royzeng/.virtualenvs/myp2/bin/get_env_details
    (myp2)
    

    查看当前虚拟环境目录

    $ workon
    myp2
    myp3
    (myp3)
    $ which python
    /Users/royzeng/.virtualenvs/myp3/bin/python
    (myp3)
    

    切换虚拟环境

    $ workon myp2
    (myp2)
    

    退出虚拟环境

    $ deactivate
    
    

    删除虚拟环境

    $ rmvirtualenv myp2
    Removing myp2...
    (myp3)
    

    相关文章

      网友评论

          本文标题:Python 虚拟环境 virtualenv

          本文链接:https://www.haomeiwen.com/subject/zzrctqtx.html