• 打造百折不挠的 setuptools


    赖勇浩(http://laiyonghao.com

    我一直习惯使用 setuptools 来安装 python 的程序库,因为 easy_install 实在是太简单实用了。但因为公司的网络环境比较严格,把具有登陆功能的 pypi.python.org 域名给禁用了,开放的是 c.pypi.python.org 镜像,使用 setuptools 安装 python 程序库的时候有些包下载不了,如下:

    xxx@ubuntu:~# easy_install -i http://c.pypi.python.org/simple -U lxml
    Searching for lxml
    Reading http://c.pypi.python.org/simple/lxml/
    Reading http://codespeak.net/lxml
    Reading http://lxml.de/
    Best match: lxml 2.3.3
    Downloading http://pypi.python.org/packages/source/l/lxml/lxml-2.3.3.tar.gz
    error: Can't download http://pypi.python.org/packages/source/l/lxml/lxml-2.3.3.tar.gz: 403 Forbidden

    后来发现是其尝试机制有问题,其实 lxml-2.3.3.tar.gz 在镜像站中本身就有,只是 easy_install 尝试了一次下载链接不成功后就放弃了。太不够执着了!我先定位到尝试下载的地方,然后直接修改了其源码,让它遇到下载不成功后再多试试其它的 URL,就解决了问题:

    xxx@ubuntu:~# easy_install -i http://c.pypi.python.org/simple -U lxml
    Searching for lxml
    Reading http://c.pypi.python.org/simple/lxml/
    Reading http://codespeak.net/lxml
    Reading http://lxml.de/
    Best match: lxml 2.3.3
    Downloading http://pypi.python.org/packages/source/l/lxml/lxml-2.3.3.tar.gz
    Can't download http://pypi.python.org/packages/source/l/lxml/lxml-2.3.3.tar.gz: 403 Forbidden.
    Best match: lxml 2.3.3
    Downloading http://c.pypi.python.org/packages/source/l/lxml/lxml-2.3.3.tar.gz#md5=a7825793c69d004f388ec6600bad7a6f
    Processing lxml-2.3.3.tar.gz
    Running lxml-2.3.3/setup.py -q bdist_egg --dist-dir /tmp/easy_install-0g1ftW/lxml-2.3.3/egg-dist-tmp-T6eTHt
    Building lxml version 2.3.3.
    Building with Cython 0.15.1.
    Using build configuration of libxslt 1.1.26
    Building against libxml2/libxslt in the following directory: /usr/lib
    Adding lxml 2.3.3 to easy-install.pth file
    
    Installed /usr/local/lib/python2.6/dist-packages/lxml-2.3.3-py2.6-linux-x86_64.egg
    Processing dependencies for lxml
    Finished processing dependencies for lxml
    

    补丁如下:

    Index: package_index.py
    
    ===================================================================
    
    --- package_index.py (revision 124)
    
    +++ package_index.py (working copy)
    
    @@ -445,10 +445,14 @@
    
                         continue
     
                     if dist in req and (dist.precedence<=SOURCE_DIST or not source):
    -                    return dist
    +                    self.info("Best match: %s", dist)
    +                    try:
    +                        return dist.clone(location=self.download(dist.location, tmpdir))
    +                    except DistutilsError, e:
    +                        self.info(str(e))
    +                        continue
     
     
    -
             if force_scan:
                 self.prescan()
                 self.find_packages(requirement)
    @@ -471,8 +475,7 @@
    
                     (source and "a source distribution of " or ""),
                     requirement,
                 )
    -        self.info("Best match: %s", dist)
    -        return dist.clone(location=self.download(dist.location, tmpdir))
    +        return dist
     
     
         def fetch(self, requirement, tmpdir, force_scan=False, source=False):

    后来考虑到很多朋友即使公司没设黑名单,但也可能会有域名无法访问,打 patch 也麻烦,所以我就自己打包了一个 patch 后的 setuptools,大家可以在这里(http://abu-rpc.googlecode.com/files/setuptools-0.6c11-lai-patched.tar.gz)下载。


    2012-4-27 更新:

    当执行 python setup.py install 来安装程序库的时候,也可以在后面加上 -i 参数来指定 package index。

    python setup.py install -i http://c.pypi.python.org/simple

    2012-5-24 更新:

    对于使用 -i 参数指定 package index 还说,还可以通过 easy_install 或 pip 的配置文件来简化。

    使用pip的用户可以如下配置:在unix和macos,配置文件为: $HOME/.pip/pip.conf,在windows上,配置文件为:%HOME%\pip\pip.ini,需要在配置文件内加上

    [global]
    index-url=http://c.pypi.python.org/simple

    使用easy_install的用户可以如下配置:在unix和macos,配置文件为:~/.pydistutils.cfg,在windows上,配置文件为:%HOME%\pydistutils.cfg,在配置文件中加上
    [easy_install]
    index-url=http://c.pypi.python.org/simple
  • 相关阅读:
    java.lang.NoSuchMethodError: org.springframework.core.io.ResourceEditor错误
    http://blog.sina.com.cn/s/blog_6145ed810102vr8k.html
    异或巧用:Single Number
    Highcharts:X轴分组堆叠图
    Vs2012在Linux开发中的应用(5):项目属性的定义
    BZOJ 1005 明明的烦恼 Prufer序列+组合数学+高精度
    Python 点滴 I
    easyUI 验证控件应用、自己定义、扩展验证 手机号码或电话话码格式
    InnoDB: Error: io_setup() failed with EAGAIN after 5 attempts
    Java设计模式-设计模式的六种原则
  • 原文地址:https://www.cnblogs.com/aiwz/p/6154315.html
Copyright © 2020-2023  润新知