英文:
How to define a script in the venv/bin dir with pyproject.toml (in hatch or any other wrapper)
问题
我对使用hatch创建的新文档中的打包部分不太确定,不知道是否有人已经解决了如何在pip可安装的包中定义脚本的问题。简而言之,我需要能够通过 python -m build
将 open_foo_bar.py
等示例中的包安装到 (虚拟环境)/bin
目录中。
我的包的结构如下(经过执行 python -m build
生成 dist
目录后):
pypi_package/
├── bin
│ └── open_foo_bar.py
├── dist
│ ├── foo-0.1.0-py3-none-any.whl
│ └── foo-0.1.0.tar.gz
├── pyproject.toml
├── README.md
└── test_pkg
├── foolib.py
└── __init__.py
我试图将 bin/open_foo_bar.py
安装到 $(虚拟环境)/bin
中,但它实际上被安装到了 site-packages/bin
中:
./lib/python3.10/site-packages/bin/open_foo_bar.py
我的 pyproject.toml
文件如下:
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
name = "FOO"
version = "0.1.0"
authors = [
{ name="Mr Foo", email="foo@bar.com" },
]
description = "a Foo bar without drinks"
readme = "README.md"
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]
dependencies = [
'requests'
]
[project.urls]
"Homepage" = "http://footopia.s/howto_foo"
以前,通过在 setup.py
中定义 scripts
部分,这个任务很容易完成:
setuptools.setup(
...
scripts ['bin/script1'],
...
)
英文:
Im unsure about the new doc on packaging with hatch and wonder if someone worked out how to define a script in a pip installable package. So in short I need to be able to direct python -m build
to make a package with open_foo_bar.py
as in the example, install into the (virtual env)/bin
dir.
my package looks like this (after a python -m build step that generated dist dir)
pypi_package/
├── bin
│ └── open_foo_bar.py
├── dist
│ ├── foo-0.1.0-py3-none-any.whl
│ └── foo-0.1.0.tar.gz
├── pyproject.toml
├── README.md
└── test_pkg
├── foolib.py
└── __init__.py
Im trying to get bin/open_foo_bar.py
installed into the $(virtual env)/bin
instead it installs it into the site-packages/bin
./lib/python3.10/site-packages/bin/open_foo_bar.py
myproject.toml is
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
name = "FOO"
version = "0.1.0"
authors = [
{ name="Mr Foo", email="foo@bar.com" },
]
description = "a Foo bar without drinks"
readme = "README.md"
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]
dependencies = [
'requests'
]
[project.urls]
"Homepage" = "http://footopia.s/howto_foo"
This used to be easy by defining the scripts
section in setup.py
setuptools.setup(
...
scripts ['bin/script1'],
...
)
答案1
得分: 1
在理论中,Python规范定义了入口点和脚本应该如何处理。
Hatch
选择了一种稍微不同的方式来处理,我不确定这是否被认为是最佳实践。这种方法结合了强制包含和元数据入口点/CLI。
以下是如何实现您期望的结果:
- 在您的脚本
open_foo_bar.py
中添加一个main
或run
函数。 - 强制
hatch
在安装后使您的脚本出现为包的一部分。您需要添加以下行:
[tool.hatch.build.targets.sdist.force-include]
"bin/open_foo_bar.py" = "test_pkg/open_foo_bar.py"
[tool.hatch.build.targets.wheel.force-include]
"bin/open_foo_bar.py" = "test_pkg/open_foo_bar.py"
- 使用脚本部分创建一个入口点:
[project.scripts]
open_foo_bar = "test_pkg.open_foo_bar:main"
当您安装包时,open_foo_bar
应该在您的虚拟环境bin/
目录中,它将调用open_foo_bar.py
中的main
函数,该文件将已移动到site-packages
中的包内。
这是一个巧妙的解决方案,但似乎没有支持任意脚本的一对一功能。Hatch
实际上提到了入口点部分的使用,但它是为插件管理而设计的。
英文:
In theory the Python spec defines how entrypoints and scripts are supposed to be handled.
Hatch
has chosen to do it a bit differently, and I'm not sure if this is even considered best-practice. This approach combines Forced Inclusion with Metadata Entrypoints/CLI.
Here's how you might achieve your desired outcome:
- Add a
main
orrun
function to your scriptopen_foo_bar.py
- Force
hatch
to make your script appear as part of the package once it is installed. You would add the lines:
[tool.hatch.build.targets.sdist.force-include]
"bin/open_foo_bar.py" = "test_pkg/open_foo_bar.py"
[tool.hatch.build.targets.wheel.force-include]
"bin/open_foo_bar.py" = "test_pkg/open_foo_bar.py"
- Create an entrypoint using the scripts section:
[project.scripts]
open_foo_bar = "test_pkg.open_foo_bar:main"
When you install the package, open_foo_bar
should be in your virtualenv bin/
directory, and it will call your main
function within open_foo_bar.py
, which will have been moved to reside within your package in site-packages
.
It's a hacky solution, but there doesn't appear to be a 1:1 feature for supporting arbitrary scripts. Hatch
actually does mention the usage of an Entrypoints section, but it's catered towards plugin management.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论