关于python:虚拟环境的创建和使用

116次阅读

共计 8572 个字符,预计需要花费 22 分钟才能阅读完成。

本篇笔记内容全副摘自慕课网 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

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

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

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

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

三、Pipfile 和 Pipfile.lock

  • Pipfile, 扼要地显示我的项目环境和依赖包
[source] name = "pypi" url = "https://mirrors.aliyun.com/pypi/simple/"  # 指定国内 pip 源,不然会慢死人的 verify_ssl = true [dev-packages]  # 开发环境 [packages]  # 生产环境 requests = "*"  # * 示意最新版本 django = "*" [requires] python_version = "3.7"  # python 版本
  • Pipfile.lock, 具体记录环境依赖,并且利用了 Hash 算法保障了它残缺对应关系
{"_meta": { "hash": { "sha256": "d737a1ef2f8b591f86cd0fa6a8cb48918f742d60df2a394ebc3d23aff4748999"}, "pipfile-spec": 6, "requires": {"python_version": "3.7"}, "sources": [{ "name": "pypi", "url": "https://pypi.org/simple", "verify_ssl": true} ] }, "default": {"certifi": { "hashes": [ "sha256:47f9c83ef4c0c621eaef743f133f09fa8a74a9b75f037e8624f83bd1b6626cb7", "sha256:993f830721089fef441cdfeb4b2c8c9df86f0c63239f06bd025a76a7daddb033"], "version": "==2018.11.29" }, "chardet": {"hashes": [ "sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae", "sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691"], "version": "==3.0.4" }, "django": {"hashes": [ "sha256:a32c22af23634e1d11425574dce756098e015a165be02e4690179889b207c7a8", "sha256:d6393918da830530a9516bbbcbf7f1214c3d733738779f06b0f649f49cc698c3"], "index": "pypi", "version": "==2.1.5" }, "idna": {"hashes": [ "sha256:c357b3f628cf53ae2c4c05627ecc484553142ca23264e593d327bcde5e9c3407", "sha256:ea8b7f6188e6fa117537c3df7da9fc686d485087abf6ac197f9c46432f7e4a3c"], "version": "==2.8" }, "pytz": {"hashes": [ "sha256:32b0891edff07e28efe91284ed9c31e123d84bea3fd98e1f72be2508f43ef8d9", "sha256:d5f05e487007e29e03409f9398d074e158d920d36eb82eaf66fb1136b0c5374c"], "version": "==2018.9" }, "requests": {"hashes": [ "sha256:502a824f31acdacb3a35b6690b5fbf0bc41d63a24a45c4004352b0242707598e", "sha256:7bf2a778576d825600030a110f3c0e3e8edc51dfaafe1c146e39a2027784957b"], "index": "pypi", "version": "==2.21.0" }, "urllib3": {"hashes": [ "sha256:61bf29cada3fc2fbefad4fdf059ea4bd1b4a86d2b6d15e1c7c0b582b9752fe39", "sha256:de9529817c93f27c8ccbfead6985011db27bd0ddfcdb2d86f3f663385c6a9c22"], "version": "==1.24.1" } }, "develop": {}}

四、虚拟环境治理

4.1 创立虚拟环境

进入我的项目的文件夹,间接 pipenv install xxx 装置你须要的包,就会在以后文件夹下创立虚拟环境,生成 PipfilePipfile.lock

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

如果须要指定 python 版本的话

$ pipenv –three # 泛指 python 的版本 $ pipenv –python 3.7 # 或者,特指某个 python 版本 $ pipenv –python <path/to/python> # 或者,特指某个地位的 python

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

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

有几点须要留神:

  • 平时装置和卸载包的时候 不须要 先进入或退出虚拟环境,间接在我的项目文件下 pipenv install/uninstall xxx 就行了
  • 退出虚拟环境不要 deactivate,请应用exit,否则下次pipenv 进入的时候就会报如下错
[root@shiyanlou myproject]# pipenv shell Shell for UNKNOWN_VIRTUAL_ENVIRONMENT already activated. No action taken to avoid nested environments. [root@shiyanlou myproject]#
  • pipenv --rm并不会删除生成的 PipfilePipfile.lock

4.3 治理开发环境

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

$ pipenv install –dev pytest

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

$ pipenv run python manage.py runserver

这将应用以后虚拟环境关联的 Python 解释器,执行命令。或者简略的执行脚本:

$ pipenv run python your_script.py

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

$ alias prp=”pipenv run python”

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

[root@shiyanlou myproject]# pipenv lock Locking [dev-packages] dependencies… Locking [packages] dependencies… ✔ Success! Updated Pipfile.lock (748999)! [root@shiyanlou myproject]#

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

[root@shiyanlou myproject]# pipenv --where  # 定位我的项目门路 /root/myproject [root@shiyanlou myproject]# pipenv --venv  # 定位虚拟环境 /root/.local/share/virtualenvs/myproject-zKJX12a5 [root@shiyanlou myproject]# pipenv --py  # 定位 Python 解释器 /root/.local/share/virtualenvs/myproject-zKJX12a5/bin/python [root@shiyanlou myproject]# pipenv graph  # 显示依赖关系 atomicwrites==1.2.1 attrs==18.2.0 Django==2.1.5  - pytz [required: Any, installed: 2018.9] more-itertools==5.0.0  - six [required: >=1.0.0,<2.0.0, installed: 1.12.0] py==1.7.0 requests==2.21.0  - certifi [required: >=2017.4.17, installed: 2018.11.29] - chardet [required: >=3.0.2,<3.1.0, installed: 3.0.4] - idna [required: >=2.5,<2.9, installed: 2.8] - urllib3 [required: >=1.21.1,<1.25, installed: 1.24.1] [root@shiyanlou myproject]# pipenv uninstall --all  # 卸载所有包 Un-installing all [dev-packages] and [packages]… Found 12 installed package(s), purging… Uninstalling Django-2.1.5:  Successfully uninstalled Django-2.1.5 ... 略... Environment now purged and fresh! [root@shiyanlou myproject]#

4.4 有个大坑等你跳

pipenv有个毛病,lock不稳固而且工夫十分长,所以安装包的时候记得加上--skip-lock,在最初开发实现要提交到仓库的时候再pipenv lock。心愿未来的版本可能优化。

[root@shiyanlou myproject]# pipenv install django --skip-lock Installing django… ✔ Installation Succeeded Installing dependencies from Pipfile…  ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 2/2 — 00:00:02 [root@shiyanlou myproject]#

五、常用命令汇总

pipenv具备下列的选项:

$ pipenv Usage: pipenv [OPTIONS] COMMAND [ARGS]… Options: –where 显示我的项目文件所在门路 –venv 显示虚拟环境理论文件所在门路 –py 显示虚拟环境 Python 解释器所在门路 –envs 显示虚拟环境的选项变量 –rm 删除虚拟环境 –bare 最小化输入 –completion 残缺输入 –man 显示帮忙页面 –three / –two 应用 Python 3/ 2 创立虚拟环境(留神本机已装置的 Python 版本)–python TEXT 指定某个 Python 版本作为虚拟环境的装置源 –site-packages 附带装置原 Python 解释器中的第三方库 –jumbotron An easter egg, effectively. –version 版本信息 -h, –help 帮忙信息

pipenv可应用的命令参数:

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

六、在线练习

Pipenv playground 是官网的在线练习网站:https://rootnroll.com/d/pipenv/,十分棒!

[文章参考]

疾速入门 Python 最新最风行的 pipenv 虚拟环境

Pipenv: Python Dev Workflow for Humans

正文完
 0