使用pyins编译时找不到exe文件

2024-09-27 00:15:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从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)

我用的是:

^{pr2}$

威奇创造了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

我要它做的是执行可执行文件.exe我包括在内,威奇应该会给你一个留言框


Tags: 文件pathinpynewbaseossys
1条回答
网友
1楼 · 发布于 2024-09-27 00:15:34

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

在Python脚本中,可以使用subprocess.Popen(exe_path)调用嵌入在exe中的exe。您可以使用sys._MEIPASS访问exe所在的临时位置,以便生成指向exe的路径。在

示例

油灰_启动器.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)

文件夹结构

^{pr2}$

在根文件夹中,执行:

pyinstaller add-binary "binaries\putty.exe;binaries" onefile putty_launcher.py

然后,这将从putty生成一个exe_启动器.py可以成功调用版本的脚本腻子.exe嵌入exe中。在

相关问题 更多 >

    热门问题