Pyinstaller 可执行文件不接受任何输入。

4
我想从.py文件创建.exe文件。如果我运行.py文件,它可以正常工作,没有问题。但是当我运行用pyinstaller创建的.exe文件时,在命令行中无法输入(键入)任何内容。
我已经尝试了几个选项 - 单文件可执行文件(--onefile),禁用upx(--noupx)。但都没有改善。
我导入自己的库是否有问题?最好将我使用的函数粘贴到我的.py文件中吗?
from PIL import Image
import numpy as np
from convetai.cropImage import cropImage, step
def main():
    path = input()
    img = np.array(Image.open(f"{path}"))

    print("Your image has a shape of ", img.shape)
    n = int(input("Enter number of divisions."))
    #dx, dy = step(n, img.shape)
    i = int(input("Enter num of row"))
    j = int(input("Enter num of column"))
    n_img = cropImage(img, n, i, j, save=True)
    print("Done.")

if __name__ == '__main__':
    main()

谢谢。


1
你运行一个Windows程序时,确保声明该程序为控制台程序,因为它似乎需要用户输入。因此不要忘记使用 --console 开关另外,从 -D 选项开始。只有在使用 onedir 选项正常工作后,才应继续使用 --onefile也许在 path = input() 之前加入一个打印语句,并改为以下形式: path = input("请输入路径:") 以便更好地进行调试。告诉我们你在这些更改后在控制台上看到的确切内容。 - gelonida
当我键入 pyinstaller --onedir --console script.py 时,没有任何变化。我运行 dist\script\script.exe 文件,它打开 cmd 窗口约半秒钟,然后就关闭了。我还添加了 path = input("Enter the path") - Huvi
不要点击exe进行调试(除非你是一个非常快速的读者;-)) 打开cmd窗口并从那里调用exe。 这样可以给您一些时间来阅读错误信息,然后您可以在此处发布。 - gelonida
哦,它说“没有名为'numpy.random.common'的模块”。 - Huvi
通过添加 import numpy.random.common,import numpy.random.bounded_integers, import numpy.random.entropy 来解决。 :) 谢谢! - Huvi
2个回答

4

由于存在 input() 方法,当转换为exe文件时应使用控制台选项。GUI不起作用。尝试下面的代码:

pyinstaller --noconfirm --onefile --console test_Script.py 

它对我来说完美地运行了。有没有一种方法可以添加带空格的文件名,例如“test 001.py”? - Just doin Gods work

0

我的建议是使用pyinstaller制作exe文件,每当您的程序需要输入时,只需使用tkinter。

例如:

import tkinter as tk from tkinter import simpledialog for features in FeatureList:

ROOT = tk.Tk()

ROOT.withdraw()
# the input dialog
USER_INP = simpledialog.askstring(title="User Input",
                                  prompt="Do You want to Continue")
# check it out
print("Continuing", USER_INP)

现在你可以随意使用USER_INP了


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接