我正在尝试打包我的项目以便将其上传到 PyPI 中。我有以下目录结构:
.
├── docs
├── LICENSE
├── pyproject.toml
├── README.md
├── src
│ ├── package_name
│ │ ├── __init__.py
│ │ ├── data.json
│ │ ├── __main__.py
│ │ ├── utils.py
└── tests
我的包裹位于
src
下,名为src/package_name
。
pyproject.toml
有以下几行:
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
name = "project_name"
version = "0.0.1"
authors = [{name = "Foo Bar"}]
license = {text = "GPL-3.0-only"}
description = "A small example package"
readme = "README.md"
requires-python = ">=3.10"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: POSIX :: Linux",
]
[project.urls]
Homepage = "https://example.com"
我希望用户能够执行安装:
pip install project_name
但使用代码为:
>>> from package_name import x
有什么方法可以通过Hatch实现这一点吗?我已阅读构建说明,但找不到如何操作。
注意
我尝试过以下方法:
python3 -m build
python3 -m twine upload --repository testpypi dist/*
pip install --upgrade -i https://test.pypi.org/simple/ project_name
问题是当我输入:
>>> import package_name
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'package_name'
我搜索了
path/to/site-packages
,但只有path/to/site-packages/project_name-0.0.1.dist-info
在那里。有什么想法吗?
尝试根据此教程解决我的问题。
我使用 Hatch 的原因是因为教程中介绍了它。也赞赏替代解决方案(也使用具有不同后端的
pyproject.toml
)。
编辑
我已经按照评论中的建议更新了目录结构。
我通过简单地切换到
setuptools
作为后端并修改 pyproject.toml
如下找到了问题的解决方案:
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
[tool.setuptools.packages.find]
where = ["src"]
[tool.setuptools.dynamic]
version = {attr = "package_name.__version__"}
[project]
name = "package_name"
dynamic = ["version"]
authors = [{name = "Foo Bar"}]
license = {text = "GPL-3.0-only"}
description = "A small example package"
readme = "README.md"
requires-python = ">=3.10"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
"Operating System :: POSIX :: Linux",
]
[project.urls]
Homepage = "https://example.com""
我的解决方案基于 setuptools 文档。