.pyw 和 pythonw 不能在 Windows 7 下运行 [英] .pyw and pythonw does not run under Windows 7

查看:69
本文介绍了.pyw 和 pythonw 不能在 Windows 7 下运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行一个简单的 .py 或 .pyw python 文件会导致 python.exe 显示在任务管理器下.

Running a simple .py or .pyw python file causes python.exe to show up under Task Manager.

python myApp.py
python myApp.pyw

然而,当我们尝试在不使用控制台的情况下运行它时,脚本似乎没有运行,python.exepythonw.exe 也不会出现在任务管理器下

However when we try to run it without using the console, the script does not appear to run, nor does python.exe or pythonw.exe appears under Task Manager

pythonw myApp.pyw
pythonw myApp.py

我们如何解决问题?系统运行的是 Python 2.7.8 x64.

How do we troubleshoot the problem? The system is running Python 2.7.8 x64.

推荐答案

tl;dr

  • 疑难解答,请在调用时使用输出重定向:

pythonw myApp.py 1>stdout.txt 2>stderr.txt

这将捕获文件 stdout.txt 中的 stdout 输出,例如来自 print() 的输出,以及文件 stdout.txt 中的 stderr 输出(例如来自未处理的异常)代码>stderr.txt;从 PowerShell,使用
cmd/c pythonw myApp.py 1>stdout.txt 2>stderr.txt).
请注意,重定向标准输出的行为实际上可能使您的脚本再次工作如果pythonw 失败的唯一原因是使用了print(在 Python 2.x 中 - 见下文).
警告:当直接调用 *.pyw 脚本时,这种输出重定向技术似乎不起作用(而不是通过将脚本文件路径传递给 pythonw.exe).如果您知道原因和/或它是否适合您,请告诉我.

This will capture stdout output, such as from print(), in file stdout.txt, and stderr output (such as from unhandled exceptions), in file stderr.txt; from PowerShell, use
cmd /c pythonw myApp.py 1>stdout.txt 2>stderr.txt).
Note that the very act of redirecting stdout may actually make your script work again, if the only reason for its failure with pythonw was the use of print (in Python 2.x - see below).
Caveat: This output redirection technique seemingly does not work when invoking *.pyw scripts directly (as opposed to by passing the script file path to pythonw.exe). Do let me know if you know why and/or if it does work for you.

  • 修复您的脚本:

将以下内容放在您要使用 pythonw.exe 运行的任何 Python 2.x 或 3.x 脚本的顶部:

import sys, os
if sys.executable.endswith("pythonw.exe"):
  sys.stdout = open(os.devnull, "w");
  sys.stderr = open(os.path.join(os.getenv("TEMP"), "stderr-"+os.path.basename(sys.argv[0])), "w")

当使用 pythonw.exe 运行脚本时,这可确保以下内容:

This ensures the following when a script is run with pythonw.exe:

  • print() 调用和对 sys.stdout() 的显式调用被有效地忽略(无操作).
  • Stderr 输出,包括来自未处理的致命异常,被发送到文件%TEMP%\stderr-;%TEMP% 是一个标准的 Windows 环境变量,指向当前用户的临时文件文件夹.
  • print() calls and explicit calls to sys.stdout() are effectively ignored (are no-ops).
  • Stderr output, including from an unhandled fatal exception, is sent to file %TEMP%\stderr-<scriptFileName>; %TEMP% is a standard Windows environment variable that points to the current user's folder for temporary files.

换句话说:有了上面的代码,当使用 pythonw 调用脚本时,在您的脚本静默失败后,检查文件 %TEMP%\stderr-.exe.

In other words: With the above code in place, check file %TEMP%\stderr-<scriptFileName> after your script has failed silently when invoked with pythonw.exe.

有关解释,请继续阅读.

For an explanation, read on.

在 Windows 上,pythonw.exe 用于启动 GUI/no-UI-at-all 脚本,这意味着 标准输入和输出流 - sys.stdinsys.stdoutsys.stderr 不可用.

On Windows, pythonw.exe is for launching GUI/no-UI-at-all scripts, which means that the standard in- and output streams - sys.stdin, sys.stdout, sys.stderr are NOT available.

这有两个讨厌的副作用:

  • 使用 print() - 默认针对 sys.stdout - 在 Python 2.x 中引起异常强>.
    • 此问题已在 Python 3.x 中修复.
    • 默认情况下,异常错误消息会转到 sys.stderr,这在这种情况下是不可用的.
    • Exception error messages go to sys.stderr by default, which is the very thing not available in this scenario.

    以上代码通过以下方式解决了这些问题:

    The above code fixes these problems by:

    • 将 stdout 输出发送到空设备,有效地忽略任何输出到 sys.stdout 的尝试 - 无论是显式还是通过 print() 隐式输出.

    • sending stdout output to the null device, effectively ignoring any attempt to output to sys.stdout - whether explicitly, or implicitly via print().

    将所有 stderr 输出发送到一个临时文件.

    sending all stderr output to a temporary file.

    Python 2.x 和 Python 3.x 之间的差异:

    Differences between Python 2.x and Python 3.x:

    当使用 pythonw.exesys.stdinsys.stdoutsys.stderr<运行脚本时/代码>:

    When a script is run with pythonw.exe, sys.stdin, sys.stdout, and sys.stderr:

    • 在 Python 2.x 中:具有 无效 文件描述符
      • 尝试写入 sys.stdoutsys.stderr 时的最终结果是以下异常:IOError: [Errno 9] 错误的文件描述符
      • 陷阱:由于输出缓冲,这个异常可能不会出现,直到你输出了,比如 4K 字节;您可以通过使用 -u(用于无缓冲输出)调用 pythonw.exe 来立即激发它.
      • print() 盲目尝试sys.stdout(默认情况下),所以迟早会引发这个异常.
      • in Python 2.x: have invalid file descriptors
        • The eventual result when trying to write to sys.stdout or sys.stderr is the following exception: IOError: [Errno 9] Bad file descriptor
        • Pitfall: Due to output buffering, this exception may not surface until you've output, say, 4K bytes; you can provoke it instantly by invoking pythonw.exe with -u (for unbuffered output).
        • print() blindly tries to sys.stdout (by default), so it provokes this exception sooner or later.
        • 这是与 3.x print() 函数在发现 sys.stdout 时执行 no-op(什么都不做)的补充code> 是 None,因此 print() 语句可以默认安全地使用 - 当使用 运行时,它们将被忽略>pythonw.exe
        • 然而,尝试使用 sys.stdout.write()sys.stderr.write() 仍然会导致异常.
        • This is complemented with the 3.x print() function performing a no-op (doing nothing) when it finds that sys.stdout is None, so that print() statements can by default safely be used - they'll simply be ignored when run with pythonw.exe
        • However, it follows that trying to use sys.stdout.write() and sys.stderr.write() still results in an exception.

        请参阅此处了解更多背景信息.

        See here for more background.

        这篇关于.pyw 和 pythonw 不能在 Windows 7 下运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