百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程字典 > 正文

关于Python虚拟环境与包管理你应该知道的事

toyiye 2024-09-07 20:34 3 浏览 0 评论

关于我

一个有思想的程序猿,终身学习实践者,目前在一个创业团队任team lead,技术栈涉及Android、Python、Java和Go,这个也是我们团队的主要技术栈。

Github:https://github.com/hylinux1024

微信公众号:终身开发者(angrycode)

Python拥有大量的第三方库,引用这些库也非常方便,通过pip install就可以将这些第三方库安装到本地Python库文件目录中,然后就可以import到项目中,极大地提升了开发者的编码效率。

但这也带来了一个问题:当A项目和B项目同时引用Lib库,而A项目需要Lib版本是1.0,B项目需要Lib的版本是2.0。这样在使用pip install命令将Lib直接安装到本地全局环境中就会发生冲突,有可能会导致A和B的运行环境无法同时得到满足而运行失败。

于是虚拟环境(virtualenv)就出现了。它的核心思想就是为每个项目提供独立的运行环境,这样不同的项目依赖库就不会冲突。

0x00 使用venv+pip

1、创建venv

安装虚拟环境也非常简单,可以使用venv模块,例如在项目目录中使用Python3创建一个虚拟环境

 ? python3 -m venv venv

于是在项目目录中就多了一个venv的文件目录。这个目录就是该项目的虚拟环境。

要使用虚拟环境就必须激活

 ? source venv/bin/activate

然后在命令行中就会出现

(venv) ?

说明虚拟环境已经激活。

要取消虚拟环境,使用

(venv) ? deactivate

2、使用pip install/uninstall

激活虚拟环境后就可以使用pip命令安装项目的依赖库。例如

(venv) ? pip install requests

pip会将requests安装到venv/lib/python3.7/site-packages目录中。

要卸载依赖库

(venv) ? pip uninstall requests

3、pip list

要查看venv中安装了哪些依赖库

(venv) ? pip list
Package Version 
-------------- --------
beautifulsoup4 4.7.1 
certifi 2019.3.9
requests 2.21.0

4、pip freeze

使用pip freeze可以将依赖库列表保存到一个requirements.txt文件,可以让其他项目的协作者可以快速地建立项目的运行环境。

(venv) ? pip freeze > requirements.txt

于是要安装项目依赖

(venv) ? pip install -r requirements.txt

这样就可以建立一个统一的运行环境了。

5、venv+pip方案存在的问题

到目前为止一切都运行良好,这应该是不错的解决方案了吧。实际上这种方案我一直都在使用,目前也是,也并没有遇到什么问题。

直到有一天有人说使用venv+pip也不能保证我的运行环境的一致性和可靠性。

他的理由也很简单,项目的库环境是依赖requirements.txt,而requirements.txt中库有没有指定版本号,这样在使用pip install -r requirements.txt的时候也会导致安装不确定的版本。

例如

beautifulsoup4 
certifi 
requests

当然可以为每个库指定确切的版号来解决,于是

beautifulsoup4 4.7.1 
certifi 2019.3.9
requests 2.21.0

这样做可以解决上面的问题,但是如果某个第三方库修补了一个漏洞,要使用pip install --upgrade更新这些依赖库的话,就不是那么容易了。

还有一个问题requirements.txt中的依赖库也有可能会出现版本号冲突。 情况是这样:

ALib -> sublib_1.0
BLib -> sublib_2.1

ALib和BLib同时依赖于sublib,但它们依赖的版本不一样,最终在使用pip install -r requirements.txt也可能会因为依赖库中的子依赖库版本不兼容而导致项目运行失败。

于是就出现了pipenv

0x01 pipenv

说实话在这之前我一直都是使用venv+pip,当看到上面的问题之后,我觉得有必要了解一下pipenv。

1、安装pipenv

? pip install pipenv

一旦安装完成就会引用两个文件,Pipfile和Pipfile.lock。前者是用于取代requirements.txt文件的,后者则是用于保证依赖库的确定性和一致性。

实际上,pipenv命令底层也是封装了pip和venv的操作,并提供了一套简单的交互命令。

创建虚拟环境

要在项目目录下(如pipenvdemo)使用

? pipenv shell

当出现类似以下信息时说明虚拟环境创建并激活成功,当然还可以使用--two或--three参数指定使用Python2还是Python3来创建,也可以使用确定的Python版本号如--python3.7

? pipenv shell --three
? pipenv shell --python3.7

