有关 pip 的使用

有关 pip 的使用

每门语言都有自己的依赖管理工具,比如 Node.js 使用的 npmJava 使用的 MavenGo 使用的 dep 等,而我们的 Python 使用的就是 pip

pip 是 Package Installer for Python 的缩写,可以实现 Python 包的查询、下载、安装等功能。

1 功能

安装第三方包

以 two 为例子

1
2
3
4
5
6
7
8
# 最新版本
pip install two

# 指定版本
pip install two==0.1.2

# 最低版本
pip install "two>=0.1.0"

安装 requirements 文件

1
2
3
4
5
# 将依赖信息打包,输出到文件中
pip freeze > requirements.txt

# 安装所有文件中指定的包
pip install -r requirements.txt

查看

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 打印所有已经安装包的信息
pip list
# Package Version
# ---------------------------------- ----------
# alabaster 0.7.12
# anaconda-client 1.7.2
# anaconda-navigator 1.9.7
# anaconda-project 0.8.3
# ...

# 查看 two 的信息
pip show two
# Name: two
# Version: 0.1.2
# Summary: 😏随机一句「中二」的台词!
# Home-page: https://github.com/Ailln/two
# Author: Ailln
# ...

2 更改源

常用的地址有:

  • 默认:https://pypi.org/simple
  • 豆瓣:https://pypi.douban.com/simple
  • 网易:https://mirrors.163.com/pypi/simple
  • 百度:https://mirror.baidu.com/pypi/simple
  • 腾讯云:https://mirrors.cloud.tencent.com/pypi/simple
  • 阿里云:https://mirrors.aliyun.com/pypi/simple
  • 清华大学:https://pypi.tuna.tsinghua.edu.cn/simple
  • 中国科学技术大学:https://pypi.mirrors.ustc.edu.cn/simple

临时修改

1
2
# 使用时,临时修改源,以豆瓣源举例
pip install $package_name -i https://pypi.douban.com/simple

永久修改

在 Linux 下

1
2
3
4
5
6
7
8
# 更改源配置
vi ~/.pip/pip.conf

# 修改为上面任意一个源,以豆瓣源为例
[global]
index-url = https://pypi.douban.com/simple
[install]
trusted-host = https://pypi.douban.com

在 Windows 下

1
2
3
4
5
6
7
8
# 在 C:\Users\$your_name 创建一个 pip 文件夹
# 然后进入 pip 目录,新建文件 pip.ini

# 写入豆瓣源地址
[global]
index-url = https://pypi.douban.com/simple
[install]
trusted-host = https://pypi.douban.com

3 参考

作者

Ailln

发布于

2019-12-20

更新于

2024-03-02

许可协议

评论