pyinstaller编译时找不到exe文件

时间:2019-02-07 09:35:11

标签: python 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,但应该带有一个消息框

1 个答案:

答案 0 :(得分:1)

您可以使用--add-binary选项通过pyinstaller将另一个二进制文件捆绑到exe中。

然后,您可以使用subprocess.Popen(exe_path)在Python脚本中调用嵌入在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,该脚本可以成功调用嵌入在exe中的 putty.exe 的版本。

相关问题