使用pyinstaller编译时找不到exe文件
exe file not found while compiled with pyinstaller
我想从使用 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,它应该会出现一个消息框。
您可以使用 --add-binary
选项将另一个二进制文件与 pyinstaller 捆绑到您的 exe 中。
在您的 Python 脚本中,您可以使用 subprocess.Popen(exe_path)
调用嵌入在您的 exe 中的 exe。您可以使用 sys._MEIPASS
访问将在其中找到 exe 的临时位置,以构建 exe 的路径。
例子
putty_launcher.py
import os
import sys
import subprocess
if getattr(sys, 'frozen', False):
base_path = sys._MEIPASS
else:
base_path = ""
exe_path = os.path.join(base_path, 'binaries\putty.exe')
subprocess.Popen(exe_path)
文件夹结构
root
├── binaries
│ └── putty.exe
├── putty_launcher.py
在根文件夹中,执行:
pyinstaller --add-binary "binaries\putty.exe;binaries" --onefile putty_launcher.py
这将从 putty_launcher.py 脚本构建一个 exe,它可以成功调用 putty.exe[=38= 的版本] 嵌入在 exe 中。
我想从使用 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,它应该会出现一个消息框。
您可以使用 --add-binary
选项将另一个二进制文件与 pyinstaller 捆绑到您的 exe 中。
在您的 Python 脚本中,您可以使用 subprocess.Popen(exe_path)
调用嵌入在您的 exe 中的 exe。您可以使用 sys._MEIPASS
访问将在其中找到 exe 的临时位置,以构建 exe 的路径。
例子
putty_launcher.py
import os
import sys
import subprocess
if getattr(sys, 'frozen', False):
base_path = sys._MEIPASS
else:
base_path = ""
exe_path = os.path.join(base_path, 'binaries\putty.exe')
subprocess.Popen(exe_path)
文件夹结构
root
├── binaries
│ └── putty.exe
├── putty_launcher.py
在根文件夹中,执行:
pyinstaller --add-binary "binaries\putty.exe;binaries" --onefile putty_launcher.py
这将从 putty_launcher.py 脚本构建一个 exe,它可以成功调用 putty.exe[=38= 的版本] 嵌入在 exe 中。