【问题标题】:Pyinstaller windowed problemsPyinstaller 窗口化问题
【发布时间】:2020-05-10 19:56:37
【问题描述】:

我目前在使用 pyinstaller 3.5 时遇到问题,我使用 Pyqt 和 Subprocess 编写了一个脚本,我使用 pyinstaller 将其打包成一个 .exe。如果我在没有参数的情况下打包 .exe --windowed,则脚本可以正常工作。一旦我通过 --windowed 作为参数,就没有任何效果。因为我没有输出,所以调试不再可能了。任何人都知道这个错误是否有意义?

【问题讨论】:

  • 您可以使用pyinstaller --onefile <filename.py>进入调试模式,所有打印/错误输出都将显示在命令行上。注意你必须用命令行打开程序,双击程序是不行的。
  • 我知道。脚本与 pyinstaller --onefile 一起工作得很好。但是当我添加 --windowed 时,脚本不再起作用了。

标签: python pyqt subprocess exe pyinstaller


【解决方案1】:

可能需要更多信息才能发现问题。

1.

在使用pyinstaller + subprocess 时有几点需要注意。 我会推荐给check this page,要点是:

subprocess.Popen 在使用 --noconsole 选项从 Pyinstaller 运行时会默认弹出一个命令窗口。

Windows 默认不搜索路径。

使用 --noconsole 选项从 Pyinstaller 生成的二进制文件运行此命令需要重定向所有内容(stdin、stdout、stderr)以避免 OSError 异常:“[错误 6] 句柄无效。”

2.

然后,出于调试目的,您可以尝试使用调试选项运行 pyinstaller(文档为 here):

pyinstaller --debug=all

这可以为您指明正确的方向。

3.

最后,一些sys.stdout 代码也可以阻止exe--windowed 模式下运行,例如:

sys.stdout.reconfigure(encoding='utf-8')
#or
sys.stdout = open(sys.stdout.fileno(), mode='w', encoding='utf8', buffering=1)

在这种情况下,使用以下代码更改sys.stdout 编码可能会有所帮助:

if sys.stdout.encoding != 'UTF-8':
    sys.stdout = codecs.getwriter('utf-8')(sys.stdout.buffer, 'strict')
if sys.stderr.encoding != 'UTF-8':
    sys.stderr = codecs.getwriter('utf-8')(sys.stderr.buffer, 'strict')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-26
    相关资源
    最近更新 更多