温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用Python Pip的技巧有哪些

发布时间:2023-04-13 15:23:01 来源:亿速云 阅读:111 作者:iii 栏目:编程语言

这篇文章主要介绍了使用Python Pip的技巧有哪些的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇使用Python Pip的技巧有哪些文章都会有所收获,下面我们一起来看看吧。

1.安装 pip

从 Python 3.4 开始,pip 已经内置在 Python 中,因此无需再次安装。

如果你的 Python 版本没有 pip,可以使用以下两种方法安装它。

  • 在命令行输入 easy_install pip,非常迅速。

  • 从以下网址下载 pip 安装文件,然后将其提取到 Python 脚本目录,并执行 python setup.py install 命令。

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
PackageVersion Latest Type
---------- ------- ------ -----
docutils 0.15.20.18.1 wheel
PyYAML 5.4.1 6.0wheel
rsa4.7.2 4.8wheel
setuptools 56.0.062.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

关于“使用Python Pip的技巧有哪些”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“使用Python Pip的技巧有哪些”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI