Python pip包打包指南
最新样例工程可参考 Python 模块的 pip 化
https://devops.momenta.works/Momenta/public/_git/tbc_rdp
样例工程:
- https://devops.momenta.works/Momenta/hdmap-workflow/_git/worker-io
- https://devops.momenta.works/Momenta/public/_git/mdk-tools
增加setup.py 文件
setuptools:https://setuptools.readthedocs.io/en/latest/setuptools.html
setup.py样例
from setuptools import setup, find_packages
def sh(command):
try:
if isinstance(command, list):
command = ' '.join(command)
return subprocess.check_output(
command, shell=True,
stderr=subprocess.STDOUT).decode('utf-8').rstrip()
except Exception as e:
return None
VERSION = 'major.minor.patch'
git_branch = sh('git rev-parse --abbrev-ref HEAD')
git_commit_hash = sh('git log -1 --format=%h')
git_commit_count = sh('git rev-list --count HEAD')
git_diff_name_only = sh('git diff --name-only')
if git_diff_name_only:
git_diff_name_only = git_diff_name_only.replace('
', ',')
# this is a hack, the easiest way I found to inject these metedata
with open('worker_io/version.py', 'w') as f:
f.write(f'# DO NOT EDIT THIS FILE, IT IS GENERATED BY SETUP.PY
')
f.write(f'__version__ = "{VERSION}"
')
f.write(f'git_branch = "{git_branch}"
')
f.write(f'git_commit_hash = "{git_commit_hash}"
')
f.write(f'git_commit_count = {git_commit_count}
')
f.write(f'git_diff_name_only = "{git_diff_name_only}"
')
# 获取当前打包文件的依赖 建议通过读取requirements.txt 来实现确保更新后打包的依赖及时更新
with open('requirements.txt') as f:
lines = f.readlines()
install_requires = [
line.strip() for line in lines if line and not line.startswith('--')
]
setup(
# 这里列举部分 keywords 如有需要 https://setuptools.readthedocs.io/en/latest/references/keywords.html
name=PACKAGE_NAME,
version=VERSION,
keywords=KEYWORDS, # package 关键字和package相关即可
description=DESCRIPTION, # package 功能描述
url=URL, # 可增加package confluence说明页面
author='HDMap Platform R&D',
author_email='hdmap-platformrnd-group@momenta.ai',
packages=find_packages(), # 如果含有多个子目录,需要指定多个子目录。like packages=[ditu.inspect, ditu]
platforms='any',
zip_safe=False,
install_requires=install_requires,
python_requires='>=3', # 对所需python版本进行限制
)
Makefile 中增加以下命令
package: # 打包当前项目
python3 setup.py bdist_wheel
package_install: package # 打包并在本机安装当前项目
python3 -m pip install dist/*.whl -U
upload: # 上传当前项目至artifactory
python3 setup.py bdist_wheel upload -r local
clean:
rm -rf __pycache__ dist *.egg
配置.pypirc→ 需要放到~目录下
见python artifactory set me up 方法(配置 pypirc)
[distutils]
index-servers = local
[local] #local 于上述上传upload -r local相对应
repository: https://artifactory.momenta.works/artifactory/api/pypi/pypi-momenta
username:xxxx
password: ****************
发布流程
- 修改完成代码后,在setup.py中修改版本信息。
- make package 打包当前package
- make package_install 安装当前package 到本机进行测试
- 测试通过后执行 make upload
- 如遇下图,代表当前包含多个版本 package包缓存,执行make clean清空即可