由于我电脑本地Python环境是3.7,所以这里直接默认它会指向本地默认版本

Creating a virtualenv for this project…
Pipfile: /Users/mac/PycharmProjects/pipenvdemo/Pipfile
Using /usr/local/opt/python/bin/python3.7 (3.7.3) to create virtualenv…
? Creating virtual environment...Already using interpreter /usr/local/opt/python/bin/python3.7
Using base prefix '/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7'
New python executable in /Users/mac/.local/share/virtualenvs/pipenvdemo-fHPp2Wq9/bin/python3.7
Also creating executable in /Users/mac/.local/share/virtualenvs/pipenvdemo-fHPp2Wq9/bin/python
Installing setuptools, pip, wheel...
done.
? Successfully created virtual environment!
Virtualenv location: /Users/mac/.local/share/virtualenvs/pipenvdemo-fHPp2Wq9
Creating a Pipfile for this project…
Launching subshell in virtual environment…
 . /Users/mac/.local/share/virtualenvs/pipenvdemo-fHPp2Wq9/bin/activate

同时在项目目录下(如pipenvdemo)生成一个Pipfile文件。类似的,命令行也会出现以下样式

(pipenvdemo) ? pipenvdemo

其中Pipfile的内容为

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
[requires]
python_version = "3.7"

2、pipenv install

当要安装第三方库当时候就直接使用pipenv install命令,以下指定flask版本号进行安装。

? pipenv install flask==1.0.1

也可以不指定版本号

? pipenv install flask

终端会出现类似以下信息

Installing flask==1.0.1…
Adding flask to Pipfile's [packages]…
? Installation Succeeded
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
? Locking...

还可以使用--dev参数用于安装开发环境依赖的库

? pipenv install pytest --dev

在项目目录又生成了另一文件Pipfile.lock,并且Pipfile文件也会更新

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
pytest = "*"
[packages]
flask = "==1.0.1"
[requires]
python_version = "3.7"

看到在[packages]标签下列出了依赖库flask和版本号,上面还有一个[dev-packages]是用于标示开发版本依赖库,这样可以用于区分生产环境和开发环境。只有在命令中使用了--dev参数才会安装[dev-packages]下列出的依赖库。

Pipfile.lock文件的内容要丰富一些,主要是包含了依赖库(包括子依赖库)的版本号以及文件hash的信息,从而可以保证依赖库是确定的。

{
 "_meta": {
 "hash": {
 "sha256": "3bba1f1c4de8dd6f8d132dda17cd3c720372a1eed94b9523b9c23013e951c8ae"
 },
 "pipfile-spec": 6,
 "requires": {
 "python_version": "3.7"
 },
 "sources": [
 {
 "name": "pypi",
 "url": "https://pypi.org/simple",
 "verify_ssl": true
 }
 ]
 },
 "default": {
 "click": {
 "hashes": [
 "sha256:2335065e6395b9e67ca716de5f7526736bfa6ceead690adf616d925bdc622b13",
 "sha256:5b94b49521f6456670fdb30cd82a4eca9412788a93fa6dd6df72c94d5a8ff2d7"
 ],
 "version": "==7.0"
 },
 "flask": {
 "hashes": [
 "sha256:cfc15b45622f9cfee6b5803723070fd0f489b3bd662179195e702cb95fd924c8",
 "sha256:dbe2a9f539f4d0fe26fa44c08d6e556e2a4a4dd3a3fb0550f39954cf57571363"
 ],
 "index": "pypi",
 "version": "==1.0.1"
 },
 "itsdangerous": {
 "hashes": [
 "sha256:321b033d07f2a4136d3ec762eac9f16a10ccd60f53c0c91af90217ace7ba1f19",
 "sha256:b12271b2047cb23eeb98c8b5622e2e5c5e9abd9784a153e9d8ef9cb4dd09d749"
 ],
 "version": "==1.1.0"
 },
 "jinja2": {
 "hashes": [
 "sha256:065c4f02ebe7f7cf559e49ee5a95fb800a9e4528727aec6f24402a5374c65013",
 "sha256:14dd6caf1527abb21f08f86c784eac40853ba93edb79552aa1e4b8aef1b61c7b"
 ],
 "version": "==2.10.1"
 },
 "markupsafe": {
 "hashes": [
 "sha256:00bc623926325b26bb9605ae9eae8a215691f33cae5df11ca5424f06f2d1f473",
 "sha256:09027a7803a62ca78792ad89403b1b7a73a01c8cb65909cd876f7fcebd79b161",
 "sha256:09c4b7f37d6c648cb13f9230d847adf22f8171b1ccc4d5682398e77f40309235",
 "sha256:1027c282dad077d0bae18be6794e6b6b8c91d58ed8a8d89a89d59693b9131db5",
 "sha256:24982cc2533820871eba85ba648cd53d8623687ff11cbb805be4ff7b4c971aff",
 "sha256:29872e92839765e546828bb7754a68c418d927cd064fd4708fab9fe9c8bb116b",
 "sha256:43a55c2930bbc139570ac2452adf3d70cdbb3cfe5912c71cdce1c2c6bbd9c5d1",
 "sha256:46c99d2de99945ec5cb54f23c8cd5689f6d7177305ebff350a58ce5f8de1669e",
 "sha256:500d4957e52ddc3351cabf489e79c91c17f6e0899158447047588650b5e69183",
 "sha256:535f6fc4d397c1563d08b88e485c3496cf5784e927af890fb3c3aac7f933ec66",
 "sha256:62fe6c95e3ec8a7fad637b7f3d372c15ec1caa01ab47926cfdf7a75b40e0eac1",
 "sha256:6dd73240d2af64df90aa7c4e7481e23825ea70af4b4922f8ede5b9e35f78a3b1",
 "sha256:717ba8fe3ae9cc0006d7c451f0bb265ee07739daf76355d06366154ee68d221e",
 "sha256:79855e1c5b8da654cf486b830bd42c06e8780cea587384cf6545b7d9ac013a0b",
 "sha256:7c1699dfe0cf8ff607dbdcc1e9b9af1755371f92a68f706051cc8c37d447c905",
 "sha256:88e5fcfb52ee7b911e8bb6d6aa2fd21fbecc674eadd44118a9cc3863f938e735",
 "sha256:8defac2f2ccd6805ebf65f5eeb132adcf2ab57aa11fdf4c0dd5169a004710e7d",
 "sha256:98c7086708b163d425c67c7a91bad6e466bb99d797aa64f965e9d25c12111a5e",
 "sha256:9add70b36c5666a2ed02b43b335fe19002ee5235efd4b8a89bfcf9005bebac0d",
 "sha256:9bf40443012702a1d2070043cb6291650a0841ece432556f784f004937f0f32c",
 "sha256:ade5e387d2ad0d7ebf59146cc00c8044acbd863725f887353a10df825fc8ae21",
 "sha256:b00c1de48212e4cc9603895652c5c410df699856a2853135b3967591e4beebc2",
 "sha256:b1282f8c00509d99fef04d8ba936b156d419be841854fe901d8ae224c59f0be5",
 "sha256:b2051432115498d3562c084a49bba65d97cf251f5a331c64a12ee7e04dacc51b",
 "sha256:ba59edeaa2fc6114428f1637ffff42da1e311e29382d81b339c1817d37ec93c6",
 "sha256:c8716a48d94b06bb3b2524c2b77e055fb313aeb4ea620c8dd03a105574ba704f",
 "sha256:cd5df75523866410809ca100dc9681e301e3c27567cf498077e8551b6d20e42f",
 "sha256:e249096428b3ae81b08327a63a485ad0878de3fb939049038579ac0ef61e17e7"
 ],
 "version": "==1.1.1"
 },
 "werkzeug": {
 "hashes": [
 "sha256:865856ebb55c4dcd0630cdd8f3331a1847a819dda7e8c750d3db6f2aa6c0209c",
 "sha256:a0b915f0815982fb2a09161cb8f31708052d0951c3ba433ccc5e1aa276507ca6"
 ],
 "version": "==0.15.4"
 }
 },
 "develop": {}
}

3、pipenv lock

假设我们要把项目发布到生产环境了,这时就要使用pipenv lock命令

? pipenv lock

终端会出现类似如下信息

Locking [dev-packages] dependencies…
? Success!
Locking [packages] dependencies…

这个命令会创建或更新Pipfile.lock文件,需要注意的是我们不应该手动修改此文件。

然后就可以在生产环境中使用以下命令恢复环境

? pipenv install --ignore-pipfile

指定--ignore-pipfile参数的意思是,只要恢复Pipfile.lock的列表的依赖库和子依赖库。如果要恢复开发环境中的依赖库,即安装[dev-packages]下面的依赖库,可以使用

? pipenv install --dev

以上就是pipenv的简单用法。

4、pipenv graph

现在再来看看前面提到的问题

