ckdk's blog

搭建私有PYPI源

2019/01/14

搭建

建立pypi目录和packages目录

以deploy用户为例:

1
2
cd ~
mkdir -p pypi/packages

创建虚拟环境

1
2
cd ~/pypi
virtualenv pypienv --no-site-packages

安装pypiserver和passlib

1
2
3
. ~/pypi/pypienv/bin/activate
pip install pypiserver
pip install passlib

创建pypiserver密码文件

如果有apache可以是用apache bin目录下的htpasswd,用其他机器上apache创建的密码文件同样适用

1
2
# 用户名test 密码输入123456
/PATH/TO/APACHEDIR/bin/htpasswd -sc /home/deploy/pypi/.htaccess test

编写启动脚本 ~/pypi/run_pypi.sh

1
2
3
4
# 启动virtualenv 
. /home/deploy/pypi/pypienv/bin/activate
# 使用端口号3141
pypi-server -i localhost -p 3141 -P /home/deploy/pypi/.htaccess /home/deploy/pypi/packages
  • -i 指定绑定的地址,默认0.0.0.0,即允许任何地址访问
  • -P 指定使用的密码文件

创建文件 ~/home/deploy/.pypirc

应该是上传包用的文件?

1
2
3
4
5
6
7
[distutils]
index-servers = privatepypi

[privatepypi]
repository:http://127.0.0.1:3141
username:test
password:123456

usernamepassword是生成密码文件时使用的用户名和密码

使用

启动服务

1
sh ~/pypi/run_pypi.sh

也可以配合supervisor使用,supervisor配置 pypi-server.ini

1
2
3
4
5
6
7
[program:pypi-server] 
directory=/home/deploy/pypi/
command=sh run_pypi.sh
autostart=true
autorestart=true
stdout_logfile=/home/deploy/pypi/server.log
redirect_stderr=true

上传包

1
2
cd /PATH/TO/YOUR_PACKAGE
python setup.py sdist upload -r privatepypi
1
2
3
4
5
6
7
8
9
10
11
12
13
Options for 'sdist' command:
--formats formats for source distribution (comma-separated list)
--keep-temp (-k) keep the distribution tree around after creating archive
file(s)
--dist-dir (-d) directory to put the source distribution archive(s) in
[default: dist]
--help-formats list available distribution formats

Options for 'upload' command:
--repository (-r) url of repository [default: http://pypi.python.org/pypi]
--show-response display full response text from server
--sign (-s) sign files to upload using gpg
--identity (-i) GPG identity used to sign files

安装包

1
2
3
4
# 查找
# pip search search_pattern --index http://localhost:3141

pip install your_package -i http://localhost:3141
CATALOG
  1. 1. 搭建
    1. 1.1. 建立pypi目录和packages目录
    2. 1.2. 创建虚拟环境
    3. 1.3. 安装pypiserver和passlib
    4. 1.4. 创建pypiserver密码文件
    5. 1.5. 编写启动脚本 ~/pypi/run_pypi.sh
    6. 1.6. 创建文件 ~/home/deploy/.pypirc
  2. 2. 使用
    1. 2.1. 启动服务
    2. 2.2. 上传包
    3. 2.3. 安装包