打造百折不挠的 setuptools

分类: Python 4606人阅读 评论(0) 收藏 举报

赖勇浩(http://laiyonghao.com

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

  1. xxx@ubuntu:~# easy_install -i http://c.pypi.python.org/simple -U lxml  
  2. Searching for lxml  
  3. Reading http://c.pypi.python.org/simple/lxml/  
  4. Reading http://codespeak.net/lxml  
  5. Reading http://lxml.de/  
  6. Best match: lxml 2.3.3  
  7. Downloading http://pypi.python.org/packages/source/l/lxml/lxml-2.3.3.tar.gz  
  8. 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,就解决了问题:

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

补丁如下:

  1. Index: package_index.py  
  2.   
  3. ===================================================================  
  4.   
  5. --- package_index.py (revision 124)  
  6.   
  7. +++ package_index.py (working copy)  
  8.   
  9. @@ -445,10 +445,14 @@  
  10.   
  11.                      continue  
  12.    
  13.                  if dist in req and (dist.precedence<=SOURCE_DIST or not source):  
  14. -                    return dist  
  15. +                    self.info("Best match: %s", dist)  
  16. +                    try:  
  17. +                        return dist.clone(location=self.download(dist.location, tmpdir))  
  18. +                    except DistutilsError, e:  
  19. +                        self.info(str(e))  
  20. +                        continue  
  21.    
  22.    
  23. -  
  24.          if force_scan:  
  25.              self.prescan()  
  26.              self.find_packages(requirement)  
  27. @@ -471,8 +475,7 @@  
  28.   
  29.                  (source and "a source distribution of " or ""),  
  30.                  requirement,  
  31.              )  
  32. -        self.info("Best match: %s", dist)  
  33. -        return dist.clone(location=self.download(dist.location, tmpdir))  
  34. +        return dist  
  35.    
  36.    
  37.      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。

  1. 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,需要在配置文件内加上

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

使用easy_install的用户可以如下配置:在unix和macos,配置文件为:~/.pydistutils.cfg,在windows上,配置文件为:%HOME%\pydistutils.cfg,在配置文件中加上
  1. [easy_install]  
  2. index-url=http://c.pypi.python.org/simple  
更多
::...
免责声明:
当前网页内容, 由 大妈 ZoomQuiet 使用工具: ScrapBook :: Firefox Extension 人工从互联网中收集并分享;
内容版权归原作者所有;
本人对内容的有效性/合法性不承担任何强制性责任.
若有不妥, 欢迎评注提醒:

或是邮件反馈可也:
askdama[AT]googlegroups.com


订阅 substack 体验古早写作:


点击注册~> 获得 100$ 体验券: DigitalOcean Referral Badge

关注公众号, 持续获得相关各种嗯哼:
zoomquiet


自怼圈/年度番新

DU22.4
关于 ~ DebugUself with DAMA ;-)
粤ICP备18025058号-1
公安备案号: 44049002000656 ...::