ALib -> sublib_1.0
BLib -> sublib_2.1

A和B模块都依赖同一个库,但依赖库的版本号不一样。这时使用pipenv install就出现类似以下信息

Warning: Your dependencies could not be resolved. You likely have a mismatch in your sub-dependencies.
 You can use $ pipenv install --skip-lock to bypass this mechanism, then run $ pipenv graph to inspect the situation.
Could not find a version that matches sublib=1.0,sublib=2.1

可以使用

? pipenv graph

查看当前项目依赖库及其子依赖库信息,以树层级方式进行展示

Flask==1.0.1
 - click [required: >=5.1, installed: 7.0]
 - itsdangerous [required: >=0.24, installed: 1.1.0]
 - Jinja2 [required: >=2.10, installed: 2.10.1]
 - MarkupSafe [required: >=0.23, installed: 1.1.1]
 - Werkzeug [required: >=0.14, installed: 0.15.4]
pytest==4.6.2
 - atomicwrites [required: >=1.0, installed: 1.3.0]
 - attrs [required: >=17.4.0, installed: 19.1.0]
 - importlib-metadata [required: >=0.12, installed: 0.17]
 - zipp [required: >=0.5, installed: 0.5.1]
 - more-itertools [required: >=4.0.0, installed: 7.0.0]
 - packaging [required: Any, installed: 19.0]
 - pyparsing [required: >=2.0.2, installed: 2.4.0]
 - six [required: Any, installed: 1.12.0]
 - pluggy [required: >=0.12,<1.0, installed: 0.12.0]
 - importlib-metadata [required: >=0.12, installed: 0.17]
 - zipp [required: >=0.5, installed: 0.5.1]
 - py [required: >=1.5.0, installed: 1.8.0]
 - six [required: >=1.10.0, installed: 1.12.0]
 - wcwidth [required: Any, installed: 0.1.7]

本例中我们安装了flask和pytest,pipenv graph命令展示了它们各自需要的依赖。还可以加上--reverse参数

? pipenv graph --reverse

使用这个参数可以很方便的分析出冲突的库。

atomicwrites==1.3.0
 - pytest==4.6.2 [requires: atomicwrites>=1.0]
attrs==19.1.0
 - pytest==4.6.2 [requires: attrs>=17.4.0]
click==7.0
 - Flask==1.0.1 [requires: click>=5.1]
itsdangerous==1.1.0
 - Flask==1.0.1 [requires: itsdangerous>=0.24]
MarkupSafe==1.1.1
 - Jinja2==2.10.1 [requires: MarkupSafe>=0.23]
 - Flask==1.0.1 [requires: Jinja2>=2.10]
more-itertools==7.0.0
 - pytest==4.6.2 [requires: more-itertools>=4.0.0]
pip==19.1.1
py==1.8.0
 - pytest==4.6.2 [requires: py>=1.5.0]
pyparsing==2.4.0
 - packaging==19.0 [requires: pyparsing>=2.0.2]
 - pytest==4.6.2 [requires: packaging]
setuptools==41.0.1
six==1.12.0
 - packaging==19.0 [requires: six]
 - pytest==4.6.2 [requires: packaging]
 - pytest==4.6.2 [requires: six>=1.10.0]
wcwidth==0.1.7
 - pytest==4.6.2 [requires: wcwidth]
Werkzeug==0.15.4
 - Flask==1.0.1 [requires: Werkzeug>=0.14]
wheel==0.33.4
zipp==0.5.1
 - importlib-metadata==0.17 [requires: zipp>=0.5]
 - pluggy==0.12.0 [requires: importlib-metadata>=0.12]
 - pytest==4.6.2 [requires: pluggy>=0.12,<1.0]
 - pytest==4.6.2 [requires: importlib-metadata>=0.12]

5、其它命令

删除一个依赖包

? pipenv uninstall numpy

删除所有依赖库

? pipenv uninstall --all
? pipenv uninstall --all-dev

--all-dev是指定删除所有开发环境的依赖。

查看venv目录路径

? pipenv --venv
/Users/mac/.local/share/virtualenvs/pipenvdemo-fHPp2Wq9

查看当前项目路径

? pipenv --where
/Users/mac/PycharmProjects/pipenvdemo

0x02 总结一下

