目录

PIP

主要功能

安装包

升级包

卸载包

列出包

检查依赖

pip的配置和环境

主要用法

1:版本

2:安装 Python 库

3:升级库

4:卸载库

5:搜索库

6:查看已安装库详细信息

7:只下载库而不安装

8:创建requirements文件

9:指定库版本安装

10:检查是否存在安全漏洞

11:创建虚拟环境并激活

12:清理未使用的库或缓存

13:查看 pip 自身的版本信息及更新 pip

14:指定源安装库

15:分发本地构建的库

16:查看库安装路径

17:在安装时跳过测试

18:列出所有全局安装的库

19:在安装时指定额外选项

20:查看依赖树

21:修复损坏的库

22:获取库的源码

23:执行pip的自定义脚本

24:在安装时指定 Python 版本

25:检查依赖冲突

26:创建独立可执行文件

27:管理用户级别的包

28:只升级指定的包而不升级所有包

29:批量卸载多个包

30:冻结当前环境下的所有包及其版本

pip的未来

结论

PIP

Python作为一门流行的编程语言,拥有一个庞大的生态系统,其中包含了数以万计的库和框架,这些库和框架为Python程序员提供了强大的功能支持。在这样的背景下,一个高效、便捷的包管理工具就显得尤为重要。Python的包安装器(pip)就是这样一个工具,它允许用户轻松地安装和管理Python包。

pip是Python的官方包管理工具,其名称源自“Pip Installs Packages”。它最初由Julien Phalip在2008年创建,目的是为了简化Python包的安装过程。随着时间的推移,pip逐渐成为了Python社区的标配工具。

主要功能

安装包

用户可以通过pip安装来自Python包索引(PyPI)的包,或者从其他源安装。

升级包

pip可以升级已安装的包到最新版本。

卸载包

用户可以使用pip来卸载不再需要的包。

列出包

pip可以列出已安装的包及其版本信息。

检查依赖

pip能够检查包之间的依赖关系,并在安装时自动处理这些依赖。

pip的配置和环境

pip默认使用PyPI作为包的来源,但用户也可以配置pip使用其他的源,比如国内的阿里云、豆瓣等镜像源,这在某些情况下可以加快包的下载速度。此外,pip还支持虚拟环境的概念,这允许用户为不同的项目创建独立的Python环境,避免包版本冲突。通过使用venv模块,用户可以轻松创建和管理虚拟环境。

主要用法

