美文网首页
python下多版本与环境隔离

python下多版本与环境隔离

作者: 左手边是幸福 | 来源:发表于2019-01-14 14:32 被阅读10次

    需求

    在使用python的过程中我们经常需要部署python的不同版本. 然后使用不同的安装包. 并且希望他们之间互相不会影响.

    Last login: Mon Jan 14 13:45:52 on ttys000
    xxzxdeMacBook-Pro:~ xxzx$ pyenv versions
    * system (set by /usr/local/var/pyenv/version)
      2.7.14
      2.7.14/envs/my-virtual-env-2.7.14
      2.7.7
      3.5.5
      3.6.4
      3.6.5
      my-virtual-env-2.7.14
    xxzxdeMacBook-Pro:~ xxzx$ cd ~/Desktop/python/python3.6.5/
    xxzxdeMacBook-Pro:python3.6.5 xxzx$ pyenv virtualenv 3.6.5 my-virtual-env-3.6.5
    Using base prefix '/usr/local/var/pyenv/versions/3.6.5'
    New python executable in /usr/local/var/pyenv/versions/3.6.5/envs/my-virtual-env-3.6.5/bin/python3.6
    Also creating executable in /usr/local/var/pyenv/versions/3.6.5/envs/my-virtual-env-3.6.5/bin/python
    Installing setuptools, pip, wheel...
    done.
    Requirement already satisfied: setuptools in /usr/local/var/pyenv/versions/3.6.5/envs/my-virtual-env-3.6.5/lib/python3.6/site-packages
    Requirement already satisfied: pip in /usr/local/var/pyenv/versions/3.6.5/envs/my-virtual-env-3.6.5/lib/python3.6/site-packages
    xxzxdeMacBook-Pro:python3.6.5 xxzx$ pip list
    Package                                Version 
    -------------------------------------- --------
    altgraph                               0.10.2  
    bdist-mpkg                             0.5.0   
    bonjour-py                             0.3     
    macholib                               1.5.1   
    matplotlib                             1.3.1   
    modulegraph                            0.10.4  
    numpy                                  1.8.0rc1
    pbr                                    5.1.1   
    pip                                    18.1    
    py2app                                 0.7.3   
    pyobjc-core                            2.5.1   
    pyobjc-framework-Accounts              2.5.1   
    pyobjc-framework-AddressBook           2.5.1   
    pyobjc-framework-AppleScriptKit        2.5.1   
    pyobjc-framework-AppleScriptObjC       2.5.1   
    pyobjc-framework-Automator             2.5.1   
    pyobjc-framework-CFNetwork             2.5.1   
    pyobjc-framework-Cocoa                 2.5.1   
    pyobjc-framework-Collaboration         2.5.1   
    pyobjc-framework-CoreData              2.5.1   
    pyobjc-framework-CoreLocation          2.5.1   
    pyobjc-framework-CoreText              2.5.1   
    pyobjc-framework-DictionaryServices    2.5.1   
    pyobjc-framework-EventKit              2.5.1   
    pyobjc-framework-ExceptionHandling     2.5.1   
    pyobjc-framework-FSEvents              2.5.1   
    pyobjc-framework-InputMethodKit        2.5.1   
    pyobjc-framework-InstallerPlugins      2.5.1   
    pyobjc-framework-InstantMessage        2.5.1   
    pyobjc-framework-LatentSemanticMapping 2.5.1   
    pyobjc-framework-LaunchServices        2.5.1   
    pyobjc-framework-Message               2.5.1   
    pyobjc-framework-OpenDirectory         2.5.1   
    pyobjc-framework-PreferencePanes       2.5.1   
    pyobjc-framework-PubSub                2.5.1   
    pyobjc-framework-QTKit                 2.5.1   
    pyobjc-framework-Quartz                2.5.1   
    pyobjc-framework-ScreenSaver           2.5.1   
    pyobjc-framework-ScriptingBridge       2.5.1   
    pyobjc-framework-SearchKit             2.5.1   
    pyobjc-framework-ServiceManagement     2.5.1   
    pyobjc-framework-Social                2.5.1   
    pyobjc-framework-SyncServices          2.5.1   
    pyobjc-framework-SystemConfiguration   2.5.1   
    pyobjc-framework-WebKit                2.5.1   
    pyOpenSSL                              0.13.1  
    pyparsing                              2.0.1   
    python-dateutil                        1.5     
    pytz                                   2013.7  
    scipy                                  0.13.0b1
    setuptools                             18.5    
    six                                    1.12.0  
    stevedore                              1.30.0  
    virtualenv                             16.2.0  
    virtualenv-clone                       0.4.0   
    virtualenvwrapper                      4.8.2   
    xattr                                  0.6.4   
    xxzxdeMacBook-Pro:python3.6.5 xxzx$ pyenv activate my-virtual-env-3.6.5 
    pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
    (my-virtual-env-3.6.5) xxzxdeMacBook-Pro:python3.6.5 xxzx$ pip list
    Package    Version
    ---------- -------
    pip        18.1   
    setuptools 40.5.0 
    wheel      0.32.3 
    (my-virtual-env-3.6.5) xxzxdeMacBook-Pro:python3.6.5 xxzx$ pip install Flask
    Collecting Flask
      Downloading https://files.pythonhosted.org/packages/7f/e7/08578774ed4536d3242b14dacb4696386634607af824ea997202cd0edb4b/Flask-1.0.2-py2.py3-none-any.whl (91kB)
        100% |████████████████████████████████| 92kB 10.0kB/s 
    Collecting Werkzeug>=0.14 (from Flask)
      Using cached https://files.pythonhosted.org/packages/20/c4/12e3e56473e52375aa29c4764e70d1b8f3efa6682bef8d0aae04fe335243/Werkzeug-0.14.1-py2.py3-none-any.whl
    Collecting Jinja2>=2.10 (from Flask)
      Using cached https://files.pythonhosted.org/packages/7f/ff/ae64bacdfc95f27a016a7bed8e8686763ba4d277a78ca76f32659220a731/Jinja2-2.10-py2.py3-none-any.whl
    Collecting itsdangerous>=0.24 (from Flask)
      Using cached https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
    Collecting click>=5.1 (from Flask)
      Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
        100% |████████████████████████████████| 81kB 16kB/s 
    Collecting MarkupSafe>=0.23 (from Jinja2>=2.10->Flask)
      Using cached https://files.pythonhosted.org/packages/da/fc/2979c425ad23d528d6ac2e1f3efdc28e572fa1e1fbd5a75171cbdd7ddaa5/MarkupSafe-1.1.0-cp36-cp36m-macosx_10_6_intel.whl
    Installing collected packages: Werkzeug, MarkupSafe, Jinja2, itsdangerous, click, Flask
    Successfully installed Flask-1.0.2 Jinja2-2.10 MarkupSafe-1.1.0 Werkzeug-0.14.1 click-7.0 itsdangerous-1.1.0
    (my-virtual-env-3.6.5) xxzxdeMacBook-Pro:python3.6.5 xxzx$ pip list
    Package      Version
    ------------ -------
    Click        7.0    
    Flask        1.0.2  
    itsdangerous 1.1.0  
    Jinja2       2.10   
    MarkupSafe   1.1.0  
    pip          18.1   
    setuptools   40.5.0 
    Werkzeug     0.14.1 
    wheel        0.32.3 
    (my-virtual-env-3.6.5) xxzxdeMacBook-Pro:python3.6.5 xxzx$ 
    

    步骤

    接下来我们说下步骤, 这里就不说pyenv与pyenv-virtualenv的安装和使用了.

    结合上面的终端步骤来看:

    • 在合适的位置新建一个文件夹.比如我新建了一个文件夹python3.6.5. 因为我准备在在当前的文件夹下使用python3.6.5的python环境
    • 来到终端通过pyenv versions查看当前pyenv安装了哪些版本的python包
    • cd ~/Desktop/python/python3.6.5/进入到这个文件夹中
    • 通过pyenv virtualenv 3.6.5 my-virtual-env-3.6.5命令来创建一个基于python3.6.5的虚拟环境,命名为my-virtual-env-3.6.5
    • 在👆的命令之后会出现下面的. 我们记住当前安装的目录
    Using base prefix '/usr/local/var/pyenv/versions/3.6.5'
    New python executable in /usr/local/var/pyenv/versions/3.6.5/envs/my-virtual-env-3.6.5/bin/python3.6
    Also creating executable in /usr/local/var/pyenv/versions/3.6.5/envs/my-virtual-env-3.6.5/bin/python
    Installing setuptools, pip, wheel...
    
    • 在这之后我通过pip list, 查看的是global下的包
    • pyenv activate my-virtual-env-3.6.5命令激活我们的虚拟环境, 使用pip list再次查看, 默认为我们安装的情况如下
    (my-virtual-env-3.6.5) xxzxdeMacBook-Pro:python3.6.5 xxzx$ pip list
    Package    Version
    ---------- -------
    pip        18.1   
    setuptools 40.5.0 
    wheel      0.32.3 
    
    • 这时候我们安装一个新的 python包
    (my-virtual-env-3.6.5) xxzxdeMacBook-Pro:python3.6.5 xxzx$ pip install Flask
    

    至此, 在终端中的过程就结束了

    pycharm中使用

    正常情况下我们是在pycharm中写我们的代码. 也需要pycharm能够正确的找到我们想要的虚拟环境

    通过pycharm open打开python3.6.5文件夹

    image.png

    打开之后我们可以看到当前的界面

    image.png

    这里的环境是错误的, 我们需要通过如下的配置

    image.png

    通过add添加

    image.png

    ]

    还记得上面终端的安装虚拟环境的时候的地址么

    image.png

    之后退出重新打开当前的项目目录

    image.png

    相关文章

      网友评论

          本文标题:python下多版本与环境隔离

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