PIP 是 Python 的包安裝程序。用於安裝來自 Python 包索引 (PyPI)等索引的包。
Python 包索引
PyPI 是 Python 社區的 Python 包的默認存儲庫,包括框架、工具和庫。Python 開發人員可以在 Python 應用中安裝和使用包。所有 Python 開發人員都可以使用和分發他們的發行版。 開發者可以搜索或瀏覽來自 pypi.org 的項目。
安裝 pip
從 Python 3.4 開始,Python 安裝程序中就包含了 PIP。您可以通過在控制台中運行以下命令來驗證 pip 是否安裝在您的計算機上:
C:\>pip --version
pip 18.0 from c:\python37\lib\site-packages\pip (python 3.7)
如果您使用的是舊版本的 pip,可以通過在 Windows 上運行以下命令來升級 pip:
C:\> python -m pip install -U pip
在 Linux 或 Mac OS 上執行以下命令來升級 pip:
$ pip install -U pip
如果 pip 尚未安裝,則首先通過在控制台或終端窗口中執行以下命令,嘗試從標準庫中引導它:
python -m ensurepip --default-pip
如果仍然沒有安裝 pip,下面的步驟應該會在您的平台上安裝 pip。
- 從https://bootstrap.pypa.io/get-pip.py下載
get-pip.py
保存到本地文件夾。 - 導航命令提示符或終端到您下載文件的文件夾,運行命令:
python get-pip.py
這個命令將在你的電腦上安裝 PIP。此外,它還安裝了輪和設置工具。
pip 幫助命令
pip help
命令用於獲取 pip 中所有可用功能的列表。
C:\> pip help
Usage:
pip <command> [options]
Commands:
install Install packages.
download Download packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
list List installed packages.
show Show information about installed packages.
check Verify installed packages have compatible dependen cies.
config Manage local and global configuration.
search Search PyPI for packages.
cache Inspect and manage pip's wheel cache.
wheel Build wheels from your requirements.
hash Compute hashes of package archives.
completion A helper command used for command completion.
debug Show information useful for debugging.
help Show help for commands.
General Options:
-h, --help Show help.
--isolated Run pip in an isolated mode, ignoring
environment variables and user configuration.
-v, --verbose Give more output. Option is additive, and can be
used up to 3 times.
-V, --version Show version and exit.
-q, --quiet Give less output. Option is additive, and can be
used up to 3 times (corresponding to WARNING,
ERROR, and CRITICAL logging levels).
--log <path > Path to a verbose appending log.
--no-input Disable prompting for input.
--proxy <proxy > Specify a proxy in the form [user:[[email protected]](/cdn-cgi/l/email-protection)]proxy.server:port.
--retries <retries > Maximum number of retries each connection should attempt (default 5 times).
--timeout <sec > Set the socket timeout (default 15 seconds).
--exists-action <action > Default action when a path already exists:
(s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.
--trusted-host <hostname > Mark this host or host:port pair as trusted,
even though it does not have valid or any HTTPS.
--cert <path > Path to alternate CA bundle.
--client-cert <path > Path to SSL client certificate, a single file
containing the private key and the certificate
in PEM format.
--cache-dir <dir > Store the cache data in <dir >.
--no-cache-dir Disable the cache.
--disable-pip-version-check
Don't periodically check PyPI to determine
whether a new version of pip is available for
download. Implied with --no-index.
--no-color Suppress colored output
--no-python-version-warning
Silence deprecation warnings for upcoming
unsupported Pythons.
--use-feature <feature > Enable new functionality, that may be backward
incompatible.
--use-deprecated <feature > Enable deprecated functionality, that will be
removed in the future.
安裝包
PyPI 將包維護為項目。使用以下 pip 命令安裝項目的最新版本。
Pip install "project-name"
使用以下命令安裝項目的特定版本:
pip install "project-name==2.4"
使用以下命令安裝與特定版本「兼容」的版本:
pip install "project-name~=2.4"
讓我們安裝一個包,用 Python 發送一個 HTTP 請求。urllib3 是一個功能強大、用戶友好的 Python HTTP 客戶端。在應用中使用 urllib3 包之前,請使用pip
命令安裝它,如下所示。
C:\MyProject> Pip install urllib3
以上命令將安裝最新版本的urllib3
。現在,您可以導入並使用它,如下所示。
Example: Use urllib3 Package
import urllib3
http = urllib3.PoolManager()
req = http.request('GET', 'http://www.google.com')
print(req.status)
列出包
list 命令可用於查看系統上已安裝的所有包。如果您想檢查所有包裝,使用pip list
命令:
C:\> pip list
Package Version
------- -------
argon2-cffi 20.1.0
async-generator 1.10
attrs 20.2.0
backcall 0.1.0
bleach 3.2.1
certifi 2020.6.20
cffi 1.14.3
chardet 3.0.4
colorama 0.3.9
decorator 4.3.0
defusedxml 0.6.0
entrypoints 0.3
idna 2.10
importlib-metadata 2.0.0
ipykernel 5.3.4
ipython 6.5.0
ipython-genutils 0.2.0
jedi 0.12.1
Jinja2 2.11.2
json5 0.9.5
jsonschema 3.2.0
jupyter-client 6.1.7
jupyter-core 4.6.3
jupyterlab-pygments 0.1.2
jupyterlab-server 1.2.0
MarkupSafe 1.1.1
mistune 0.8.4
mongobox 0.1.8
nbclient 0.5.0
nbconvert 6.0.7
nbformat 5.0.7
nest-asyncio 1.4.1
notebook 6.1.4
packaging 20.4
pandocfilters 1.4.2
parso 0.3.1
pickleshare 0.7.4
pip 20.2.4
prometheus-client 0.8.0
prompt-toolkit 1.0.15
pycparser 2.20
Pygments 2.7.1
pyparsing 2.4.7
pyrsistent 0.17.3
python-dateutil 2.8.1
pywin32 228
pywinpty 0.5.7
pyzmq 19.0.2
requests 2.24.0
Send2Trash 1.5.0
setuptools 39.0.1
simplegeneric 0.8.1
six 1.11.0
terminado 0.9.1
testpath 0.4.4
tornado 6.0.4
traitlets 4.3.2
urllib3 1.26.2
wcwidth 0.1.7
webencodings 0.5.1
zipp 3.3.0
這將列出系統中可用的所有包。注意urllib3
包也列在那裡。
顯示包
如果要檢查包的元數據,請使用 pip show 命令。以下命令將顯示 urllib3 包的元數據。
C:\> pip show urllib3
Name: urllib3
Version: 1.26.2
Summary: HTTP library with thread-safe connection pooling, file post, and more.
Home-page: https://urllib3.readthedocs.io
Author: Andrey Petrov
Author-email: [[email protected]](/cdn-cgi/l/email-protection)
License: MIT
Location: c:\python37\lib\site-packages
Requires:
Required-by: requests
卸載包
pip uninstall
命令可用於移除包裹。例如,如果您想刪除urllib3
包,您可以簡單地使用以下命令:
$ pip uninstall urllib3
pip 包管理器將要求您確認是否要卸載該包。Proceed (y/n)?:
如果按 y,包裹將被移除。
因此,您可以使用 pip 作為 Python 應用的包管理器。*
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/278307.html