打造百折不撓的 setuptools

來源:互聯網
上載者:User

賴勇浩(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 lxmlSearching for lxmlReading http://c.pypi.python.org/simple/lxml/Reading http://codespeak.net/lxmlReading http://lxml.de/Best match: lxml 2.3.3Downloading http://pypi.python.org/packages/source/l/lxml/lxml-2.3.3.tar.gzerror: 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 lxmlSearching for lxmlReading http://c.pypi.python.org/simple/lxml/Reading http://codespeak.net/lxmlReading http://lxml.de/Best match: lxml 2.3.3Downloading http://pypi.python.org/packages/source/l/lxml/lxml-2.3.3.tar.gzCan't download http://pypi.python.org/packages/source/l/lxml/lxml-2.3.3.tar.gz: 403 Forbidden.Best match: lxml 2.3.3Downloading http://c.pypi.python.org/packages/source/l/lxml/lxml-2.3.3.tar.gz#md5=a7825793c69d004f388ec6600bad7a6fProcessing lxml-2.3.3.tar.gzRunning lxml-2.3.3/setup.py -q bdist_egg --dist-dir /tmp/easy_install-0g1ftW/lxml-2.3.3/egg-dist-tmp-T6eTHtBuilding lxml version 2.3.3.Building with Cython 0.15.1.Using build configuration of libxslt 1.1.26Building against libxml2/libxslt in the following directory: /usr/libAdding lxml 2.3.3 to easy-install.pth fileInstalled /usr/local/lib/python2.6/dist-packages/lxml-2.3.3-py2.6-linux-x86_64.eggProcessing dependencies for lxmlFinished 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.