`
hw999
  • 浏览: 49152 次
文章分类
社区版块
存档分类
最新评论

打造百折不挠的 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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics