【问题标题】:Python build exe file runs successfully in conda prompt but gets quit while runningPython 构建 exe 文件在 conda 提示符下成功运行,但在运行时退出
【发布时间】:2020-12-28 23:00:13
【问题描述】:

这是代码,

from pymatting import *
#import numpy as np
import tkinter as tk
root = tk.Tk()


def func():
    print( "before matting")
    scale = 1
    print( "1")
    image = load_image(r"s.png", "RGB", scale, "box")
    trimap = load_image(r"tri.png", "GRAY", scale, "nearest")
    print( "2")
    # estimate alpha from image and trimap
    alpha = estimate_alpha_knn(image, trimap)
    
    print( "3")
    # estimate foreground from image and alpha
    foreground = estimate_foreground_ml(image, alpha, return_background=False)
    print( "4")
    # save cutout
    cutout = stack_images(foreground, alpha)
    
    save_image("2_out.png", cutout)   
    print( "saved")
    
button = tk.Button(root, text = "start",command =func) 
button.pack() 
root.mainloop()

我已经使用 pyinstaller 构建了 exe,例如

pyinstaller --hidden-import six --hidden-import='pkg_resources.py2_warn' --hidden-import pymatting main.py

它构建成功,并在 conda 提示符./main.exe 中使用我可以查看输出..

但是当 main.exe 直接使用 windows 打开时,此代码一直失败。它在执行时退出..程序退出而不显示任何错误..

(我认为构建中没有包含 numpy 函数的线性求解方程,例如我的分析中的 numpy.inner 或 numpy.linalg 等,如果我错了,抱歉)

建议我解决这个问题的方法。 这是原图1 和它的trimap 2

【问题讨论】:

  • 你是什么意思...代码一直失败...,你能显示错误或更具体的问题吗?
  • 保持失败意味着无法按预期工作。 exe 文件自动停止并退出应用程序。在 conda 提示符下运行时,我可以查看输出。但是在windows下运行,只是退出程序,没有错误显示
  • 如果代码正常运行,但exe自动退出,那么可能是代码的某些部分有错误,看看提示看看,错误是什么?请记住,您也必须将exe复制到项目目录。否则,当您按下按钮时,图像路径无效,因此会带来错误和崩溃。
  • 我已将图像复制到构建目录中,它在 conda 提示符下运行良好。但一般运行时控制台中不会显示错误。然后退出
  • pyinstaller --hidden-import numpy --hidden-import six --hidden-import='pkg_resources.py2_warn' --hidden-import pymatting main.py 试试这个?

标签: python numpy tkinter build pyinstaller


【解决方案1】:

真正的问题是pymatting使用numpy函数np.linalg.normnp.inner,这两个导致实时应用程序退出。我用替代品scipy.linalg.norm and manual inner function 替换了这两个功能。 Pyinstaller 或 cx_freeze 在构建应用程序时不会使用这两个函数。所以它在 conda 提示环境之外失败

【讨论】:

  • 问题解决了吗?为什么 pyinstaller 不能与 numpy 一起使用?我有一个用numpy.linalg 制作的exe。无论如何尝试,pyinstaller --hidden-import numpy --hidden-import six --hidden-import='pkg_resources.py2_warn' --hidden-import pymatting main.py
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-24
  • 2015-11-28
  • 2023-03-26
  • 2015-08-26
  • 1970-01-01
相关资源
最近更新 更多