Python包管理工具——Pip

前言

pip 是一个Python包管理工具,主要是用于安装 =PyPI= 上的软件包,可以替代 =easy_install= 工具。

获取pip

脚本安装pip

1
2
3
$ curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
$ python get-pip.py

使用包管理软件安装

1
2
3
$ sudo yum install python-pip
$ sudo apt-get install python-pip

更新pip

1
$ pip install -U pip

pip基本使用

安装PyPI软件

1
2
3
4
$ pip install SomePackage
[...]
Successfully installed SomePackage

查看具体安装文件

1
2
3
4
5
6
7
8
$ pip show --files SomePackage
Name: SomePackage
Version: 1.0
Location: /my/env/lib/pythonx.x/site-packages
Files:
../somepackage/__init__.py
[...]

查看哪些软件需要更新

1
2
3
$ pip list --outdated
SomePackage (Current: 1.0 Latest: 2.0)

升级软件包

1
2
3
4
5
6
7
8
$ pip install --upgrade SomePackage
[...]
Found existing installation: SomePackage 1.0
Uninstalling SomePackage:
Successfully uninstalled SomePackage
Running setup.py install for SomePackage
Successfully installed SomePackage

卸载软件包

1
2
3
4
5
6
$ pip uninstall SomePackage
Uninstalling SomePackage:
/my/env/lib/pythonx.x/site-packages/somepackage
Proceed (y/n)? y
Successfully uninstalled SomePackage

pip简明手册

安装具体版本软件

1
2
3
$ pip install SomePackage # latest version
$ pip install SomePackage==1.0.4 # specific version
$ pip install 'SomePackage>=1.0.4' # minimum version

Requirements文件安装依赖软件

=Requirements文件= 一般记录的是依赖软件列表,通过pip可以一次性安装依赖软件包:

1
2
$ pip freeze > requirements.txt
$ pip install -r requirements.txt

列出软件包清单

1
2
3
4
5
$ pip list
$ pip list --outdated
ipython (Current: 1.2.0 Latest: 2.3.0)

查看软件包信息

1
2
3
4
5
6
7
8
9
10
11
12
13
$ pip show pip
---
Name: pip
Version: 1.4.1
Location: /Library/Python/2.7/site-packages/pip-1.4.1-py2.7.egg
Requires:
$ pip show pyopencl
---
Name: pyopencl
Version: 2014.1
Location: /Library/Python/2.7/site-packages
Requires: pytools, pytest, decorator

搜寻

1
2
3
4
5
6
7
$ pip search pycuda
pycuda - Python wrapper for Nvidia CUDA
pyfft - FFT library for PyCuda and PyOpenCL
cudatree - Random Forests for the GPU using PyCUDA
reikna - GPGPU algorithms for PyCUDA and PyOpenCL
compyte - A common set of compute primitives for PyCUDA and PyOpenCL (to be created)

配置文件

配置文件: =$HOME/.pip/pip.conf=, 举例:

1
2
3
4
5
6
7
[global]
timeout = 60
index-url = http://download.zope.org/ppix
[install]
ignore-installed = true
no-dependencies = yes

命令行自动补全

对于bash:

1
$ pip completion --bash >> ~/.profile

对于zsh:

1
$ pip completion --zsh >> ~/.zprofile

加载此配置文件后,则pip命令支持自动补全功能.

后记

应该尽量使用pip,不要继续使用easy_install.

吴羽舒 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!