How to define a script in the venv/bin dir with pyproject.toml (in hatch or any other wrapper)

huangapple go评论142阅读模式
英文:

How to define a script in the venv/bin dir with pyproject.toml (in hatch or any other wrapper)

问题

我对使用hatch创建的新文档中的打包部分不太确定,不知道是否有人已经解决了如何在pip可安装的包中定义脚本的问题。简而言之,我需要能够通过 python -m buildopen_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

以下是如何实现您期望的结果:

  1. 在您的脚本open_foo_bar.py中添加一个mainrun函数。
  2. 强制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"
  1. 使用脚本部分创建一个入口点:
[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:

  1. Add a main or run function to your script open_foo_bar.py
  2. 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"
  1. 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.

huangapple
  • 本文由 发表于 2023年3月7日 11:56:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657917.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定