Python私有仓库打包部署方式
一、背景
最近在做 Java&Python工程化的内容,为了将自己的通用功能或基础功能打包发布到私有仓库,类似 Java Maven deploy 的功能。
二、文档结构及示例
1. 示例结构
|--com
| |--static
| | |--icon.svg
| | |--confg.json
| |--demo
| | |--__init__.py
| | |--core.py
| |--__init__.py
| |--__version__.py
| |--api.py
| |--utils.py
|--tests
| |--__init__.py
|--LICENSE
|--README.md
|--requirements.txt
|--setup.py
2. setup.py 文件示例
from setuptools import setup, find_packages
with open("requirements.txt", "r", encoding="utf-8") as fh:
requires = fh.readlines()
with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()
setuptools.setup(
name="demo-py",
version="0.0.1",
author="lytao123",
author_email="lytao123",
description="a demo by python package",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/pypa/sampleproject",
packages=find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires='>=3.6',
install_requires=requires
)
三、打包发布
1. 安装
python setup.py install
2. 打包
# tar.gz包(源码)
python setup.py sdist
# whl 包(编译)
python setup.py bdist_wheel
# tar.gz包和whl 包(推荐)
python setup.py sdist bdist_wheel
3. 上传到私有仓库
# 安装 twine
pip install twine
-
方式一
- 新增 ~/.pypirc 文件,注意 window 系统的路径为
C:\Users\用户名\.pypirc
[distutils] index-servers=nexus [nexus] repository:pypi私服地址 username:username password:passwaord
- 执行命令
python -m twine upload -r nexus dist/*
或者twine upload -r nexus dist/*
- 新增 ~/.pypirc 文件,注意 window 系统的路径为
-
方式二
- 直接执行如下命令
python -m twine upload --repository-url http://pypi私服地址/ -u username -p password dist/*
- 直接执行如下命令
四、验证拉取包
执行如下命令,成功拉取包;也可去私服仓库查看是否存在上传的包。
pip install demo-py==0.0.1 -i http://pypi私服地址/simple --trusted-host pypi私服.com
五、问题记录
打包命令执行错误
执行后报错为: error: invalid command bdist_wheel
解决方式:执行 pip install wheel
按照 wheel 模块
可通过执行 python setup.py --help-commands
查询支持的包格式,默认有