Python-pip常见使用与离线安装包
本内容包括:
- pip命令行解释
- pip基础操作
- pip离线安装
一、PIP 的基础操作
pip
指令,进一步的细节可以使用 -h
参数查看Usage:
pip <command> [options]
Commands:
install Install packages.
download Download packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
inspect Inspect the python environment.
list List installed packages.
show Show information about installed packages.
check Verify installed packages have compatible dependencies.
config Manage local and global configuration.
search Search PyPI for packages.
cache Inspect and manage pip's wheel cache.
index Inspect information available from package indexes.
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.
--debug Let unhandled exceptions propagate outside the main subroutine, instead of logging them
to stderr.
--isolated Run pip in an isolated mode, ignoring environment variables and user configuration.
--require-virtualenv Allow pip to only run in a virtual environment; exit with an error otherwise.
--python <python> Run pip with the specified Python interpreter.
-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.
--keyring-provider <keyring_provider>
Enable the credential lookup via the keyring library if user input is allowed. Specify
which mechanism to use [disabled, import, subprocess]. (default: disabled)
--proxy <proxy> Specify a proxy in the form scheme://[user:passwd@]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 PEM-encoded CA certificate bundle. If provided, overrides the default. See 'SSL
Certificate Verification' in pip documentation for more information.
--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.
命令:
requirements.txt
格式。通用选项:
二、常用操作
pip install 包名
pip install -r requirements.txt
pip freeze > requirements.txt
pip install 包名 --proxy http://代理地址:端口
三、下载 whl 包用于离线安装
0. 介绍
WHL:,是把编译好的 Python 文件打包到了一起,有点类似 Java 的 jar 包,省去了编译过程,可以直接安装。
注意 1:下载 WHL 包的机器和离线安装的机器注意:Python 版本一致、机器位数要一致(比如文件名中的 cp311)、系统框架一致(一些包存在 x86 和 arm 不同版本)
注意 2: 在安装时会出现红色 ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts
经过测试程序依然可以运行,问题不大
1. 情景
在一些特定情况下,服务器只能离线安装 python 环境。拷贝虚拟环境和使用 docker 是一种解决方案,这里从下载 whl 包离线安装的角度出发解决这个问题。
下载环境的设备和目的环境设备应该一致,尽量保证 CPU 架构和系统都一致,否则会出现不兼容的问题,拷贝虚拟环境、whl 离线安装等都要注意这些问题!
2. 下载 whl 包
使用 pip download
指令下载 whl 包到本地
pip download -d spark-libs 包名
pip download -d <whl_folder> -r <requirements.txt>
--index-url
下载包:pip download -d <whl_folder> torch torchvision torchaudio --index-url <https://download.pytorch.org/whl/cu118>
【因为 requirements.txt 中的 torch 无法安装 cuda 因此使用 index-url 安装】
3. 安装 whl 包
使用 pip download
指令下载 whl 包到本地
pip install <whl_path>
pip install --no-index --find-links=d:\spark-libs -r requirements.txt
【测试时可以加入:
--no-index
防止 pip 从 pypi 中下载包】作者:Mars7823