網站首頁 編程語言 正文
眾所周知,pip
?可以安裝、更新、卸載 Python 的第三方庫,非常方便。你們中的許多人可能已經使用?pip
?很長時間了,但不清楚它有哪些還不錯的功能。希望我今天分享的技巧能讓你從 Python?pip
?中受益。
Python pip
讓我們從 Python 語言開始。Python 之所以受歡迎,不僅因為它易于學習,還因為它擁有成千上萬的(寶)庫。
這些庫相當于已經集成的工具,只要安裝了就可以在 Python 中使用。它們可以處理各種各樣的問題,而無需你重新造輪子,而且隨著社區的不斷更新和維護,一些庫越來越強大,幾乎可以與企業級應用程序媲美。
那么如何下載安裝這些工具庫呢?它們被放置在一個名為?PyPi
(Python 包索引)的統一“倉庫”中,所有庫安裝都會來源于該倉庫。
有了倉庫之后,還需要一個管理員,pip
?就是這樣一個角色。pip
?從?PyPi
?中取出庫并將其安裝到 Python 中。它還可以管理已安裝的庫,如更新、查看、搜索、卸載等。
下面總結了 10 個使用 pip 的常識和技巧,供大家參考。
1.安裝 pip
從 Python 3.4 開始,pip
?已經內置在 Python 中,因此無需再次安裝。
如果你的 Python 版本沒有?pip
,可以使用以下兩種方法安裝它。
- 在命令行輸入?
easy_install pip
,非常迅速。 - 從以下網址下載?
pip
?安裝文件,然后將其提取到 Python 腳本目錄,并執行?python setup.py install
?命令。
pip
?的下載地址:https://pypi.org/project/pip/#files
但是,如果您還在使用 Python3.4 及更早版本,請升級到 Python 的最新穩定版本(https://www.python.org/downloads/)。否則,您每天都會增加更多的技術債務。
2.升級 pip
如果?pip
?的版本太低,可以升級當前版本: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
?安裝第三方庫,可以執行如下語句:pip install package_name
指定包版本:pip install package_name==1.1.2
比如,我要安裝 3.4.1 版本的?matplotlib
:pip install matplotlib==3.4.1
4. 庫的批量安裝
如果一個項目需要安裝很多庫,可以批量安裝:pip install -r requirements.txt
文件的內容格式如下:
#?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
當前庫的版本升級:
$?pip?install?--upgrade?package_name
或
$?pip?install?-U?package_name
6. 凍結 Python pip 依賴
有時您想輸出當前環境中所有已安裝的包,或生成一個需求文件,然后通過該文件在另一個環境中進行安裝。您可以使用?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
請注意,包會以排序順序列出(不區分大小寫)。如果您只想列出非全局安裝的軟件包,請使用?-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:?xi@resolvent.net 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.查看需要升級的庫
在當前安裝的庫中,查看有哪些庫需要進行版本升級:
$?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. 檢查兼容性問題
驗證已安裝的庫的兼容性依賴,你可以使用?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
原文鏈接:https://mp.weixin.qq.com/s/AREc4P8fj0X4S4TS_g89_w
相關推薦
- 2022-11-02 Golang實現多存儲驅動設計SDK案例_Golang
- 2024-03-24 MyBatis-Plus QueryWrapper及LambdaQueryWrapper的使用
- 2022-12-14 C++?容器中map和unordered?map區別詳解_C 語言
- 2023-07-02 Python配置文件管理之ini和yaml文件讀取的實現_python
- 2022-08-13 uni-app發送請求封裝
- 2022-06-01 Python實現圖像的二進制與base64互轉_python
- 2022-12-22 Python使用imagehash庫生成ahash算法的示例代碼_python
- 2022-07-28 超級詳細講解C++中的多態_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支