【问题标题】:exe file not found while compiled with pyinstaller使用 pyinstaller 编译时找不到 exe 文件
【发布时间】: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


【解决方案1】:

您可以使用 pyinstaller 使用 --add-binary 选项将另一个二进制文件捆绑到您的 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,该脚本可以成功调用嵌入在 exe 中的 putty.exe 版本。

【讨论】:

  • 能给个示例代码吗?我不完全明白这是怎么做到的。
  • @hetijav 我已经编辑了答案以包含这种方法的示例。希望这会有所帮助。
  • @hetijav 我注意到这个问题自从我第一次回答以来已经发生了很大变化,所以无论如何你现在已经非常接近我的答案了。唯一出现的问题是executable.exe的路径是错误的。您的 --addbinary executable.exe;exe 将 executable.exe 放在一个名为 exe 的文件夹中,该文件夹位于运行 exe 时创建的临时文件夹中。这相当于我的回答中的“二进制文件”。所以你只需要将“exe”添加到你调用executable.exe的路径中。
  • 感谢您的 awsner,它可以工作,但是如果我将 pyinstaller 更改为 pyinstaller --add-binary "binaries\hello.exe;binaries" --onefile script.py 并将第 10 行更改为 exe_path = os.path.join(base_path, 'binaries\hello.exe') 并将我的脚本名称更改为 script.py 和将 putty.exe 改成 hello.exe,然后我编译它,我得到以下错误:WindowsError: [Error 2] The system cannot find the file specified [26264] Failed to execute script script
  • 我知道如何重现错误。如果您将 exe 的名称更改为以“n”开头的任何内容,您需要将第 10 行更改为 exe_path = os.path.join(base_path, 'binaries\no.exe') 您已经看到了吗?现在我们有了 \n ,python 将其视为新行。所以解决这个问题的方法是将 r 放在二进制路径变量之前
猜你喜欢
  • 2022-11-13
  • 1970-01-01
  • 2016-06-07
  • 2017-12-06
  • 1970-01-01
  • 2021-07-24
  • 2017-04-28
  • 2022-09-27
  • 1970-01-01
相关资源
最近更新 更多