Usage:   pip [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           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 to a verbose appending log.   --no-input                  Disable prompting for input.   --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             Specify a proxy in the form scheme://[user:passwd@]proxy.server:port.   --retries         Maximum number of retries each connection should attempt (default 5 times).   --timeout             Set the socket timeout (default 15 seconds).   --exists-action    Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup,                               (a)bort.   --trusted-host   Mark this host or host:port pair as trusted, even though it does not have valid or any                               HTTPS.   --cert               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 to SSL client certificate, a single file containing the private key and the                               certificate in PEM format.   --cache-dir

          Store the cache data in .   --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     Enable new functionality, that may be backward incompatible.   --use-deprecated  Enable deprecated functionality, that will be removed in the future.

1:版本

如果你是Python新手,很可能系统已经自带pip。验证是否已安装,只需在命令行输入:

python -m pip --version

若未安装,可参考官方指引进行安装。

C:\Users>pip --version pip 24.0 from D:\Program Files\Python\Lib\site-packages\pip (python 3.12)

2:安装 Python 库

pip install 库名

这就是pip最基础也是最常见的用法,它会自动处理库及其依赖关系。

3:升级库

世界日新月异,库也要保持最新状态。要升级已安装的所有库:

pip list --outdated # 查看待升级库

pip upgrade # 升级指定库

4:卸载库

不再需要某个库时,我们可以这样卸载:

pip uninstall

5:搜索库

在决定安装某个库之前,你可能想先了解下有哪些可用的库能满足你的需求。这时可以使用search命令:

pip search

比如查找与机器学习相关的库,你可以输入 pip search machine learning。

6:查看已安装库详细信息

想知道已安装的库具体版本和其他详情?show命令可以帮助你:

pip show

7:只下载库而不安装

有时你可能需要离线环境安装包或者备份当前环境的依赖,那么可以使用download命令只下载不安装:

pip download

8:创建requirements文件

在项目开发中,为了方便团队成员统一环境,我们可以创建一个包含所有依赖的requirements文件:

pip freeze > requirements.txt

这会列出当前环境中所有已安装库及其版本,并保存到requirements.txt文件中。而要根据这个文件安装所有依赖,只需:pip install -r requirements.txt

9:指定库版本安装

在某些情况下,你可能需要安装特定版本的库,比如安装 requests 库的2.25.1版本:

pip install requests==2.25.1

10:检查是否存在安全漏洞

pip 配合Safety工具可以检查已安装库的安全性:

pip install safety

safety check --full-report

这会扫描所有已安装的库,并报告是否有已知的安全漏洞。

11:创建虚拟环境并激活

为了避免不同项目间依赖冲突,我们通常会在每个项目下创建独立的虚拟环境,然后使用 pip 进行管理:

python -m venv my_project_env # 创建虚拟环境

source my_project_env/bin/activate # Linux/Mac激活环境

my_project_env\Scripts\activate.bat # Windows激活环境

在虚拟环境中,你可以放心使用 pip 安装和管理项目的专属依赖。

12:清理未使用的库或缓存

随着项目迭代,有些不再使用的库可能会被遗忘,这时可以使用autoremove命令来清除:

pip-autoremove

同时,你也可以清理 pip 下载缓存以释放磁盘空间:

pip cache purge

13:查看 pip 自身的版本信息及更新 pip

要查看 pip 本身的版本,只需运行:

pip --version

为了确保 pip 始终保持最新,可使用如下命令升级 pip 自身:

python -m pip install --upgrade pip

当然,pip 的功能确实丰富多样,让我们继续探索:

14:指定源安装库

在某些网络环境下,可能需要从国内镜像或者其他自定义源下载和安装库。例如使用阿里云的Python 镜像源:

pip install -i https://mirrors.aliyun.com/pypi/simple/

或永久更改 pip 默认源(推荐在配置文件中修改):

pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/

15:分发本地构建的库

如果你自己开发了一个 Python 包,并希望在本地测试安装,可以先打包成 whl 或 tar.gz格式,然后通过 pip 进行安装:

# 假设你已经将项目打包为my_package-0.1.0.whl pip install ./my_package-0.1.0.whl

16:查看库安装路径

如果想知道某个库具体安装在系统哪个位置,可以使用show --files选项:

pip show --files

17:在安装时跳过测试

有些库在安装过程中会执行单元测试,若想快速安装可选择跳过这些测试:

pip install --no-deps --ignore-installed --no-cache-dir --disable-pip-version-check --no-compile

18:列出所有全局安装的库

获取全局环境下的所有已安装Python库列表:

pip list

19:在安装时指定额外选项

某些库可能在安装时需要额外参数,比如numpy、scipy等科学计算库,你可以直接在pip命令中传递这些选项:

pip install numpy --install-option="--openblas" 这里的--openblas是一个示例,实际选项请根据库文档进行设置。

20:查看依赖树

要了解一个包及其所有依赖关系,可以使用deptree第三方工具:

pip installdeptree deptree -l 这将展示指定包及其所有依赖项之间的层级关系。

21:修复损坏的库

如果某个库在安装或升级过程中出现问题导致无法正常使用,可以尝试修复它:

pip install --force-reinstall

22:获取库的源码

如果你对某个库的实现细节感兴趣,可以通过pip下载其源代码:

pip download --no-binary :all: 然后在下载目录中找到对应的tar.gz或whl文件解压查看。

23:执行pip的自定义脚本

有些开发者可能会编写自定义的 pip 脚本来自动化一些任务,你可以通过run命令执行:

pip run my_script.py

24:在安装时指定 Python 版本

如果你有多个Python版本并希望为特定版本安装库:

python3.7 -m pip install 这里python3.7替换成你想要使用的Python解释器路径。

25:检查依赖冲突

在项目中可能存在不兼容的依赖版本,可以使用pipdeptree第三方工具来检测:

pip install pipdeptree pipdeptree --packages 这将展示指定包及其依赖项之间是否存在版本冲突。

26:创建独立可执行文件

借助pyinstaller等工具,你可以通过pip将Python程序打包成一个独立可执行文件:

pip install pyinstaller pyinstaller your_script.py

27:管理用户级别的包

默认情况下,pip 会安装全局系统级别的包。若想为当前用户安装不影响系统的包,可以加上--user选项:

pip install --user

28:只升级指定的包而不升级所有包

当只需要更新某个特定的库时,可以使用--upgrade选项:

pip install --upgrade

29:批量卸载多个包

如果需要一次性卸载多个Python包,可以通过列表形式提供包名:

pip uninstall ...

30:冻结当前环境下的所有包及其版本

为了能够复制或记录当前环境中所有已安装包的状态,可以生成一个 requirements 文件:

pip freeze > requirements.txt 这将把所有已安装包及其版本号写入requirements.txt文件中,便于在其他环境下复现相同的软件环境。

pip的未来

随着Python生态的不断发展,pip也在不断进化。例如,pip 20.2版本开始,官方不再支持Python 2.7和Python 3.4,这意味着pip将更加专注于支持Python 3.5及以上版本。

此外,pip社区也在积极开发新的功能,比如更好的依赖解析器,以及更强大的包管理能力,以满足日益增长的Python用户和开发者的需求。

结论

pip作为Python的官方包管理工具,对于Python开发者来说是一个不可或缺的工具。它简化了包的安装、升级和卸载过程,使得Python的生态系统更加易于使用和维护。随着Python的不断发展,pip也将继续进化,以更好地服务于Python社区。

相关文章

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: