使用诗歌在“包”上使用 pyinstaller 创建二进制可分发文件?

Rob*_*bAu 3 python executable package python-poetry

我想我错过了一些简单的东西

我有一个Python诗歌应用程序:

name = "my-first-api"
version = "0.1.0"
description = ""
readme = "README.md"
packages = [{include = "application"}]

[tool.poetry.scripts]
start = "main:start"

[tool.poetry.dependencies]
python = ">=3.10,<3.12"
pip= "23.0.1"
setuptools="65.5.0"
fastapi="0.89.1"
uvicorn="0.20.0"

[tool.poetry.group.dev.dependencies]
pyinstaller = "^5.10.1"
pytest = "^7.3.1"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Run Code Online (Sandbox Code Playgroud)

我可以运行它并使用 Poetry 构建它,但是,我也希望能够使用诗歌脚本创建可执行文件。

现在我这样构建它:

poetry run pyinstaller main.py --collect-submodules application --onefile --name myapi

我想要类似的东西

poetry package也自动创建此可执行文件。我该如何连接它?

顺便提一句。这不起作用:(

[tool.poetry.scripts]
start = "main:start"
builddist = "poetry run pyinstaller main.py --collect-submodules application --onefile --name myapi"
Run Code Online (Sandbox Code Playgroud)

Álv*_*aro 7

我找到了使用pyinstaller API 的解决方案。

\n

正如您可能已经知道的,Poetry 只会让您运行“脚本”(如果它们是包内的函数)。就像在您的 中一样pyproject.toml,您将start命令映射到main:start,这是start()您的main.py模块的功能。

\n

同样,您可以在模块中创建一个触发 Pyinstaller 的函数,并将其映射到可以作为poetry run <commmand>.

\n
\n

假设您有这样的模块结构:

\n
my_package\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 my_package\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 pyinstaller.py\n\xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 main.py\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 pyproject.toml\n
Run Code Online (Sandbox Code Playgroud)\n

1.创建一个文件pyinstaller.py来调用Pyinstaller API

\n

该文件应该位于您的包结构内,如上图所示。这是改编自Pyinstaller 文档

\n
my_package\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 my_package\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 pyinstaller.py\n\xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 main.py\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 pyproject.toml\n
Run Code Online (Sandbox Code Playgroud)\n

2. 将build命令映射到pyproject.toml

\n

pyproject.toml文件中添加这个

\n
import PyInstaller.__main__\nfrom pathlib import Path\n\nHERE = Path(__file__).parent.absolute()\npath_to_main = str(HERE / "main.py")\n\ndef install():\n    PyInstaller.__main__.run([\n        path_to_main,\n        \'--onefile\',\n        \'--windowed\',\n        # other pyinstaller options... \n    ])\n
Run Code Online (Sandbox Code Playgroud)\n

3. 从终端调用build命令

\n

你必须在诗歌创造的虚拟环境下这样做:

\n
poetry run build\n
Run Code Online (Sandbox Code Playgroud)\n

利润

\n