创建包
1 2 3 4 5 6 7 | mkdir example_pkg cd example_pkg/ touch __init__.py echo 'name = "example_pkg"' > __init__.py cd .. echo "This is long readme" > README.md vim setup.py |
setup.py 内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import setuptools with open ( "README.md" , "r" ) as fh: long_description = fh.read() setuptools.setup( name = "example_pkg" , version = "0.0.1" , author = "Example Author" , author_email = "author@example.com" , description = "A small example package" , url = "", long_description = long_description, packages = setuptools.find_packages(), classifiers = [ "Programming Language :: Python :: 2" , "License :: OSI Approved :: MIT License" , "Operating System :: OS Independent" , ], ) |
如果需要将额外的文件,打包进去,则需要写 MANIFEST.in 文件,示例:
1 2 | $ cat MANIFEST. in recursive-include rpc_client_api * |
参考:https://docs.python.org/3/distutils/sourcedist.html#the-manifest-in-template
安装 setuptools 和 wheel
1 | pip install --user --upgrade setuptools wheel |
然后生成包文件
1 2 3 | $ sudo python setup.py sdist bdist_wheel $ ls dist/ example_pkg-0.0.1-py2-none-any.whl example_pkg-0.0.1. tar .gz |
.whl 文件和 .tar.gz 文件就是生成好的包文件,其中 pip 安装的时候,会优先使用 whl 文件。
参考 https://packaging.python.org/tutorials/packaging-projects/
用 nginx 搭建 pypi server
创建好目录,用于存储生成的包文件,并把我们刚生成好的包文件拷贝到目录中。
1 2 3 4 5 6 7 8 9 10 11 | cd /var/www mkdir pypi_packages && cd pypi_packages mkdir example_pkg && cd example_pkg scp vagrant@172.31.0.32: /home/vagrant/projects/example_pkg/dist/ * . cd .. $ tree . └── example_pkg ├── example_pkg-0.0.1-py2-none-any.whl └── example_pkg-0.0.1. tar .gz |
编辑 nginx 配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | $ cat /etc/nginx/conf .d /pypi .conf server { listen 80; server_name pypi.server.com; access_log /var/log/nginx/pypi-access .log; error_log /var/log/nginx/pypi-error .log error; root /var/www/pypi_packages/ ; location / { autoindex on; } } |
设置一下 pypi.server.com 的 hosts ,指向 nginx 服务器所在的ip地址,然后通过浏览器访问,应该就可以看到文件列表。
在 pip 命令中使用我们的服务
可以直接在 pip 命令后面添加 –extra-index-url 参数
1 | $ env /bin/pip install --extra-index-url http: //pypi .server.com/ example_pkg |
或者把参数写到 requirements.txt 中也是可以的
1 2 3 4 5 6 | $ cat requirements.txt --extra-index-url http: //pypi .server.com/ example_pkg == 0.0.1 $ pip install -r . /requirements .txt |
或者全局配置参数
1 2 3 4 | $ vim ~/.pip /pip .conf [global] extra-index-url = http: //pypi .server.com/ |
使用 pypiserver 搭建 server
如果不想用 nginx,或者想通过命令行来方便地上传包到 server,可以使用 pypiserver 这个包来运行server
1 2 3 4 | sudo pip install pypiserver sudo pypi-server -p 80 /var/www/pypi_packages/ & pip install --extra-index-url http: //pypi .server.com /simple/ example_pkg |
配置用户权限
1 2 3 | pip install passlib htpasswd -sc .htaccess <some_username> sudo . /pypi-server -p 80 -P .htaccess /var/www/pypi_packages/ & |
在本地端配置 pip,使用密码
1 2 3 4 5 | $ cat ~/.pypirc [internal] repository: http: //pypi .server.com username: <some_username> password: <some_passwd> |
然后打包的时候,上传到我们的 pypi server
1 | python setup.py sdist upload -r internal |
参考 https://pypiserver.readthedocs.io/en/latest/