4.2 虚拟环境:Pipenv ==================== |image0| 以前一直使用pip+virtualenv+virtualwrapper管理模块和环境, 但是virtualwrapper在windows上使用不太方便,而且包和环境分开管理确实经常不记得哪个是哪个了。 为什么 会推荐 pipenv 呢? - 它是 ``virtualenv`` 和 ``pip`` 的合体,可以合起来使用; - 使用\ ``Pipfile`` 和 ``Pipfile.lock``\ 替代\ ``requirements.txt`` - 可以使用 ``pipenv graph``\ 很方便的看出包的依赖关系。 - 通过加载\ ``.env``\ 文件简化开发工作流程 1. 安装pipenv ------------- 如果你的电脑上没有安装 pipenv,可以使用如下方法安装 .. code:: shell # mac $ brew install pipenv # windows pip install [--user] pipenv 如果你的电脑是 windows 的。 |image1| 需要将如标示路径,加入到 环境变量 PATH 中。 |image2| 然后需要重启一下,CMD 终端才能够刷新环境变量。 2. 创建虚拟环境 --------------- DjangoWebBlog 是我们的项目目录,进入这个目录下创建虚拟环境 .. code:: shell $ mkdir DjangoWebBlog && cd DjangoWebBlog # 在当前目录下创建一个虚拟环境(默认的Python版本) $ pipenv install 你也可以指定版本创建 .. code:: shell $ pipenv --two # 相当于 pipenv --python /usr/bin/python2 $ pipenv --three # 相当于 pipenv --python /usr/bin/python3 $ pipenv --python 3.7 # 也可以指定具体的版本 pipenv install --python 2 这边以安装 python2 版本的虚拟环境为例说明。 |image3| 如果你原项目使用的是 requirements.txt 这个管理包的方式,这时候执行 ``pipenv --tow`` 创建一个虚拟环境后,会找到 requirements.txt ,并根据这里面的依赖包生成 Pipfile文件。 |image4| 3. 查询虚拟环境 --------------- .. code:: shell # 返回项目的路径 $ pipenv --where # 返回虚拟环境路径 $ pipenv --venv # 返回该虚拟环境的解释器 $ pipenv --py 演示如下: |image5| 4. 操作虚拟环境 --------------- .. code:: shell # 进入这个虚拟环境 $ pipenv shell # 退出这个虚拟环境 $ exit $ deactivate # 移除当前目录的虚拟环境 $ pipenv --rm 执行 ``pipenv shell`` 就可以进入这个虚拟环境,在头部会有虚拟环境的标识名称。有这个标识,说明已经进入虚拟环境。 |image6| .. code:: python # 在当前虚拟环境中运行 $ pipenv run python # 进入交互式,跟直接执行 python 一样 $ pipenv run python 文件名 # 运行文件 $ pipenv run pip ... # 运行pip 5. 虚拟环境包管理 ----------------- .. code:: shell # 安装一个本地包(setup.py)到虚拟环境(Pipfile) $ pipenv install -e . # 安装、卸载模块 $ pipenv install requests $ pipenv uninstall requests $ pipenv uninstall --all # 卸载全部包 $ pipenv install -r path/to/requirements.txt # 安装所有依赖 $ pipenv install --dev # 更新包 $ pipenv update # 更新所有包 $ pipenv update --outdated # 打印所有要更新的包 $ pipenv update <包名> # 更新指定的包 # 将Pipfile和Pipfile.lock文件里面的包导出为requirements.txt文件 $ pipenv run pip freeze # 相当于pipenv run pip freeze >requirements.txt $ pipenv lock -r > requirements.txt $ pipenv lock -r --dev # 若只想导出开发用的包 6. 其他命令 ----------- .. code:: shell # 创建一个包含预发布的锁文件: $ pipenv lock --pre # 打印所有包的依赖关系图 $ pipenv graph # 检查安全漏洞 $ pipenv check 打印该虚拟环境下所有包的依赖关系图 |image7| 有的python第三方包旧版本会有安全漏洞,使用 pipenv check 可以检查安全漏洞。 |image8| .env`文件,用来存放一些环境变量。 -------------- |image9| .. |image0| image:: http://image.iswbm.com/20200602135014.png .. |image1| image:: http://image.iswbm.com/Fk6WZ2xbqg2DM3AvnYCpsiKQ4xOn .. |image2| image:: http://image.iswbm.com/FjuJ8yZsgjkzVuBRZHxK1ZnnzaEX .. |image3| image:: http://image.iswbm.com/20190612211330.png .. |image4| image:: http://image.iswbm.com/20190612213015.png .. |image5| image:: http://image.iswbm.com/20190612213950.png .. |image6| image:: http://image.iswbm.com/20190612211925.png .. |image7| image:: http://image.iswbm.com/20190614000336.png .. |image8| image:: http://image.iswbm.com/20190612215924.png .. |image9| image:: http://image.iswbm.com/20200607174235.png