背景
前幾天,在不同的 CentOS 服務(wù)器上嘗試部署同一個(gè)發(fā)行版本的 Galaxy 生物信息分析平臺的時(shí)候,總是發(fā)現(xiàn)在執(zhí)行 sh run.sh 部署時(shí)由于網(wǎng)絡(luò)限制導(dǎo)致的 requirements.txt 包安裝出現(xiàn) ReadTimeoutError。
galaxy@ecs-steven 08:58:14 /data/galaxy-dist/galaxy
$ sh run.sh
Found conda at: /data/galaxy-dist/anaconda2/bin/conda
Found Conda, virtualenv will not be used.
To use a virtualenv instead, create one with a non-Conda Python 2.7 at .venv
Activating Conda environment: _galaxy_18.05
Looking in indexes: https://wheels.galaxyproject.org/simple, https://pypi.python.org/simple
Collecting amqp==2.2.2 (from -r requirements.txt (line 1))
......
Collecting bx-python==0.8.1 (from -r requirements.txt (line 14))
Downloading https://wheels.galaxyproject.org/packages/bx_python-0.8.1-cp27-cp27mu-manylinux1_x86_64.whl (3.2MB)
50% |████████████████▎ | 1.6MB 4.1kB/s eta 0:06:15Exception:
Traceback (most recent call last):
File "/data/galaxy-dist/anaconda2/envs/_galaxy_18.05/lib/python2.7/site-packages/pip/_internal/cli/base_command.py", line 143, in main
status = self.run(options, args)
......
File "/data/galaxy-dist/anaconda2/envs/_galaxy_18.05/lib/python2.7/site-packages/pip/_vendor/urllib3/response.py", line 345, in _error_catcher
raise ReadTimeoutError(self._pool, None, 'Read timed out.')
ReadTimeoutError: HTTPSConnectionPool(host='wheels.galaxyproject.org', port=443): Read timed out.
原因在于:
第一,Galaxy 的初始化部署,其中有一步就是通過 pip 去在線下載位于 https://wheels.galaxyproject.org/simple 和 https://pypi.python.org/simple 這兩個(gè) PyPI 庫的 requirements.txt 包,并執(zhí)行安裝。
第二,由于這兩個(gè) pypi 庫位于國外,國內(nèi)服務(wù)器想要下載里面的包可能會出現(xiàn)網(wǎng)絡(luò)超時(shí)。
從 https://wheels.galaxyproject.org/ 的首頁介紹,可以知道這是 Galaxy 基于 pypiserver-1.2.1 搭建的一個(gè) PyPI 源(This instance is running version 1.2.1 of the pypiserver software.)。為了解決文章開頭的 ReadTimeoutError,我們借著這個(gè)機(jī)會來學(xué)習(xí)一下如何使用 pypiserver 快速搭建一個(gè)屬于自己的離線 PyPI 倉庫(本文章使用 pip==18.1)。
PyPI 私有源
PyPI(Python Package Index,https://pypi.org/) 是 Python 官方的第三方庫的倉庫,所有人都可以下載第三方庫或上傳自己開發(fā)的庫到 PyPI。
通常我們使用 pip 安裝 Python 包,默認(rèn)就是從 https://pypi.python.org/pypi (https://pypi.org/) 上安裝。當(dāng)然,我們也可以通過配置 pip.conf 或者使用命令行從指定的 PyPI 源安裝。
# 從阿里云的 PyPI 源安裝
pip install --index-url https://mirrors.aliyun.com/pypi/simple/ PACKAGE-NAME
# 替換默認(rèn) pip 源為阿里云
mkdir ~/.pip
tee ~/.pip/pip.conf <<-'EOF'
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host= mirrors.aliyun.com
EOF
這里需要提到的是,有些是公司內(nèi)部的項(xiàng)目,不方便放到外網(wǎng)上去,這個(gè)時(shí)候我們就要搭建自己的內(nèi)網(wǎng) PyPI 源服務(wù)器,需要安全并且擁有同樣的舒適體驗(yàn)。關(guān)于 PyPI 私有源的實(shí)現(xiàn),Python 官方的 PyPi Implementations 說明中列出了幾個(gè)比較成熟的實(shí)現(xiàn)方案:

這里選擇 pypiserver,除了 Galaxy 的原因外,最重要的是因?yàn)樗钚《沂褂煤唵巍?/p>
pypiserver 簡介
pypiserver is a minimal PyPI compatible server for pip or easy_install. It is based on bottle and serves packages from regular directories. Wheels, bdists, eggs and accompanying PGP-signatures can be uploaded either with pip, setuptools, twine, pypi-uploader, or simply copied with scp.
pypiserver 服務(wù)端配置
pypiserver > 1.2.x works with Python 2.7 and 3.4+ or pypy. Older Python versions may still work, but they are not tested. For legacy Python versions, use pypiserver-1.1.x series.
也就是說,pypiserver 1.2.x 以上版本要求 Python 2.7 或 3.4+,低版本的 Python 請使用 pypiserver-1.1.x。
# 替換默認(rèn) pip 源為阿里云
$ mkdir ~/.pip
$ tee ~/.pip/pip.conf <<-'EOF'
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host= mirrors.aliyun.com
EOF
# 直接在線安裝 pypiserver
$ pip install pypiserver
# 離線安裝 pypiserver
$ wget https://files.pythonhosted.org/packages/ec/f6/593ff8da4862f73c55027c32ac6f73ea09eabb546e7ebec82f83cc034fcb/pypiserver-1.2.4-py2.py3-none-any.whl
$ cp pypiserver-1.2.4-py2.py3-none-any.whl /tmp/pypiserver/
$ pip install /tmp/pypiserver/pypiserver-1.2.4-py2.py3-none-any.whl
# 創(chuàng)建 pypiserver 離線包的保存路徑
$ mkdir ~/packages
# 以 mako 包為例,下載該包并拷貝至 ~/packages
$ cd ~/packages
$ pip download -d /home/shenweiyan/packages mako
# 啟動 pypiserver 服務(wù)
$ pypi-server -p 8080 ~/packages &
pypiserver 客戶端配置
$ python -c "import mako"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'mako'
# 從本地鏡像搜索包
$ pip search --index http://localhost:8080 Mako
Mako (1.0.7) - 1.0.7
# 從指定的本地鏡像安裝包
pip install -i http://localhost:8080/simple/ mako