从目前使用pip+venv的包管理工具遇到的问题出发,提到这种方式遇到的问题是依赖库以及它们的子依赖库又可能会出现版本冲突以及管理的问题。于是有人提出新的包管理工具pipenv。它有两个核心文件Pipfile和Pipfile.lock文件,前者用于指定当前项目中的直接依赖库信息,而后者则指定了依赖的依赖的库信息,通过依赖的hash值以及版本号来确定依赖库的一致性。

0x03 参考引用

  • https://docs.python.org/3/tutorial/venv.html
  • 虚拟环境与包管理官方文档
  • https://realpython.com/pipenv-guide/
  • A Guide to the New Python Packaging Tool
  • https://www.kennethreitz.org/essays/a-better-pip-workflow
  • pipenv作者关于使用pip与virtualenv问题的描述
  • https://packaging.python.org/tutorials/managing-dependencies/#managing-dependencies
  • 依赖库管理官方文档

相关推荐

# Python 3 # Python 3字典Dictionary(1)

Python3字典字典是另一种可变容器模型,且可存储任意类型对象。字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中,格式如...

Python第八课:数据类型中的字典及其函数与方法

Python3字典字典是另一种可变容器模型,且可存储任意类型对象。字典的每个键值...

Python中字典详解(python 中字典)

字典是Python中使用键进行索引的重要数据结构。它们是无序的项序列(键值对),这意味着顺序不被保留。键是不可变的。与列表一样,字典的值可以保存异构数据,即整数、浮点、字符串、NaN、布尔值、列表、数...

Python3.9又更新了:dict内置新功能,正式版十月见面

机器之心报道参与:一鸣、JaminPython3.8的热乎劲还没过去,Python就又双叒叕要更新了。近日,3.9版本的第四个alpha版已经开源。从文档中,我们可以看到官方透露的对dic...

Python3 基本数据类型详解(python三种基本数据类型)

文章来源:加米谷大数据Python中的变量不需要声明。每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建。在Python中,变量就是变量,它没有类型,我们所说的"类型"是变...

一文掌握Python的字典(python字典用法大全)

字典是Python中最强大、最灵活的内置数据结构之一。它们允许存储键值对,从而实现高效的数据检索、操作和组织。本文深入探讨了字典,涵盖了它们的创建、操作和高级用法,以帮助中级Python开发...

超级完整|Python字典详解(python字典的方法或操作)

一、字典概述01字典的格式Python字典是一种可变容器模型,且可存储任意类型对象,如字符串、数字、元组等其他容器模型。字典的每个键值key=>value对用冒号:分割,每个对之间用逗号,...

Python3.9版本新特性:字典合并操作的详细解读

处于测试阶段的Python3.9版本中有一个新特性:我们在使用Python字典时,将能够编写出更可读、更紧凑的代码啦!Python版本你现在使用哪种版本的Python?3.7分?3.5分?还是2.7...

python 自学,字典3(一些例子)(python字典有哪些基本操作)

例子11;如何批量复制字典里的内容2;如何批量修改字典的内容3;如何批量修改字典里某些指定的内容...

Python3.9中的字典合并和更新,几乎影响了所有Python程序员

全文共2837字,预计学习时长9分钟Python3.9正在积极开发,并计划于今年10月发布。2月26日,开发团队发布了alpha4版本。该版本引入了新的合并(|)和更新(|=)运算符,这个新特性几乎...

Python3大字典:《Python3自学速查手册.pdf》限时下载中

最近有人会想了,2022了,想学Python晚不晚,学习python有前途吗?IT行业行业薪资高,发展前景好,是很多求职群里严重的香饽饽,而要进入这个高薪行业,也不是那么轻而易举的,拿信工专业的大学生...

python学习——字典(python字典基本操作)

字典Python的字典数据类型是基于hash散列算法实现的,采用键值对(key:value)的形式,根据key的值计算value的地址,具有非常快的查取和插入速度。但它是无序的,包含的元素个数不限,值...

324页清华教授撰写【Python 3 菜鸟查询手册】火了,小白入门字典

如何入门学习python...

Python3.9中的字典合并和更新,了解一下

全文共2837字,预计学习时长9分钟Python3.9正在积极开发,并计划于今年10月发布。2月26日,开发团队发布了alpha4版本。该版本引入了新的合并(|)和更新(|=)运算符,这个新特性几乎...

python3基础之字典(python中字典的基本操作)

字典和列表一样,也是python内置的一种数据结构。字典的结构如下图:列表用中括号[]把元素包起来,而字典是用大括号{}把元素包起来,只不过字典的每一个元素都包含键和值两部分。键和值是一一对应的...

取消回复欢迎 发表评论:

请填写验证码