企业内网利用Devpi搭建pypi私服

安装devpi

1. 概述

devpi包含三个组件:

  • devpi-server: 提供镜像与缓存功能,在企业DMZ区部署,提高下载python package的效率
  • devpi-web: 提供web界面与查询功能
  • devpi-client: 命令行工具, 提供包上传等功能

devpi-server

1
2
3
4
5
# 若devpi-server与互联网不通,可通过设置代理来安装
# pip --proxy http://proxy_server:3128 install -q -U devpi-server

pip install -q -U devpi-server
devpi-server --version

2. 初始化与初次启动

1
2
devpi-server --init
devpi-server --start

3. 测试

1
2
3
4
pip install -i http://localhost:3141/root/pypi/ simplejson
pip uninstall -y simplejson
easy_install -i http://localhost:3141/root/pypi/+simple/ simplejson
pip uninstall -y simplejson

devpi-web

1
2
3
4
5
pip install -q -U devpi-web
devpi-server --stop
devpi-server --recreate-search-index
devpi-server --start
pip search --index http://localhost:3141/root/pypi/ devpi-client

4. 常用命令

1
2
3
4
5
devpi-server --init
devpi-server --start
devpi-server --stop
devpi-server --status
devpi-server --log

devpi behind proxy

1
2
3
4
export http_proxy="http://proxy_server:3128"
export https_proxy="http://proxy_server:3128"
export no_proxy="localhost,10.0.0.0/8,172.16.0.0/12"
devpi-server --start

nginx reverse proxy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# http block
upstream pypi.example.com {
server 127.0.0.1:3141;
keepalive 16;
}

# server block
location ~ /(root|\+search) {
proxy_pass http://pypi.example.com;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

5. 客户端设置

pip

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Linux:       $HOME/.pip/pip.conf
# Windows 7: C:\ProgramData\pip\pip.ini
# windows xp: C:\Documents and Settings\All Users\Application Data\pip\pip.ini
# or use environment variable PIP_CONFIG_FILE to specify location

# please replace example.com to your REAL domain name

[global]
index-url = http://pypi.example.com/root/pypi/+simple/

[install]
trusted-host=pypi.example.com

[search]
index = http://pypi.example.com/root/pypi/

easy_install

1
2
3
# $HOME/.pydistutils.cfg:
[easy_install]
index_url = http://pypi.example.com/root/pypi/+simple/