pypiserver 客戶端推薦的個(gè)人配置:
tee ~/.pip/pip.conf <<-'EOF'
[global]
index-url = http://120.77.xx.xx/simple
extra-index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host = 120.77.xx.xx
EOF
requirements.txt 離線 PyPI 倉庫
一般 Python 項(xiàng)目使用 pip 安裝的包,都可以通過 pip freeze >requirements.txt 導(dǎo)出環(huán)境中已有的模塊。搭建 requirements.txt 離線 PyPI 倉庫,我們首先需要把 requirements.txt 所有的模塊安裝包下載到本地。
$ pip download -d /home/shenweiyan/packages -r /home/shenweiyan/galaxy/requirements.txt --index-url https://mirrors.aliyun.com/pypi/simple --extra-index-url https://wheels.galaxyproject.org/simple
Looking in indexes: https://mirrors.aliyun.com/pypi/simple, https://wheels.galaxyproject.org/simple
Collecting amqp==2.2.2 (from -r //home/shenweiyan/galaxy/requirements.txt (line 1))
Downloading https://mirrors.aliyun.com/pypi/packages/88/4a/8c45a882d842678963516ebd9cf584a4ded51af719234c3b696c2e884c60/amqp-2.2.2-py2.py3-none-any.whl (48kB)
100% |████████████████████████████████| 51kB 779kB/s
Saved ./amqp-2.2.2-py2.py3-none-any.whl
......
Collecting wrapt==1.10.11 (from -r /home/shenweiyan/galaxy/requirements.txt (line 134))
Downloading https://wheels.galaxyproject.org/packages/wrapt-1.10.11-cp27-cp27mu-manylinux1_x86_64.whl (64kB)
100% |████████████████████████████████| 71kB 321kB/s
Saved /home/galaxy/packages/wrapt-1.10.11-cp27-cp27mu-manylinux1_x86_64.whl
Successfully downloaded amqp appdirs asn1crypto babel bagit bcrypt bdbag beaker bioblend bleach boltons boto bunch bx-python bz2file certifi ...... wcwidth webencodings webob whoosh wrapt
我們把 /home/shenweiyan/packages 整個(gè)目錄拷貝到目標(biāo)服務(wù)器(可連網(wǎng)但速度極慢,目標(biāo)路徑:/data/galaxy-dist/packages),搭建并啟動 pypiserver,然后從本地離線 PyPI 倉庫安裝 requirements 軟件:
# 登陸目標(biāo)服務(wù)器,離線安裝 pypiserver
# 啟動 pypiserver
$ cd /data/galaxy-dist/packages
$ pypi-server -p 8080 /data/galaxy-dist/packages &
# 安裝 requirements 軟件
$ conda activate galaxy
$ pip install --index-url http://localhost:8080/simple/ --extra-index-url https://mirrors.aliyun.com/pypi/simple/ -r requirements.txt
Looking in indexes: http://localhost:8080/simple/, https://mirrors.aliyun.com/pypi/simple/
Requirement already satisfied: amqp==2.2.2 in ./galaxy-dist/anaconda2/envs/galaxy/lib/python2.7/site-packages (from -r /data/galaxy-dist/galaxy/requirements.txt (line 1)) (2.2.2)
Requirement already satisfied: appdirs==1.4.3 in ./galaxy-dist/anaconda2/envs/galaxy/lib/python2.7/site-packages (from -r /data/galaxy-dist/galaxy/requirements.txt (line 2)) (1.4.3)
......
Collecting bdbag==1.2.4 (from -r /data/galaxy-dist/galaxy/requirements.txt (line 7))
Downloading http://localhost:8080/packages/bdbag-1.2.4-py2-none-any.whl (42kB)
100% |████████████████████████████████| 51kB 52.0MB/s
Collecting beaker==1.9.1 (from -r /data/galaxy-dist/galaxy/requirements.txt (line 8))
Downloading http://localhost:8080/packages/Beaker-1.9.1.tar.gz (40kB)
100% |████████████████████████████████| 40kB 49.4MB/s
......
Successfully installed bdbag-1.2.4 beaker-1.9.1 ...... warlock-1.2.0 webob-1.4.2
到這里,基于 pypiserver 的離線本地 PyPI 倉庫基本搭建與使用實(shí)踐就已經(jīng)介紹完畢了。對于一個(gè)完整的 Python 項(xiàng)目,如果需要從連網(wǎng)的開發(fā)服務(wù)器遷移至內(nèi)網(wǎng)(無法使用外網(wǎng),或者部分資源位于墻外,墻內(nèi)下載速度差)的部署服務(wù)器,pypiserver 可能是一個(gè)不錯(cuò)的解決方案。
- 對于 conda 依賴的 Python 項(xiàng)目,國內(nèi)推薦使用清華大學(xué)的鏡像。
- 對于 PyPI 源,國內(nèi)推薦使用阿里云或者清華大學(xué)的 PyPI 鏡像。
- 對于特定項(xiàng)目的第三方 PyPI 源,可考慮 pypiserver 實(shí)現(xiàn)離線本地化。
pypiserver 的一些高級用法,如基于 systemd 和 supervisor 的自動化啟動管理;基于 Docker 技術(shù)的部署與使用;基于 Nginx 反向代理的 pypiserver 運(yùn)行等等,我們后面有時(shí)間再進(jìn)行介紹。有需要的童鞋也可參考 pypiserver 的 GitHub 說明文檔:https://github.com/pypiserver/pypiserver。
參考資料
- 如何搭建自己的pypi私有源服務(wù)器,阿里云教程中心
- 使用 pypiserver 快速搭建內(nèi)網(wǎng)離線 pypi 倉庫實(shí)踐,HelloDog 博客
- pypi 鏡像使用幫助,清華大學(xué)開源軟件鏡像站
- wiki:PyPiImplementations,wiki.python.org
- https://github.com/pypiserver/pypiserver,GitHub
