【发布时间】:2020-12-10 07:27:52
【问题描述】:
我想从使用 pyinstaller 编译的 python 文件中执行一个 exe 文件
我正在使用以下代码:
import subprocess, os, sys
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
new_path = resource_path("executable.exe")
print new_path
subprocess.Popen(new_path)
我使用以下代码编译它:
pyinstaller --add-binary executable.exe;exe -F incluse.py
这会创建 incluse.exe,如果我执行它,我会收到以下错误:
C:\Users\MyUsername\AppData\Local\Temp\_MEI13~1\executable.exe
Traceback (most recent call last):
File "incluse.py", line 16, in <module>
File "subprocess.py", line 394, in __init__
File "subprocess.py", line 644, in _execute_child
WindowsError: [Error 2] The system cannot find the file specified
[21812] Failed to execute script incluse
我想要它做的是执行我包含的executable.exe,它应该会出现一个消息框。
【问题讨论】:
-
...可执行文件不是 python 模块。您不能只是“导入”它们。使用 pyinstaller,您可以指定应在“dist”文件夹中复制哪些文件,这样您就可以将 executable.exe 复制到始终相对于 python 脚本的位置并在脚本中使用相对路径
-
@GPhilo 我知道,这就是我问这个问题的原因。我不知道如何在 python 脚本中使用可执行函数
-
查看我编辑的评论(在完成输入前按回车键)
-
可执行文件有什么作用?也许有一个简单的python lib?有大量的图书馆。我认为这是XY问题。见:xyproblem.info
-
@BhathiyaPerera 我已经解释过你需要给可执行文件一个参数,无论参数是什么,都来自计算机扬声器。
标签: python pyinstaller