Python管理工具:setuptools和pip的使用详解

Python包管理工具简介

setuptools

setuptools管理Python的第三方包,将包安装到site-package下,安装的包后缀一般为.egg,实际为ZIP格式。默认从 http://pypi.python.org/pypi 下载包,能够解决Python包的依赖关系;安装了setuptools之后即可用 easy_install 命令安装包,有多种安装方式可以选择。

pip

pip也是一个Python的包管理工具,它和setuptools类似,但是pip比setuptools更好用,现在安装python包基本都是使用pip了。如果使用virtualenv工具,会自动安装一个pip。
Python包管理工具安装

Python2.7安装setuptools

$ yum install python-setuptools

$ tar xvf setuptools-7.0.tar.gz

$ cd setuptools-7.0

$ python2.7 setup.py install

$ ln -s /usr/local/python27/bin/easy_install /usr/local/bin/easy_install

Python2.7安装pip

$ easy_install pip

$ yum install python-pip

$ wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5=834b2904f92d46aaa333267fb1c922bb" --no-check-certificate

$ tar xvf pip-1.5.4.tar.gz

$ cd pip-1.5.4

$ python2.7 setup.py build

$ python2.7 setup.py install

$ ln -s /usr/local/python27/bin/pip /usr/local/bin/pip

PIP使用帮助

# pip --help
 
Usage:   
  pip [options]
 
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                        #当前帮助.
 
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使用实例

# 搜索包;
$ 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 help install

Usage:
  pip install [options]  [package-index-options] ...
  pip install [options] -r  [package-index-options] ...
  pip install [options] [-e]  ...
  pip install [options] [-e]  ...
  pip install [options]  ...
........

下面说几个重要参数:

升级一个软件包。

$ pip install --upgrade pip

批量安装软件包,使用-r指定一个文本,在文本中指定软件包名称,也可以指定软件包版本。

$ pip install -r requirements.txt

修改pip的镜像地址。

$ pip install -i https://mirrors.aliyun.com/pypi/simple/ redis

由于某些不可抗因素,Python官方的包在国内有时无法访问或出现网络不稳定现象。为了解决这个问题就需要将Pip中自带的源地址修改为镜像地址。

目前收集的比较好的镜像地址有:

http://pypi.douban.com/simple/
    http://mirrors.aliyun.com/pypi/simple/

如果不适用加密方式访问的话就需要指定信任此主机。

$ pip install --trusted-host mirrors.aliyun.com -i http://mirrors.aliyun.com/pypi/simple/ redis

PIP配置文件

另外,PIP支持配置文件pip.conf,可以创建在/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/

先说这么多吧,更多的参数使用可以看pip help [command]。

原文来自: https://www.linuxprobe.com/python-setuptools-pip.html

转载于:https://my.oschina.net/ssdlinux/blog/3014215

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值