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)
我找到了使用pyinstaller API 的解决方案。
\n正如您可能已经知道的,Poetry 只会让您运行“脚本”(如果它们是包内的函数)。就像在您的 中一样pyproject.toml,您将start命令映射到main:start,这是start()您的main.py模块的功能。
同样,您可以在模块中创建一个触发 Pyinstaller 的函数,并将其映射到可以作为poetry run <commmand>.
假设您有这样的模块结构:
\nmy_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\nRun Code Online (Sandbox Code Playgroud)\npyinstaller.py来调用Pyinstaller API该文件应该位于您的包结构内,如上图所示。这是改编自Pyinstaller 文档
\nmy_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\nRun Code Online (Sandbox Code Playgroud)\nbuild命令映射到pyproject.toml在pyproject.toml文件中添加这个
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 ])\nRun Code Online (Sandbox Code Playgroud)\nbuild命令你必须在诗歌创造的虚拟环境下这样做:
\npoetry run build\nRun Code Online (Sandbox Code Playgroud)\n利润
\n| 归档时间: |
|
| 查看次数: |
4704 次 |
| 最近记录: |