如何从符合Pyproject的项目中构建PEX或SHIV软件包?

发布于 2025-02-06 20:03:01 字数 3796 浏览 3 评论 0 原文

我有一个python项目,我想将其分发为a pex or href =“ https://github.com/linkedin/shiv#user-content-shiv” rel =“ noreferrer”> shiv python包装指南,具体取决于预先安装的python“ python”部分< /a>。我的项目以文件。我的项目还包括一些不在Python标准库中的库,因此我使用 pipenv 来管理这些库。

如何使用可以在我的 [build-backend] pyproject.toml 文件中指定的后端构建PEX软件包?

pex shiv 的文档显示如何从命令行构建独立的软件包,或通过 setuptools.pys.py ,但不使用PEP518结构和 pyproject.toml 。至少,没有我能发现的。 (并且,通过“独立的”,我的意思是所有python语言软件包,但我很乐意在目标系统上使用现有的Python 3解释器。)

请注意,包装指南中列出的三个可执行软件包中, Zipapps 似乎不适合我。它没有给我一种管理外部库的方法。

更新:根据请求,一些特定的调用。

我目前使用 build> build 作为我的构建前端。我使用 setuptools 作为我的构建后端。我的 pyproject.toml 当前读取文件,

[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

我当前通过此shell命令构建轮子:

(MyPipenvVenv) % python -m build
…[many lines of output elided]…
Successfully built MyProject-0.0.6a0.tar.gz and MyProject-0.0.6a0-py3-none-any.whl

我可以使用这些 pipenv 和 shiv 命令:

(MyPipenvVenv) % pipenv requirements > requirements.txt
(MyPipenvVenv) % shiv --console-script myapp -o app/myappfile.pyz -r requirements.txt .
  Installing build dependencies: started
  Installing build dependencies: finished with status 'done'
  Getting requirements to build wheel: started
  Getting requirements to build wheel: finished with status 'done'
  Installing backend dependencies: started
  Installing backend dependencies: finished with status 'done'
  Preparing metadata (pyproject.toml): started
  Preparing metadata (pyproject.toml): finished with status 'done'
Collecting click==8.1.3
  Using cached click-8.1.3-py3-none-any.whl (96 kB)
Collecting pip==22.1.2
  Using cached pip-22.1.2-py3-none-any.whl (2.1 MB)
Collecting setuptools==62.5.0
  Using cached setuptools-62.5.0-py3-none-any.whl (1.2 MB)
Collecting shiv==1.0.1
  Downloading shiv-1.0.1-py2.py3-none-any.whl (19 kB)
Building wheels for collected packages: MyProject
  Building wheel for MyProject (pyproject.toml): started
  Building wheel for MyProject (pyproject.toml): finished with status 'done'
  Created wheel for MyProject: filename=MyProject-0.0.6a0-py3-none-any.whl size=5317 sha256=bbcc…cf
  Stored in directory: /private/var/folders/…/pip-ephem-wheel-cache-eak1xqjp/wheels/…cc1d
Successfully built MyProject
Installing collected packages: MyProject, setuptools, pip, click, shiv
Successfully installed MyProject-0.0.6a0 click-8.1.3 pip-22.1.2 setuptools-62.5.0 shiv-1.0.1

我想要的是将命令提供给PEP 517前端,让 pyproject.toml 指定由此产生的构建工作 shiv ,指向任何配置 shiv需要。我希望结果是一个独立的应用程序文件 app/myappfile.pyz 。例如

(MyPipenvVenv) % python -m build
…[many lines of output elided]…
Successfully built MyProject
Installing collected packages: MyProject, setuptools, pip, click, shiv
Successfully installed MyProject-0.0.6a0 click-8.1.3 pip-22.1.2 setuptools-62.5.0 shiv-1.0.1

我的 pyproject.toml 文件将是类似的

[build-system]
requires = ["shiv"]
build-backend = "shiv.build_something_something"

I have a Python project which I would like to distribute as a Pex or shiv self-contained Python-executable package, in the spirit of the Python Packaging Guide, "Depending on a pre-installed Python" section. My project is structured in the spirit of PEP518, and it has a pyproject.toml file. My project also includes a few libraries not in the Python Standard Library, so I use pipenv to manage those.

How to I build the pex package using a backend which I can specify in the [build-backend] of my pyproject.toml file?

The documentation for pex and shiv show how to build self-contained packages from the command line, or via setuptools.py, but not using the PEP518 structure and pyproject.toml. At least, not as far as I have been able to discover. (And, by "self-contained", I mean all Python language packages, but I am happy to use an existing Python 3 interpreter on the destination system.)

Note that of the three executable packages listed in the Packaging Guide, zipapps does not seem like a fit for me. It doesn't give me a way to manage my external libraries.

Update: some specific invocations, per request.

I currently use build as my build frontend. I use setuptools as my build backend. My pyproject.toml file currently reads,

[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

I currently build a wheel via this shell command:

(MyPipenvVenv) % python -m build
…[many lines of output elided]…
Successfully built MyProject-0.0.6a0.tar.gz and MyProject-0.0.6a0-py3-none-any.whl

I can build a self-contained app (which relies on the system's Python interpreter) using these pipenv and shiv commands:

(MyPipenvVenv) % pipenv requirements > requirements.txt
(MyPipenvVenv) % shiv --console-script myapp -o app/myappfile.pyz -r requirements.txt .
  Installing build dependencies: started
  Installing build dependencies: finished with status 'done'
  Getting requirements to build wheel: started
  Getting requirements to build wheel: finished with status 'done'
  Installing backend dependencies: started
  Installing backend dependencies: finished with status 'done'
  Preparing metadata (pyproject.toml): started
  Preparing metadata (pyproject.toml): finished with status 'done'
Collecting click==8.1.3
  Using cached click-8.1.3-py3-none-any.whl (96 kB)
Collecting pip==22.1.2
  Using cached pip-22.1.2-py3-none-any.whl (2.1 MB)
Collecting setuptools==62.5.0
  Using cached setuptools-62.5.0-py3-none-any.whl (1.2 MB)
Collecting shiv==1.0.1
  Downloading shiv-1.0.1-py2.py3-none-any.whl (19 kB)
Building wheels for collected packages: MyProject
  Building wheel for MyProject (pyproject.toml): started
  Building wheel for MyProject (pyproject.toml): finished with status 'done'
  Created wheel for MyProject: filename=MyProject-0.0.6a0-py3-none-any.whl size=5317 sha256=bbcc…cf
  Stored in directory: /private/var/folders/…/pip-ephem-wheel-cache-eak1xqjp/wheels/…cc1d
Successfully built MyProject
Installing collected packages: MyProject, setuptools, pip, click, shiv
Successfully installed MyProject-0.0.6a0 click-8.1.3 pip-22.1.2 setuptools-62.5.0 shiv-1.0.1

What I want is to give the command to the PEP 517 front-end, have the pyproject.toml specify that the resulting build work be done by shiv, and point to whatever configuration shiv needs. I want the result be a self-contained app file app/myappfile.pyz. e.g.

(MyPipenvVenv) % python -m build
…[many lines of output elided]…
Successfully built MyProject
Installing collected packages: MyProject, setuptools, pip, click, shiv
Successfully installed MyProject-0.0.6a0 click-8.1.3 pip-22.1.2 setuptools-62.5.0 shiv-1.0.1

My pyproject.toml file would be something like,

[build-system]
requires = ["shiv"]
build-backend = "shiv.build_something_something"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九八野马 2025-02-13 20:03:01

据我所知, shiv 不是“ pep 517 构建后端”(也不是 pex ),所以不可能在 pyproject.toml 中写下类似以下内容:

[build-system]
requires = ["shiv"]
build-backend = "shiv.build_something_something"

如所讨论的 ,PEP 517接口的目标是生成 source Distributions sdist ) )和 wheels

从我的角度来看,我考虑了生成 zipapps 的工具,例如上方一层。在此级别上工作时,是否通过 sdists sdists sdists 单词是否涉及 pyproject.toml 文件无关紧要。我认为Shiv和Pex要么消耗已经可用的车轮和SDIST(也许是从pypi 下载),要么他们将“构建”步骤委派给了第三方工具(也许 pip ,也许 build ),我不知道,它不知道事情。

从我的角度来看,作为输出最有意义的输入是输出的某种“锁定文件”,而不是(PEP 517) pyproject.toml 文件。 zipapp基本上是一个文件中的一个整个“虚拟环境”。这意味着Python解释器已固定,并且每个依赖关系(直接或间接)均已固定。最好用锁定文件来描述这。

unignts.txt 文件虽然不是严格锁定文件,但可能是最接近的东西,在Python包装生态系统中有足够的可用性和支持。据我所知, unignts.txt 文件是唯一的“锁定文件”格式,例如Shiv和Pex等工具接受为输入。

因此,我对您的建议是专注于 unignt.txt 文件,以提供为PEX或SHIV的输入。就像你已经在做的那样。


在Python包装生态系统中...

看起来像 pdm 支持通过a plugin pdm-packer 的支持。

诗歌也具有锁定格式,他们在某种程度上都在寻求支持

zipapps并进行标准化锁定文件格式的工作。但这是艰难的工作,可能仍需要一些时间才能得出结论。

As far as I know, shiv is not a "PEP 517 build back-end" (neither is pex), so it is not possible to write something like the following in pyproject.toml:

[build-system]
requires = ["shiv"]
build-backend = "shiv.build_something_something"

As discussed there, the PEP 517 interface is targeted at the generation of source distributions (sdist) and wheels only.

From my point of view, I consider tools like shiv and pex that generate zipapps to be (at least) one layer above. And when working at this level, it does not matter whether or not sdists and/or wheels are generated via the PEP 517 interface, in other words it does not matter whether or not pyproject.toml files are involved. I assume that shiv and pex either consume wheels and sdists that are already available (maybe downloaded from PyPI) or they delegate the "build" step to a 3rd party tool (maybe pip, maybe build), I do not know and it does not matter.

From my point of view, the input that makes the most sense to get a zipapp as output is some kind of "lock file", and not a (PEP 517) pyproject.toml file. Zipapps are basically one whole "virtual environment" in a single file. It means that the Python interpreter is fixed, and each dependency (direct or indirect) is fixed. This is best described with a lock file.

The requirements.txt files while not strictly lock files, are probably what is the closest thing with enough availability and support in the Python packaging ecosystem. And as far as I know the requirements.txt files are the only "lock file"-ish format that tools like shiv and pex accept as input.

So my recommendation for you would be to focus on requirements.txt files to provide as input to pex or shiv. As you are already doing.


In the Python packaging ecosystem...

It looks like PDM has a real lock file format and already has support for generating zipapps via a plugin pdm-packer.

Poetry also has a lock-file format and they are somewhat looking into supporting zipapps as well

There are discussions and work going on towards a standardized lock file format. But it is difficult work, and will probably still take some time to reach a conclusion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文