Python pip 用法,看这一篇文章就够了

aries 发表于 2021-05-06 909 次浏览

安装 pip

自从Python 3.4版本开始,pip已经被内置在Python中,所以无需再次安装。如果你的系统中没有pip/pip3命令,或者不小心卸载了,可以使用下面方式安装。

使用 easy_install 安装 pip

使用easy_install安装pip, 同时pip也是easy_install替代品

easy_install pip

Ubuntu 安装 pip

sudo apt install python3-pip

Mac

iw3c@MacBook-Pro-LM ~ % brew install python3
iw3c@MacBook-Pro-LM ~ % pip3 install scrapy

安装 python3 会携带 pip3 命令

查看版本

iw3c@MacBook-Pro-LM ~ % pip --version
pip 21.1 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)

iw3c@MacBook-Pro-LM ~ % pip3 --version
pip 21.1 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)

升级 pip 命令

iw3c@MacBook-Pro-LM ~ % pip install --upgrade pip

查询包

查询包

pip search "Markdown"

安装包

安装包
pip install Markdown

安装特定版本的包, 通过使用 ==, >=, <=, >, < 来指定一个版本号。

比如说我要安装3.4.1版本的matplotlib

pip install matplotlib==3.4.1           

pip install 'Markdown<2.0'
pip install 'Markdown>2.0,<2.0.3'

卸载包

卸载包
pip uninstall Markdown

升级包

升级包到当前最新的版本,可以使用-U 或者 --upgrade

pip install distribute --upgrade
pip install -U Markdown

显示包详细信息

iw3c@MacBook-Pro-LM ~ % pip show netkiller-logging
Name: netkiller-logging
Version: 0.0.5
Summary: log send to remote
Home-page: http://netkiller.github.io
Author: None
Author-email: netkiller@msn.com
License: BSD
Location: /usr/local/lib/python3.9/site-packages
Requires: 
Required-by:

列出已经安装的包

iw3c@MacBook-Pro-LM ~ % pip list                  
Package                                           Version
------------------------------------------------- ---------
autopep8                                          1.5.6
bleach                                            3.3.0
certifi                                           2020.12.5
chardet                                           4.0.0
click                                             7.1.2
colorama                                          0.4.4
cv                                                1.0.0
cycler                                            0.10.0
decorator                                         4.4.2
dlib                                              19.22.0
docutils                                          0.17

查看需要升级的包

neo@MacBook-Pro-Neo ~ % pip list -o

目前已经安装的库中,看哪些需要版本升级

neo@MacBook-Pro-Neo ~ % pip list -o
Package                                           Version   Latest   Type
------------------------------------------------- --------- -------- -----
autopep8                                          1.5.6     1.5.7    wheel
decorator                                         4.4.2     5.0.7    wheel
docutils                                          0.17      0.17.1   wheel
easyocr                                           1.3.0.1   1.3.1    wheel
et-xmlfile                                        1.0.1     1.1.0    wheel
idna                                              2.10      3.1      wheel
importlib-metadata                                3.10.0    4.0.1    wheel
Pygments                                          2.8.1     2.9.0    wheel
pyobjc                                            7.1       7.2      wheel

查看包版本

列出安装包的描述信息

$ pip freeze
iw3c@MacBook-Pro-LM ~ % pip freeze
autopep8==1.5.6
bleach==3.3.0
certifi==2020.12.5
chardet==4.0.0
click==7.1.2
colorama==0.4.4
cv==1.0.0
cycler==0.10.0
decorator==4.4.2
dlib==19.22.0
docutils==0.17
easyocr==1.3.0.1
et-xmlfile==1.0.1
face-recognition==1.3.0
face-recognition-models==0.3.0
idna==2.10
imageio==2.9.0
imageio-ffmpeg==0.4.3
importlib-metadata==3.10.0
keyboard==0.13.5
keyring==23.0.1
kiwisolver==1.3.1
lxml==4.6.3
matplotlib==3.4.1
moviepy==1.0.3
netkiller-devops==0.0.1
netkiller-kindle==0.0.3
networkx==2.5.1
numpy==1.20.2
opencv-python==4.5.1.48
openpyxl==3.0.7
packaging==20.9
pandas==1.2.4
Pillow==8.2.0
pkginfo==1.7.0
proglog==0.1.9
progress==1.5
PyAudio==0.2.11
pycodestyle==2.7.0
Pygments==2.8.1
pyobjc==7.1
python-dateutil==2.8.1
python-docx==0.8.10
pytz==2021.1
PyWavelets==1.1.1
PyYAML==5.4.1
readme-renderer==29.0
requests==2.25.1
requests-toolbelt==0.9.1
rfc3986==1.4.0
scikit-image==0.18.1
scipy==1.6.2
six==1.15.0
SpeechRecognition==3.8.1
tabulate==0.8.9
tifffile==2021.3.31
toml==0.10.2
torch==1.8.1
torchvision==0.9.1
tqdm==4.59.0
twine==3.4.1
typing-extensions==3.7.4.3
urllib3==1.26.4
webencodings==0.5.1
xlrd==2.0.1
zipp==3.4.1

