显示标签为“Python”的博文。显示所有博文
显示标签为“Python”的博文。显示所有博文

2013-09-26

如何搭建本地pypi服务器

如果是建pypi镜像,直接用pypimirror就行了。但这次是要提供一些经过检查的包(可能有C语言扩展,需要人工检查确保没有安全问题)供内网机器用easy_install安装。

1. 生成安装包
解开源码包,运行:
python setup.py bdist_egg
在dist目录下会生成egg。

有的库会报错,因为zip_safe是在setuptools里定义的,而库的setup.py里写的还是distutils,将from distutils.core import setup改成from setuptools import setup即可。

2. 提供服务
https://pypi.python.org/pypi/pypiserver 用起来最简单,下载一个文件就行:
wget --no-check-certificate https://raw.github.com/schmir/pypiserver/standalone/pypi-server-standalone.py

把上一步生成的egg都放到某个目录里,比如/root/pypi/packages,然后启动服务:
python pypi-server-standalone.py --disable-fallback --server twisted --root /root/pypi/packages --port 4321
--disable-fallback 表示本地缺包时不转到官方的pypi;
--server 指定使用twisted提供服务,因为我先前刚好装过,否则还得装waitress。

访问 http://server:4321/ 就能看到介绍了。

3. 使用
在~/.pydistutils.cfg中加入:
[easy_install]
index-url = http://server:4321/simple/

安装时使用:
/opt/apps/python/bin/easy_install package_name

如果在package_name的位置写url,就不用改配置文件。