本篇笔记内容全部摘自慕课网Jack老师课程笔记,不是原创

Pipenv管理Python虚拟环境

2020年5月更新:有不少同学反映pipenv问题太多,我后来也发现的确是,建议大家直接弃坑。管理Python项目环境的方式有多种,用virtualenv最好,简单稳定。本课程后面章节学习,同学们直接用virtualenv就好了。坚持用pipenv的同学建议先多搜几篇文章学习下,理解它的命令。

一、pipenv介绍

简单说,pipenv就是把pipvirtualenv包装起来的一个便携工具,它会自动为您的项目创建和管理virtualenv,以及在安装/卸载软件包时添加/删除Pipfile中的软件包。
使用pipenv管理python环境虚拟环境的优势:

  • 不再需要分开使用pipvirtualenv,合并到了一起
  • 不再需要管理requirements.txtpipenv install xxx安装包时自动生成版本号、Hash值、包依赖关系到PipfilePipfile.lock,并且可以手工编辑以区分开发环境和生产环境
  • 使用Hash校验安装的包,有利于主动暴露安全问题
  • 使用最新版本的依赖项,以最大限度地降低过时组件带来的安全风险
  • 可以图形化显示包之间的依赖关系(pipenv graph
  • 通过加载.env文件简化开发工作流程

    二、安装pipenv

    首先,确保你已经正确安装python3和pip3

    1. [root@shiyanlou ~]# python3 -V
    2. Python 3.7.2
    3. [root@shiyanlou ~]# pip3 -V
    4. pip 19.0.1 from /usr/local/python3/lib/python3.7/site-packages/pip (python 3.7)
    5. [root@shiyanlou ~]#

    然后,Linux下pip3 install pipenv,Mac下brew install pipenv

    1. [root@shiyanlou ~]# pip3 install pipenv
    2. Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
    3. Collecting pipenv
    4. Downloading http://mirrors.aliyun.com/pypi/packages/13/b4/3ffa55f77161cff9a5220f162670f7c5eb00df52e00939e203f601b0f579/pipenv-2018.11.26-py3-none-any.whl (5.2MB)
    5. 100% |████████████████████████████████| 5.2MB 4.0MB/s
    6. Requirement already satisfied: certifi in /usr/local/python3/lib/python3.7/site-packages (from pipenv) (2018.11.29)
    7. Requirement already satisfied: setuptools>=36.2.1 in /usr/local/python3/lib/python3.7/site-packages (from pipenv) (40.6.2)
    8. Requirement already satisfied: pip>=9.0.1 in /usr/local/python3/lib/python3.7/site-packages (from pipenv) (18.1)
    9. Requirement already satisfied: virtualenv in /usr/local/python3/lib/python3.7/site-packages (from pipenv) (16.2.0)
    10. Requirement already satisfied: virtualenv-clone>=0.2.5 in /usr/local/python3/lib/python3.7/site-packages (from pipenv) (0.4.0)
    11. Installing collected packages: pipenv
    12. Successfully installed pipenv-2018.11.26
    13. [root@shiyanlou ~]#

    pipenv依赖psutil, virtualenv-clone, pew, certifi, urllib3, chardet, requests, mccabe, pyflakes, pycodestyle, flake8等第三方模块,pipenv有很漂亮的彩色终端界面。

    三、Pipfile和Pipfile.lock

  • Pipfile, 简明地显示项目环境和依赖包

    1. [[source]]
    2. name = "pypi"
    3. url = "https://mirrors.aliyun.com/pypi/simple/" # 指定国内pip源,不然会慢死人的
    4. verify_ssl = true
    5. [dev-packages] # 开发环境
    6. [packages] # 生产环境
    7. requests = "*" # *表示最新版本
    8. django = "*"
    9. [requires]
    10. python_version = "3.7" # python版本
  • Pipfile.lock, 详细记录环境依赖,并且利用了Hash算法保证了它完整对应关系

    1. {
    2. "_meta": {
    3. "hash": {
    4. "sha256": "d737a1ef2f8b591f86cd0fa6a8cb48918f742d60df2a394ebc3d23aff4748999"
    5. },
    6. "pipfile-spec": 6,
    7. "requires": {
    8. "python_version": "3.7"
    9. },
    10. "sources": [
    11. {
    12. "name": "pypi",
    13. "url": "https://pypi.org/simple",
    14. "verify_ssl": true
    15. }
    16. ]
    17. },
    18. "default": {
    19. "certifi": {
    20. "hashes": [
    21. "sha256:47f9c83ef4c0c621eaef743f133f09fa8a74a9b75f037e8624f83bd1b6626cb7",
    22. "sha256:993f830721089fef441cdfeb4b2c8c9df86f0c63239f06bd025a76a7daddb033"
    23. ],
    24. "version": "==2018.11.29"
    25. },
    26. "chardet": {
    27. "hashes": [
    28. "sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae",
    29. "sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691"
    30. ],
    31. "version": "==3.0.4"
    32. },
    33. "django": {
    34. "hashes": [
    35. "sha256:a32c22af23634e1d11425574dce756098e015a165be02e4690179889b207c7a8",
    36. "sha256:d6393918da830530a9516bbbcbf7f1214c3d733738779f06b0f649f49cc698c3"
    37. ],
    38. "index": "pypi",
    39. "version": "==2.1.5"
    40. },
    41. "idna": {
    42. "hashes": [
    43. "sha256:c357b3f628cf53ae2c4c05627ecc484553142ca23264e593d327bcde5e9c3407",
    44. "sha256:ea8b7f6188e6fa117537c3df7da9fc686d485087abf6ac197f9c46432f7e4a3c"
    45. ],
    46. "version": "==2.8"
    47. },
    48. "pytz": {
    49. "hashes": [
    50. "sha256:32b0891edff07e28efe91284ed9c31e123d84bea3fd98e1f72be2508f43ef8d9",
    51. "sha256:d5f05e487007e29e03409f9398d074e158d920d36eb82eaf66fb1136b0c5374c"
    52. ],
    53. "version": "==2018.9"
    54. },
    55. "requests": {
    56. "hashes": [
    57. "sha256:502a824f31acdacb3a35b6690b5fbf0bc41d63a24a45c4004352b0242707598e",
    58. "sha256:7bf2a778576d825600030a110f3c0e3e8edc51dfaafe1c146e39a2027784957b"
    59. ],
    60. "index": "pypi",
    61. "version": "==2.21.0"
    62. },
    63. "urllib3": {
    64. "hashes": [
    65. "sha256:61bf29cada3fc2fbefad4fdf059ea4bd1b4a86d2b6d15e1c7c0b582b9752fe39",
    66. "sha256:de9529817c93f27c8ccbfead6985011db27bd0ddfcdb2d86f3f663385c6a9c22"
    67. ],
    68. "version": "==1.24.1"
    69. }
    70. },
    71. "develop": {}
    72. }

    四、虚拟环境管理

    4.1 创建虚拟环境

    进入项目的文件夹,直接pipenv install xxx安装你需要的包,就会在当前文件夹下创建虚拟环境,生成PipfilePipfile.lock

    1. [root@shiyanlou ~]# cd myproject/
    2. [root@shiyanlou myproject]# pipenv install requests
    3. Creating a virtualenv for this project
    4. Pipfile: /root/myproject/Pipfile
    5. Using /usr/local/python3/bin/python3.7 (3.7.2) to create virtualenv
    6. Creating virtual environment...Already using interpreter /usr/local/python3/bin/python3.7
    7. Using base prefix '/usr/local/python3'
    8. New python executable in /root/.local/share/virtualenvs/myproject-zKJX12a5/bin/python3.7
    9. Also creating executable in /root/.local/share/virtualenvs/myproject-zKJX12a5/bin/python
    10. Installing setuptools, pip, wheel...
    11. done.
    12. Successfully created virtual environment!
    13. Virtualenv location: /root/.local/share/virtualenvs/myproject-zKJX12a5
    14. Creating a Pipfile for this project
    15. Installing requests
    16. Installation Succeeded
    17. Pipfile.lock not found, creating
    18. Locking [dev-packages] dependencies
    19. Locking [packages] dependencies
    20. Success!
    21. Updated Pipfile.lock (444a6d)!
    22. Installing dependencies from Pipfile.lock (444a6d)…
    23. ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 5/5 00:00:04
    24. To activate this project's virtualenv, run pipenv shell.
    25. Alternatively, run a command inside the virtualenv with pipenv run.
    26. [root@shiyanlou myproject]# ll -al
    27. total 12
    28. drwxr-xr-x. 2 root root 39 Jan 31 14:09 .
    29. dr-xr-x---. 17 root root 4096 Jan 31 14:08 ..
    30. -rw-r--r--. 1 root root 153 Jan 31 14:09 Pipfile
    31. -rw-r--r--. 1 root root 1914 Jan 31 14:09 Pipfile.lock
    32. [root@shiyanlou myproject]#

    如果需要指定python版本的话

    1. $ pipenv --three # 泛指python的版本
    2. $ pipenv --python 3.7 # 或者,特指某个python版本
    3. $ pipenv --python <path/to/python> # 或者,特指某个位置的python

    4.2 进入/退出/删除虚拟环境

    1. [root@shiyanlou myproject]# pipenv shell # 进入虚拟环境
    2. Launching subshell in virtual environment
    3. . /root/.local/share/virtualenvs/myproject-zKJX12a5/bin/activate
    4. [root@shiyanlou myproject]# . /root/.local/share/virtualenvs/myproject-zKJX12a5/bin/activate
    5. (myproject) [root@shiyanlou myproject]# exit # 退出虚拟环境
    6. exit
    7. [root@shiyanlou myproject]# pipenv --rm # 删除整个环境
    8. Removing virtualenv (/root/.local/share/virtualenvs/myproject-zKJX12a5)…
    9. [root@shiyanlou myproject]#

    有几点需要注意:

  • 平时安装和卸载包的时候不需要先进入或退出虚拟环境,直接在项目文件下pipenv install/uninstall xxx就行了

  • 退出虚拟环境不要deactivate,请使用exit,否则下次pipenv进入的时候就会报如下错

    1. [root@shiyanlou myproject]# pipenv shell
    2. Shell for UNKNOWN_VIRTUAL_ENVIRONMENT already activated.
    3. No action taken to avoid nested environments.
    4. [root@shiyanlou myproject]#
  • pipenv --rm并不会删除生成的PipfilePipfile.lock

    4.3 管理开发环境

    通常有一些Python包只在你的开发环境中需要,而不是在生产环境中,例如单元测试包。 Pipenv使用–dev标志区分两个环境。也就是说一个–dev参数,帮你在同一个虚拟环境中又区分出了开发和非开发环境。

    1. $ pipenv install --dev pytest

    在虚拟环境中运行命令,使用run参数,提供要运行的命令:

    1. $ pipenv run python manage.py runserver

    这将使用当前虚拟环境关联的Python解释器,执行命令。或者简单的执行脚本:

    1. $ pipenv run python your_script.py

    如果你不想每次运行Python时都输入这么多字符,可以在shell中设置一个别名,例如:

    1. $ alias prp="pipenv run python"

    冻结Pipfile,相当于将项目所使用的第三方库列表进行打包输出,类似于virtualenv中生成requirements.txt文件。通过更新Pipfile.lock来冻结库名称及其版本,以及其依赖关系的列表。需要使用lock参数:

    1. [root@shiyanlou myproject]# pipenv lock
    2. Locking [dev-packages] dependencies
    3. Locking [packages] dependencies
    4. Success!
    5. Updated Pipfile.lock (748999)!
    6. [root@shiyanlou myproject]#

    定位项目路径、虚拟环境、Python解释器,显示依赖关系,卸载包

    1. [root@shiyanlou myproject]# pipenv --where # 定位项目路径
    2. /root/myproject
    3. [root@shiyanlou myproject]# pipenv --venv # 定位虚拟环境
    4. /root/.local/share/virtualenvs/myproject-zKJX12a5
    5. [root@shiyanlou myproject]# pipenv --py # 定位Python解释器
    6. /root/.local/share/virtualenvs/myproject-zKJX12a5/bin/python
    7. [root@shiyanlou myproject]# pipenv graph # 显示依赖关系
    8. atomicwrites==1.2.1
    9. attrs==18.2.0
    10. Django==2.1.5
    11. - pytz [required: Any, installed: 2018.9]
    12. more-itertools==5.0.0
    13. - six [required: >=1.0.0,<2.0.0, installed: 1.12.0]
    14. py==1.7.0
    15. requests==2.21.0
    16. - certifi [required: >=2017.4.17, installed: 2018.11.29]
    17. - chardet [required: >=3.0.2,<3.1.0, installed: 3.0.4]
    18. - idna [required: >=2.5,<2.9, installed: 2.8]
    19. - urllib3 [required: >=1.21.1,<1.25, installed: 1.24.1]
    20. [root@shiyanlou myproject]# pipenv uninstall --all # 卸载所有包
    21. Un-installing all [dev-packages] and [packages]…
    22. Found 12 installed package(s), purging
    23. Uninstalling Django-2.1.5:
    24. Successfully uninstalled Django-2.1.5
    25. ...略...
    26. Environment now purged and fresh!
    27. [root@shiyanlou myproject]#

    4.4 有个大坑等你跳

    pipenv有个缺点,lock不稳定而且时间非常长,所以安装包的时候记得加上--skip-lock,在最后开发完成要提交到仓库的时候再pipenv lock。希望将来的版本能够优化。

    1. [root@shiyanlou myproject]# pipenv install django --skip-lock
    2. Installing django
    3. Installation Succeeded
    4. Installing dependencies from Pipfile
    5. ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 2/2 00:00:02
    6. [root@shiyanlou myproject]#

    五、常用命令汇总

    pipenv具有下列的选项:

    1. $ pipenv
    2. Usage: pipenv [OPTIONS] COMMAND [ARGS]...
    3. Options:
    4. --where 显示项目文件所在路径
    5. --venv 显示虚拟环境实际文件所在路径
    6. --py 显示虚拟环境Python解释器所在路径
    7. --envs 显示虚拟环境的选项变量
    8. --rm 删除虚拟环境
    9. --bare 最小化输出
    10. --completion 完整输出
    11. --man 显示帮助页面
    12. --three / --two 使用Python 3/2创建虚拟环境(注意本机已安装的Python版本)
    13. --python TEXT 指定某个Python版本作为虚拟环境的安装源
    14. --site-packages 附带安装原Python解释器中的第三方库
    15. --jumbotron An easter egg, effectively.
    16. --version 版本信息
    17. -h, --help 帮助信息

    pipenv可使用的命令参数:

    1. Commands:
    2. check 检查安全漏洞
    3. graph 显示当前依赖关系图信息
    4. install 安装虚拟环境或者第三方库
    5. lock 锁定并生成Pipfile.lock文件
    6. open 在编辑器中查看一个库
    7. run 在虚拟环境中运行命令
    8. shell 进入虚拟环境
    9. uninstall 卸载一个库
    10. update 卸载当前所有的包,并安装它们的最新版本

    六、在线练习

    Pipenv playground是官方的在线练习网站:https://rootnroll.com/d/pipenv/,非常棒!
    虚拟环境的使用 - 图1
    [文章参考]
    快速入门Python 最新最流行的pipenv虚拟环境
    Pipenv: Python Dev Workflow for Humans