【发布时间】:2021-04-04 08:48:56
【问题描述】:
考虑以下名为 test.py 的 python (3.9) 脚本:
import sys
import os
import subprocess
from pathlib import Path
# This is for pyinstaller (as main_dir = sys.path[0] won't work)
if getattr(sys, 'frozen', False):
main_dir = os.path.dirname(sys.executable)
else:
main_dir = os.path.dirname(os.path.abspath(__file__))
processes_dir = Path(main_dir, "processes")
outfile = Path(main_dir, "output.txt")
# Initialize the output text file
with open(outfile, 'w') as f:
f.write('')
# This calls A1.py (see below)
result = subprocess.run([sys.executable, Path(processes_dir, "A1.py")], input="1\n2", capture_output=True, text=True)
# If an error is raised, it's written to output.txt; stdout is written to output.txt
if result.stderr:
with open(outfile, 'a') as f:
f.write("{0}\n\n".format(result.stderr))
else:
with open(outfile, 'a') as f:
f.write("{0}\n\n".format(result.stdout))
subprocess.run 调用以下简单脚本:
x1 = int(input())
x2 = int(input())
print(x1+x2)
这运行得很好。我正在尝试研究如何使用 Pyinstaller 将其转换为可执行文件 (.exe)。在相应的目录中,我运行:
pyinstaller --onefile test.py
这会成功构建 test.exe。当我运行 test.exe(从 cmd 或双击文件)时,它打开时没有错误,产生一个空的 output.txt,然后只是无限期地挂起。看来 subprocess.run 无法与 pyinstaller 一起正常工作。有什么想法/建议可以让 test.exe 与 pyinstaller 一起使用?
【问题讨论】:
-
你从
PyPi安装了pyinstaller吗?如果是这样,请尝试卸载它并从 githubpip install https://github.com/pyinstaller/pyinstaller/archive/develop.zip安装它 -
根据 pyinstaller 文档,
sys.executable不像普通 Python 代码那样是 Python 解释器,因此使用它来运行 Python 脚本可能无法如您所愿。尝试打印它,看看它是什么。 pyinstaller.readthedocs.io/en/stable/runtime-information.html -
@IceBear 是的,我使用
pip install pyinstaller从PyPi安装了它。我相信它与github上提供的安装相同。 -
@barny 它打印
C:\Users\Daniel\Desktop\Pyinstaller_test\test.exe这确实是 test.exe 的位置,所以这似乎是问题所在。 PyInstaller 应该捆绑一个 python 应用程序(以及所有必需的依赖项),那么我如何指导 test.exe 调用这个捆绑的 python?我可能可以将其指向我的 python 安装,但这首先破坏了将其捆绑为 exe 的全部目的,因为我打算与没有安装 python 的用户共享它。 -
你的代码可以导入python代码并从中执行一些东西,或者你可以评估它来执行一些东西。
标签: python python-3.x subprocess pyinstaller