zoukankan      html  css  js  c++  java
  • 064.Python开发虚拟环境


    在使用 Python 开发的过程中,工程一多,难免会碰到不同的工程依赖不同版本的库的问题;亦或者是在开发过程中不想让物理环境里充斥各种各样的库,引发未来的依赖灾难。此时,我们需要对于不同的工程使用不同的虚拟环境来保持开发环境以及宿主环境的清洁。

     virtualenv是一个可以帮助我们管理不同 Python 环境,可以在系统中建立多个不同并且相互不干扰的虚拟环境。

    一 安装、配置virtualenv

    1.1下载包

    指定清华源下载pip的包

    root@darren-virtual-machine:~# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple virtualenv

    升级pip工具

    root@darren-virtual-machine:~# pip3 install --upgrade pip

    1.2 安装virtualenv

    root@darren-virtual-machine:~# pip3 install virtualenv 

    1.3 创建独立的虚拟环境

    root@darren-virtual-machine:~/PycharmProjects/drfdemo# virtualenv  --python=python3  venv

      created virtual environment CPython3.6.9.final.0-64 in 371ms
      creator CPython3Posix(dest=/root/PycharmProjects/drfdemo/venv, clear=False, global=False)
      seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=/root/.local/share/virtualenv/seed-app-data/v1.0.1)
      activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

    root@darren-virtual-machine:~/PycharmProjects/drfdemo# ll

    drwxr-xr-x 4 root root 4096 4月  12 10:28 venv/

    1.4 虚拟环境简单操作

    进入虚拟环境

    root@darren-virtual-machine:~/PycharmProjects/drfdemo# source venv/bin/activate

    使用虚拟环境安装第三方包

    (venv) root@darren-virtual-machine:~/PycharmProjects/drfdemo# pip3 install django==1.9.8

    查看

    (venv) root@darren-virtual-machine:~/PycharmProjects/drfdemo# pip3 list

    复制代码
    Package    Version
    ---------- -------
    Django     1.9.8  
    pip        20.0.2 
    setuptools 46.1.3 
    wheel      0.34.2 
    复制代码

    退出虚拟环境

    (venv) root@darren-virtual-machine:~/PycharmProjects/drfdemo# deactivate

    安装虚拟环境virtualenvwrapper

    virtualenv 的一个最大的缺点就是:

    每次开启虚拟环境之前要去虚拟环境所在目录下的 bin 目录下 source 一下 activate,这就需要我们记住每个虚拟环境所在的目录。

    并且还有可能你忘记了虚拟环境放在哪。。。

    • 一种可行的解决方案是,将所有的虚拟环境目录全都集中起来,例如/opt/all_venv/,并且针对不同的目录做不同的事。
    • 使用virtualenvwrapper管理你的虚拟环境(virtualenv),其实他就是统一管理虚拟环境的目录,并且省去了source的步骤。

    二 虚拟环境virtualenvwrapper

    2.1 安装虚拟环境virtualenvwrapper

    root@darren-virtual-machine:~/PycharmProjects/drfdemo# pip3 install virtualenvwrapper

    设置Linux的用户个人配置文件

    export WORKON_HOME=~/Envs   # 设置virtualenv的统一管理目录, 以后自动下载的虚拟环境,全部都放在这
    export VIRTUALENVWRAPPER_VIRTUALENV_ARGS=''   # 添加virtualenvwrapper的参数,生成干净隔绝的环境
    export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 

    执行virtualenvwrapper安装脚本

    (drfdemo) root@darren-virtual-machine:~/PycharmProjects/drfdemo# source /usr/local/bin/virtualenvwrapper.sh

    (drfdemo) root@darren-virtual-machine:~/PycharmProjects/drfdemo# source /root/.bashrc

    2.2 创建一个虚拟环境简单使用

    root@darren-virtual-machine:~/PycharmProjects/drfdemo# mkvirtualenv drfdemo -p python3

    复制代码
    created virtual environment CPython3.6.9.final.0-64 in 325ms
      creator CPython3Posix(dest=/root/Envs/drfdemo, clear=False, global=False)
      seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=/root/.local/share/virtualenv/seed-app-data/v1.0.1)
      activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
    virtualenvwrapper.user_scripts creating /root/Envs/drfdemo/bin/predeactivate
    virtualenvwrapper.user_scripts creating /root/Envs/drfdemo/bin/postdeactivate
    virtualenvwrapper.user_scripts creating /root/Envs/drfdemo/bin/preactivate
    virtualenvwrapper.user_scripts creating /root/Envs/drfdemo/bin/postactivate
    virtualenvwrapper.user_scripts creating /root/Envs/drfdemo/bin/get_env_details
    复制代码

    (drfdemo) root@darren-virtual-machine:~/PycharmProjects/drfdemo# pip3 list

    Package    Version
    ---------- -------
    pip        20.0.2 
    setuptools 46.1.3 
    wheel      0.34.2

    安装一个第三方插件

    (drfdemo) root@darren-virtual-machine:~/PycharmProjects/drfdemo# pip3 install django

    复制代码
    (drfdemo) root@darren-virtual-machine:~/PycharmProjects/drfdemo# pip3 install django
    Collecting django
      Using cached Django-3.0.5-py3-none-any.whl (7.5 MB)
    Collecting asgiref~=3.2
      Using cached asgiref-3.2.7-py2.py3-none-any.whl (19 kB)
    Collecting pytz
      Using cached pytz-2019.3-py2.py3-none-any.whl (509 kB)
    Collecting sqlparse>=0.2.2
      Using cached sqlparse-0.3.1-py2.py3-none-any.whl (40 kB)
    Installing collected packages: asgiref, pytz, sqlparse, django
    Successfully installed asgiref-3.2.7 django-3.0.5 pytz-2019.3 sqlparse-0.3.1
    复制代码

    (drfdemo) root@darren-virtual-machine:~/PycharmProjects/drfdemo# pip3 list

    复制代码
    Package    Version
    ---------- -------
    asgiref    3.2.7  
    Django     3.0.5  
    pip        20.0.2 
    pytz       2019.3 
    setuptools 46.1.3 
    sqlparse   0.3.1  
    wheel      0.34.2
    复制代码

    退出虚拟环境

    (drfdemo) root@darren-virtual-machine:~/PycharmProjects/drfdemo# deactivate

    删除虚拟环境

    root@darren-virtual-machine:~/PycharmProjects/drfdemo# rmvirtualenv drfdemo

    Removing drfdemo...

    参考:https://www.cnblogs.com/tiger666/articles/10312546.html

    学习记录,小白一枚
  • 相关阅读:
    放苹果
    滑雪-递归
    模数之和
    n个数的最小公倍数
    webapi跨域实现(CROS、JSONP)
    HttpRequestMessage扩展方法
    webapi基于单请求封装多请求的设计【转】
    webapi中获取HttpContext
    webapi序列化控制
    webapi简介及参数绑定
  • 原文地址:https://www.cnblogs.com/wangsirde0428/p/14322944.html
Copyright © 2011-2022 走看看