新聞中心
Python pip
讓我們從 Python 語言開始。Python 之所以受歡迎,不僅因?yàn)樗子趯W(xué)習(xí),還因?yàn)樗鼡碛谐汕先f的(寶)庫。

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:空間域名、網(wǎng)頁空間、營銷軟件、網(wǎng)站建設(shè)、古丈網(wǎng)站維護(hù)、網(wǎng)站推廣。
這些庫相當(dāng)于已經(jīng)集成的工具,只要安裝了就可以在 Python 中使用。它們可以處理各種各樣的問題,而無需你重新造輪子,而且隨著社區(qū)的不斷更新和維護(hù),一些庫越來越強(qiáng)大,幾乎可以與企業(yè)級應(yīng)用程序媲美。
那么如何下載安裝這些工具庫呢?它們被放置在一個名為 PyPi(Python 包索引)的統(tǒng)一“倉庫”中,所有庫安裝都會來源于該倉庫。
有了倉庫之后,還需要一個管理員,pip 就是這樣一個角色。pip 從 PyPi 中取出庫并將其安裝到 Python 中。它還可以管理已安裝的庫,如更新、查看、搜索、卸載等。
下面總結(jié)了 10 個使用 pip 的常識和技巧,供大家參考。
1.安裝 pip
從 Python 3.4 開始,pip 已經(jīng)內(nèi)置在 Python 中,因此無需再次安裝。
如果你的 Python 版本沒有 pip,可以使用以下兩種方法安裝它。
- 在命令行輸入 easy_install pip,非常迅速。
- 從以下網(wǎng)址下載 pip 安裝文件,然后將其提取到 Python 腳本目錄,并執(zhí)行 python setup.py install 命令。
pip 的下載地址:https://pypi.org/project/pip/#files
但是,如果您還在使用 Python3.4 及更早版本,請升級到 Python 的最新穩(wěn)定版本(https://www.python.org/downloads/)。否則,您每天都會增加更多的技術(shù)債務(wù)。
2.升級 pip
如果 pip 的版本太低,可以升級當(dāng)前版本:pip install --upgrade pip 或 pip install -U pip。
$ pip install -U pip
Looking in indexes: https://pypi.python.org/simple
Requirement already satisfied: pip in ./test/lib/python3.8/site-packages (21.1.1)
Collecting pip
Using cached pip-22.0.4-py3-none-any.whl (2.1 MB)
Installing collected packages: pip
Attempting uninstall: pip
Found existing installation: pip 21.1.1
Uninstalling pip-21.1.1:
Successfully uninstalled pip-21.1.1
Successfully installed pip-22.0.4
3.安裝庫
使用 pip 安裝第三方庫,可以執(zhí)行如下語句:pip install package_name
指定包版本:pip install package_name==1.1.2
比如,我要安裝 3.4.1 版本的 matplotlib:pip install matplotlib==3.4.1
4. 庫的批量安裝
如果一個項(xiàng)目需要安裝很多庫,可以批量安裝:pip install -r requirements.txt
文件的內(nèi)容格式如下:
# This is a comment
# Specify a diffrent index
-i http://dist.repoze.org/zope2/2.10/simple
# Package with versions
tensorflow==2.3.1
uvicorn==0.12.2
fastapi==0.63.0
pkg1
pkg2
pkg3>=1.0,<=2.0
# It is possible to refer to specific local distribution paths.
./downloads/numpy-1.9.2-cp34-none-win32.whl
# It is possible to refer to other requirement files or constraints files.
-r other-requirements.txt
-c constraints.txt
# It is possible to specify requirements as plain names.
pytest
pytest-cov
beautifulsoup4
5.卸載和升級包
已安裝的庫可以再次卸載:$ pip uninstall package_name
當(dāng)前庫的版本升級:
$ pip install --upgrade package_name
或
$ pip install -U package_name
6. 凍結(jié) Python pip 依賴
有時您想輸出當(dāng)前環(huán)境中所有已安裝的包,或生成一個需求文件,然后通過該文件在另一個環(huán)境中進(jìn)行安裝。您可以使用 pip freeze 命令:
# List packages
$ pip freeze
docutils==0.11
Jinja2==2.7.2
MarkupSafe==0.19
Pygments==1.6
Sphinx==1.2.2
# Generate requirements.txt file
$ pip freeze > requirements.txt
請注意,包會以排序順序列出(不區(qū)分大小寫)。如果您只想列出非全局安裝的軟件包,請使用 -l/--local。
7.查看庫信息
您可以使用 pip show -f package_name 列出包信息:
$ pip show -f pyyaml
Name: PyYAML
Version: 5.4.1
Summary: YAML parser and emitter for Python
Home-page: https://pyyaml.org/
Author: Kirill Simonov
Author-email: [email protected]
License: MIT
Location: /private/tmp/test/lib/python3.8/site-packages
Requires:
Required-by: awscli
Files:
PyYAML-5.4.1.dist-info/INSTALLER
PyYAML-5.4.1.dist-info/LICENSE
PyYAML-5.4.1.dist-info/METADATA
PyYAML-5.4.1.dist-info/RECORD
PyYAML-5.4.1.dist-info/WHEEL
PyYAML-5.4.1.dist-info/top_level.txt
...
8.查看需要升級的庫
在當(dāng)前安裝的庫中,查看有哪些庫需要進(jìn)行版本升級:
$ pip list -o
Package Version Latest Type
---------- ------- ------ -----
docutils 0.15.2 0.18.1 wheel
PyYAML 5.4.1 6.0 wheel
rsa 4.7.2 4.8 wheel
setuptools 56.0.0 62.1.0 wheel
9. 檢查兼容性問題
驗(yàn)證已安裝的庫的兼容性依賴,你可以使用 pip check package-name:
$ pip check awscli
No broken requirements found.
如果您不指定包名稱,將檢查所有包的兼容性。
$ pip check
pyramid 1.5.2 requires WebOb, which is not installed.
10. 將庫下載到本地
將庫下載到本地的指定位置并以 whl 格式保存:pip download package_name -d "path"
$ pip download PyYAML -d "/tmp/"
Looking in indexes: https://pypi.python.org/simple
Collecting PyYAML
Downloading PyYAML-6.0-cp38-cp38-macosx_10_9_x86_64.whl (192 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 192.2/192.2 KB 4.7 MB/s eta 0:00:00
Saved ./PyYAML-6.0-cp38-cp38-macosx_10_9_x86_64.whl
Successfully downloaded PyYAML
$ ls /tmp/PyYAML-6.0-cp38-cp38-macosx_10_9_x86_64.whl
/tmp/PyYAML-6.0-cp38-cp38-macosx_10_9_x86_64.whl
文章名稱:使用PythonPip的十個技巧
文章地址:http://m.fisionsoft.com.cn/article/dpjcged.html


咨詢
建站咨詢
