將python代碼、文件、數據打成一個whl包,可以直接pip 安裝到系統中,有兩種方法:(1)setup.py;(2)pyproject.toml
1. 使用setup.py
├── CHANGELOG.md
├── README.md
├── build
│?? ├── bdist.linux-x86_64
│?? └── lib
├── ci.yml
├── dist
│?? └── nvidia_modelopt-0.0.0-py3-none-any.whl
├── modelopt
│?? ├── __init__.py
│?? ├── __init__.pyc
│?? ├── __pycache__
│?? ├── deploy
│?? ├── onnx
│?? └── torch
├── nvidia_modelopt.egg-info
│?? ├── PKG-INFO
│?? ├── SOURCES.txt
│?? ├── dependency_links.txt
│?? ├── requires.txt
│?? └── top_level.txt
├── setup.py
└── test.py
from setuptools import setup, find_packagessetup(name="nvidia-modelopt",packages=find_packages(),install_requires=["torch"],include_package_data=True,package_data={"modelopt": ["modelopt", "*", "**/*"],},extras_require={"torch": ["torch"],},description="modelopt for guanxing"
)
直接運行 python setup.py bdist_wheel 打包文件
2. 使用?pyproject.toml
my_package_demo/
├── pyproject.toml
├── README.md
├── src/
│ └── my_package/
│ ├── __init__.py
│ └── core.py
├── tests/
│ └── test_core.py
└── data/└── sample_data.txt
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"[project]
name = "my-package-demo"
version = "0.1.0"
description = "A demo Python package."
readme = "README.md"
authors = [{ name = "Your Name", email = "you@example.com" }
]
requires-python = ">=3.7"
dependencies = [][tool.hatch.build.targets.wheel]
# 明確指定要打包的包目錄(src模式)
packages = ["src/my_package"]# 如要包含額外的數據文件(可選)
include = ["src/my_package/**.py","data/sample_data.txt"
]
[tool.hatch.build.targets.wheel].packages
?指定只打包?src/my_package
?下的包,不含其它目錄。include
?顯式指定只包含?src/my_package
?目錄下的?.py
?文件和?data/sample_data.txt
?數據文件,可精準控制(若只要包本身,可以省略或只寫包目錄)。
如果使用setuptools,可以這樣寫:
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"[project]
name = "my-package-demo"
version = "0.1.0"
description = "A demo Python package."
readme = "README.md"
requires-python = ">=3.7"[tool.setuptools]
package-dir = { "" = "src" }
packages = ["my_package"]
include-package-data = true[tool.setuptools.package-data]
my_package = ["../data/sample_data.txt"] # 注意路徑調整# 或者只包含py文件
# my_package = ["*.py"]
pip install build
python -m build
附一個比較全的pyproject.toml模版
# 構建系統配置
# PEP 517 規定的構建系統配置,指定構建工具和后端
[build-system]
# 構建時需要的依賴
requires = ["hatchling"]
# 構建后端,使用 hatchling 替代傳統的 setuptools
build-backend = "hatchling.build"# 項目元數據配置
# PEP 621 規定的項目核心元數據
[project]
# 項目名稱,將用于發布和安裝
name = "nvidia-modelopt"
# 項目版本,1.0.0 以上版本被視為正式發布版本
version = "0.1.0"
# 項目簡介,不支持非英文字符
description = "Hello world"
# README 文件路徑,用于生成長描述
readme = "README.md"
# Python 版本要求
requires-python = ">=3.10"
# 許可證信息
license = { text = "MIT" }
# 作者信息列表
authors = [{ name = "xxx", email = "xxx" }
]
# 關鍵詞,用于包索引和搜索
keywords = ["xx", "yy"]
# 項目分類,用于 PyPI 分類(如果項目不打算開源可以忽略)
# 完整分類列表:https://pypi.org/pypi?%3Aaction=list_classifiers
classifiers = ["Private :: Do Not Upload","Programming Language :: Python","Programming Language :: Python :: 3","Programming Language :: Python :: 3.10"
]
# 項目依賴列表
dependencies = ["build>=1.0.3"
]# 可選依賴配置,可通過 pip install package[test] 安裝
[project.optional-dependencies]
# 測試相關依賴
test = ["pytest","mock"
]# 項目相關 URL 配置
[project.urls]
# 項目主頁,通常是項目的 icode 地址
Homepage = "http:xxxx"
# 更新日志文件鏈接
Changelog = "CHANGELOG.md"# 命令行入口點配置
[project.scripts]
# 格式:命令名 = 模塊路徑:函數名
modelopt = "modelopt.cmdline:main"# Hatch 構建工具配置
[tool.hatch.build]
# 要包含的 Python 包
packages = ["modelopt"]
# 要包含的數據文件
# 支持通配符模式
# "modelopt": ["modelopt", "*", "**/*"],
include = ["modelopt","modelopt/*"
]# wheel 包構建配置
[tool.hatch.build.targets.wheel]
# 指定要打包的 Python 包
packages = ["modelopt"]
# wheel 包輸出目錄
output-dir = "output/dist"# 源碼包構建配置
[tool.hatch.build.targets.sdist]
# 源碼包輸出目錄
output-dir = "output/dist"