Python教程:pip的使用技巧

pip 可以对 python 第三方库进行安装、更新、卸载等操作,十分方便。

全称为:package installer for python ,也就是 python 包管理工具。

python 各种各样功能的工具库被存放在一个统一的“仓库”里,名叫 PyPi (Python Package Index),所有的库安装都是从这里调度。

pip是一个命令行程序,一般都在命令行中执行各种操作。例如:在 Windows 下则是通过 cmd 执行。

1.安装pip

python 3.4 及以后,已经内置 pip 工具,无需安装。

# 方法一
easy_install pip

# 方法二
# 下载安装 python scripts目录
# 下载网址:https://pypi.org/project/pip/#files
python setup.py install

2.查看pip版本

pip --version
# pip 21.3.1 from d:\programfiles\anaconda\lib\site-packages\pip (python 3.6)

3.升级pip

pip install --upgrade pip

4.获取帮助

pip help

5.安装库

  • 默认:安装最新版本
pip install package_name
  • 指定版本
pip install package_name==1.1.1
# pip install matplotlib==3.4.1

6.批量安装库

pip install -r D:\\requirements.txt
'''
pickleshare==0.7.5
prompt-toolkit==3.0.18
'''

7.使用wheel文件安装库

离线安装, wheel 文件时库的源文件,可以下载后通过本地安装。

# 离线安装
# 1.找到对应的.whl文件
# https://www.lfd.uci.edu/~gohlke/pythonlibs/

# 2.下载对应的版本

# 3.同目录下cmd执行
pip install matplotlib‑3.4.1‑cp39‑cp39‑win_amd64.whl

8.卸载库

pip uninstall package_name

9.升级库

pip install --upgrade package_name

10.查看库信息

pip show -f package_name
'''
  pandas\tests\series\methods\__pycache__\test_isin.cpython-36.pyc
  pandas\tests\series\methods\__pycache__\test_nlargest.cpython-36.pyc
  pandas\tests\series\methods\__pycache__\test_pct_change.cpython-36.pyc
  pandas\tests\series\methods\__pycache__\test_quantile.cpython-36.pyc
  pandas\tests\series\methods\__pycache__\test_rank.cpython-36.pyc
  pandas\tests\series\methods\__pycache__\test_reindex_like.cpython-36.pyc
  pandas\tests\series\methods\__pycache__\test_rename.cpython-36.pyc
  pandas\tests\series\methods\__pycache__\test_rename_axis.cpython-36.pyc
  pandas\tests\series\methods\__pycache__\test_replace.cpython-36.pyc
  pandas\tests\series\methods\__pycache__\test_reset_index.cpython-36.pyc
  pandas\tests\series\methods\__pycache__\test_round.cpython-36.pyc
  pandas\tests\series\methods\__pycache__\test_searchsorted.cpython-36.pyc
  pandas\tests\series\methods\__pycache__\test_shift.cpython-36.pyc
  pandas\tests\series\methods\__pycache__\test_sort_index.cpython-36.pyc
  pandas\tests\series\methods\__pycache__\test_sort_values.cpython-36.pyc
'''

11.查看已安装的库

pip list
'''
C:\Users\Hider>pip list
Package                            Version
---------------------------------- ---------
alabaster                          0.7.10
anaconda-client                    1.6.9
anaconda-navigator                 1.7.0
anaconda-project                   0.8.2
asn1crypto                         0.24.0
astroid                            1.6.1
astropy                            2.0.3
attrs                              17.4.0
'''

12.保存库列表

pip freeze > requirements.txt
'''
C:\Users\Hider>pip freeze
alabaster==0.7.10
anaconda-client==1.6.9
anaconda-navigator==1.7.0
anaconda-project==0.8.2
asn1crypto==0.24.0
astroid==1.6.1
astropy==2.0.3
attrs==17.4.0
Babel==2.5.3
backports.shutil-get-terminal-size==1.0.0
'''

13.查看需要升级的库

pip list -o
# 比较慢 需要一个个核查
'''
C:\Users\Hider>pip list -o
Package                  Version   Latest    Type
------------------------ --------- --------- -----
alabaster                0.7.10    0.7.12    wheel
asn1crypto               0.24.0    1.4.0     wheel
astroid                  1.6.1     2.9.3     wheel
astropy                  2.0.3     4.1       wheel
attrs                    17.4.0    21.4.0    wheel
Babel                    2.5.3     2.9.1     wheel
beautifulsoup4           4.6.0     4.10.0    wheel
bitarray                 0.8.1     2.3.5     sdist
'''

14.检查兼容问题

`pip check package_name
'''
C:\Users\Hider>pip check pandas
notebook 5.4.0 requires ipykernel, which is not installed.
jupyter 1.0.0 requires ipykernel, which is not installed.
jupyter-console 5.2.0 requires ipykernel, which is not install
ipywidgets 7.1.1 requires ipykernel, which is not installed.
'''

15.下载库到本地

# 保存为whl格式
#学习中遇到问题没人解答?小编创建了一个Python学习交流群:725638078
pip download package_name -d '路径'
'''
C:\Users\Hider>pip download pandas -d "C:\Users\Hider"
Collecting pandas
  Using cached pandas-1.1.5-cp36-cp36m-win_amd64.whl (8.7 MB)
Collecting numpy>=1.15.4
  Using cached numpy-1.19.5-cp36-cp36m-win_amd64.whl (13.2 MB)
Collecting pytz>=2017.2
  Downloading pytz-2021.3-py2.py3-none-any.whl (503 kB)
     |████████████▌                   | 194 kB 7.4 kB/s eta 0:00:42
     |█████████████                   | 204 kB 7.3 kB/s eta 0:00:41
     |██████████████                  | 215 kB 6.5 kB/s eta 0:00:4
     |██████████████                  | 225 kB 6.5 kB/s eta 0:00:4
'''

16.更换pip源

国外服务器的源速度比较慢,可以从国内的镜像源下载,例如:清华源、豆瓣源、阿里云源等。

但镜像源数据有滞后性,比如清华源的 pypi 镜像每 5 分钟同步一次。

  • 临时使用
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package matplotlib
  • 设置默认
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

设为默认后,以后安装库都是从清华源下载,而且无需再加镜像源网址。

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
关于Python Kivy的教程,你可以按照以下步骤进行学习: 1. 首先,根据你的操作系统,安装Kivy。在Linux上,你可以使用以下命令安装Kivy:$ python -m pip install kivy。在Windows上,你可以使用以下命令安装Kivy:$ python -m pip install kivy。在Mac OS X上,你可以使用以下命令安装Kivy:$ python -m pip install kivy。 2. 一旦Kivy安装完成,你可以开始学习Kivy的基础知识。Kivy是一个基于Python的GUI库,可以用于快速编写跨平台的应用程序。它具有快速编程的特点,可以用于编写Windows、Linux、Mac、Android、iOS等主流平台的应用程序。与其他GUI库相比,Kivy最大的优点是可以快速地编写移动应用程序。 3. 你可以通过阅读Kivy的官方文档来学习更多关于Kivy的知识。官方文档提供了详细的教程和示例代码,可以帮助你快速入门并掌握Kivy的各种功能和特性。你可以在Kivy官方网站上找到官方文档。 4. 另外,你还可以参考一些在线教程和视频教程来学习Kivy。有很多开发者和教育机构提供了免费或付费的Kivy教程,可以帮助你更深入地学习和理解Kivy的使用方法和技巧。 总结起来,如果你想学习Python Kivy,你可以先安装Kivy,然后阅读官方文档并参考一些在线教程和视频教程。这样你就可以逐步掌握Kivy的知识和技能,开始编写自己的Kivy应用程序了。 #### 引用[.reference_title] - *1* *2* *3* [python编程(Kivy 安装及使用教程)](https://blog.csdn.net/hpwzjz/article/details/89703313)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值