批量安装库

如果一个项目需要安装很多库,可以将他们放入一个文件 requirements.txt,例如那可以批量安装:

pip install -r requirements.txt

requirements.txt 文件内容格式如下:

netkiller-logging==0.0.4
netkiller-firewall

操作演示

iw3c@MacBook-Pro-LM ~ % vim requirements.txt     

neo@MacBook-Pro-Neo ~ % cat requirements.txt 
netkiller-logging
netkiller-firewall
                               
neo@MacBook-Pro-Neo ~ % pip install -r requirements.txt 
Collecting netkiller-logging
  Downloading netkiller_logging-0.0.5-py3-none-any.whl (14 kB)
Collecting netkiller-firewall
  Downloading netkiller_firewall-0.0.1-py3-none-any.whl (5.4 kB)
Installing collected packages: netkiller-logging, netkiller-firewall

导出已经安装的包列表,并保存到到本地文件中:

pip freeze > requirements.txt

安装 wheel文件

pip install dist/netkiller_firewall-0.0.1-py3-none-any.whl

操作演示

iw3c@MacBook-Pro-LM ~ % cd workspace/firewall 
iw3c@MacBook-Pro-LM ~/workspace/firewall % ls dist 
netkiller-firewall-0.0.1.tar.gz           netkiller_firewall-0.0.1-py3.9.egg
netkiller_firewall-0.0.1-py3-none-any.whl
iw3c@MacBook-Pro-LM ~/workspace/firewall % pip install dist/netkiller_firewall-0.0.1-py3-none-any.whl
Processing ./dist/netkiller_firewall-0.0.1-py3-none-any.whl
Installing collected packages: netkiller-firewall
Successfully installed netkiller-firewall-0.0.1

兼容性检查

验证已安装的包是否有兼容依赖问题

pip check package-name

操作演示

iw3c@MacBook-Pro-LM ~ % pip check netkiller-firewall
No broken requirements found.

从 PyPI 下载 whl 文件到本地硬盘

将库下载到本地指定目录

pip download package_name -d "要保存的文件路径"

操作演示

iw3c@MacBook-Pro-LM ~ % pip download netkiller-logging -d /tmp/netkiller-logging    
Collecting netkiller-logging
  Using cached netkiller_logging-0.0.5-py3-none-any.whl (14 kB)
Saved /private/tmp/netkiller-logging/netkiller_logging-0.0.5-py3-none-any.whl
Successfully downloaded netkiller-logging

iw3c@MacBook-Pro-LM ~ % ls /tmp/netkiller-logging                               
netkiller_logging-0.0.5-py3-none-any.whl

切换 pip 镜像

由于 pip 源服务器放在外国,所以我们安装包的时候速度非常慢。

国内一些企业和组织做了 pip 镜像,他们每个一定时间从外国服务器同步一次数据到国内服务器,我们将 pip 切换到国内服务器后,再下载包就不会去外国服务器,所以下载速度大大提高。

临时下载一个包

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple  matplotlib

永久性切换

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

设为默认后,以后安装库都将从清华镜像源下载

下面是国内常用镜像服务器地址:

清华镜像https://pypi.tuna.tsinghua.edu.cn/simple/

阿里云镜像https://mirrors.aliyun.com/pypi/simple/

中科大镜像https://mirrors.bfsu.edu.cn/pypi/web/simple/

豆瓣镜像http://pypi.doubanio.com/simple/

来源:https://zhuanlan.zhihu.com/p/369917525

0条评论

如需评论,请填写表单。
换一个

记住我的信息