1. setuptools与pip
1.1. Python包管理工具简介
-
setuptools
setuptools管理Python的第三方包,将包安装到site-package下,安装的包后缀一般为.egg,实际为ZIP格式。默认从 http://pypi.python.org/pypi 下载包,能够解决Python包的依赖关系;安装了setuptools之后即可用 easy_install 命令安装包,安装后exe文件位于D:\python3\Scripts 。 目前在python3中已被废弃,通常报错:SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.。 -
pip
pip也是一个Python的包管理工具,它和setuptools类似,但是pip比setuptools更好用,现在安装python包基本都是使用pip了。如果使用virtualenv工具,会自动安装一个pip。
2. PIP
2.1. Python包管理工具PIP安装
# 1
$ easy_install pip
# 2
$ yum install python-pip
# 3
$ wget --no-check-certificate https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz
$ tar xvf pip-1.5.4.tar.gz
$ cd pip-1.5.4
$ python setup.py build
$ python setup.py install
$ ln -s /usr/local/python27/bin/pip /usr/local/bin/pip
2.2. PIP使用帮助
$ pip --help
install #安装包.
download #下载包.
uninstall #卸载包.
freeze #按着一定格式输出已安装包列表.
list #列出已安装包.
show #显示包详细信息.
search #搜索包,类似yum里的search.
wheel #Build wheels from your requirements.
hash #计算软件包归档HASH.completion
completion #A helper command used for command completion.
help #当前帮助.
General Options:
-h, --help #显示帮助.
--isolated #运行pip在一个隔离模式,忽略环境变量和用户配置.
-v, --verbose #更多的输出,最多可以使用3次
-V, --version #现实版本信息然后退出.
-q, --quiet #最少的输出.
--log #追加记录verbose输出的日志.
--proxy #指定安装包代理地址.
--retries #最大尝试连接次数,默认5次.
--timeout #连接超时时间,默认15秒.
--cache-dir #指定缓存目录.
--no-cache-dir #关闭缓存.
--exists-action #路径已存在的默认操作:(s)witch, (i)gnore, (w)ipe, (b)ackup.
--cert #Path to alternate CA bundle.
--client-cert #SSL客户端证书路径,包含私钥和pem格式的证书.
--disable-pip-version-check #不要定期检查pypi以确定是否可以下载新版本的pip.
# pip --help
Commands:
install #安装包.
download #下载包.
uninstall #卸载包.
freeze #按着一定格式输出已安装包列表.
list #列出已安装包.
show #显示包详细信息.
search #搜索包,类似yum里的search.
wheel #Build wheels from your requirements.
hash #计算软件包归档HASH.completion
completion #A helper command used for command completion.
help #当前帮助.
2.3. PIP使用实例
# 搜索包;
$ pip search redis
# 安装redis包;
$ pip install redis
# 安装指定版本;
$ pip install redis==2.10.5
# 升级包;
$ pip install --upgrade pip
# 查看redis驱动信息;
$ pip show redis
# 卸载redis驱动;
$ pip uninstall redis
# 列出所有安装包;
$ pip list
# 列出待更新包;
$ pip list --outdate
# 搜索包;
$ pip search redis
# 安装redis包;
$ pip install redis
# 安装指定版本;
$ pip install redis==2.10.5
# 升级包;
$ pip install --upgrade pip
# 查看redis驱动信息;
$ pip show redis
# 卸载redis驱动;
$ pip uninstall redis
# 列出所有安装包;
$ pip list
# 列出待更新包;
$ pip list --outdate
#这里特别关注一下install命令,查看一下帮助:
$ pip help install
#升级一个软件包
$ pip install --upgrade pip
#批量安装软件包,使用-r指定一个文本,在文本中指定软件包名称,也可以指定软件包版本:
$ pip install -r requirements.txt
#修改指定pip的镜像地址:
$ pip install -i https://mirrors.aliyun.com/pypi/simple/ redis
#设置默认源
$ pip config set global.index-url -i
由于Python官方的包在国内有时无法访问或出现网络不稳定现象。为了解决这个问题就需要将Pip中自带的源地址修改为镜像地址。pip默认使用国外的源下载第三方库,不仅速度慢,还容易报错,所以我整理了一下国内常用的源,速度快:
- 阿里云 http://mirrors.aliyun.com/pypi/simple/
- 中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
- 豆瓣(douban) http://pypi.douban.com/simple/
- 清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
- 中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/
如果不适用加密方式访问的话就需要指定信任此主机。
$ pip install --trusted-host mirrors.aliyun.com -i http://mirrors.aliyun.com/pypi/simple/ redis
2.4. PIP配置文件
另外,PIP支持配置文件pip.conf,Linux可以创建在/etc/pip.conf,或者在~/.pip/pip.conf。
$ cat /etc/pip.conf
[global]
timeout = 60
trusted-host=mirrors.aliyun.com #配置镜像源
index-url=http://mirrors.aliyun.com/pypi/simple/
Windows可以在%appddata%目录下,通常"admin/AppDatat/Roaming"目录下新建pip文件夹,创建pip.ini,制定如下信息
[global]
timeout = 6000
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn