问题:pythonw.exe还是python.exe?

长话短说:pythonw.exe什么都不做,python.exe什么也不接受(我应该使用哪一个?)

test.py:

print "a"

CMD窗口:

C:\path>pythonw.exe test.py
<BLANK LINE>
C:\path>

C:\path>python.exe test.py
  File "C:\path\test.py", line 7
    print "a"
            ^
SyntaxError: invalid syntax

C:\path>

请告诉我我在做错什么。

Long story short: pythonw.exe does nothing, python.exe accepts nothing (which one should I use?)

test.py:

print "a"

CMD window:

C:\path>pythonw.exe test.py
<BLANK LINE>
C:\path>

C:\path>python.exe test.py
  File "C:\path\test.py", line 7
    print "a"
            ^
SyntaxError: invalid syntax

C:\path>

Please tell me what I’m doing terrible wrong.


回答 0

如果您不希望在运行程序时弹出终端窗口,请使用pythonw.exe;
否则,使用python.exe

关于语法错误:print 现在是3.x中的函数,请
改用:

print("a")

If you don’t want a terminal window to pop up when you run your program, use pythonw.exe;
Otherwise, use python.exe

Regarding the syntax error: print is now a function in 3.x
So use instead:

print("a")

回答 1

总结和补充现有的答案:

  • python.exe用于启动CLI类型脚本的控制台(终端)应用程序。

    • 除非从现有控制台窗口运行,否则python.exe 将打开一个新的控制台窗口
    • 标准流 sys.stdinsys.stdout并且sys.stderr连接到控制台窗口
    • 从或PowerShell控制台窗口启动时,执行是同步的cmd.exe请参阅下面的eryksun的第一条评论。

      • 如果创建了新的控制台窗口,它将保持打开状态,直到脚本终止。
      • 从现有的控制台窗口中调用时,提示被阻止,直到脚本终止。
  • pythonw.exe是一个用于启动GUI /无UI脚本的GUI应用。

    • 没有打开控制台窗口
    • 执行是异步的
      • 从控制台窗口中调用时,无论脚本是否仍在运行,仅启动脚本并立即返回提示。
    • 标准流 sys.stdinsys.stdout并且sys.stderr不可用
      • 警告除非您采取额外的步骤否则可能会带来意想不到的副作用
        • 未处理的异常会导致脚本静默中止
        • 在Python 2.x中,仅尝试使用print()会导致这种情况发生(在3.x中,print()完全没有效果)。
        • 为了防止这种情况出现在您的脚本中,并要了解更多信息,请参阅我的答案
        • 临时的,您可以使用输出重定向谢谢,@ handle。
          pythonw.exe yourScript.pyw 1>stdout.txt 2>stderr.txt
          (来自PowerShell :)
          cmd /c pythonw.exe yourScript.pyw 1>stdout.txt 2>stderr.txt捕获文件中的 stdout和stderr输出。
          如果您确信使用的print()是你的脚本就会失败的唯一原因pythonw.exe,而你不是在标准输出输出,使用从注释兴趣@拉手的命令:
          pythonw.exe yourScript.pyw 1>NUL 2>&1
          买者:这个输出重定向技术确实调用工作时*.pyw脚本直接(而不是将脚本文件路径传递到pythonw.exe)。请参阅下面的eryksun的第二条评论及其后续意见。

通过选择正确的文件扩展名,您可以控制默认情况下哪个可执行文件运行您的脚本(例如从资源管理器打开时):

  • *.py 默认情况下,文件与以下文件关联(调用) python.exe
  • *.pyw 默认情况下,文件与以下文件关联(调用) pythonw.exe

To summarize and complement the existing answers:

  • python.exe is a console (terminal) application for launching CLI-type scripts.

    • Unless run from an existing console window, python.exe opens a new console window.
    • Standard streams sys.stdin, sys.stdout and sys.stderr are connected to the console window.
    • Execution is synchronous when launched from a cmd.exe or PowerShell console window: See eryksun‘s 1st comment below.

      • If a new console window was created, it stays open until the script terminates.
      • When invoked from an existing console window, the prompt is blocked until the script terminates.
  • pythonw.exe is a GUI app for launching GUI/no-UI-at-all scripts.

    • NO console window is opened.
    • Execution is asynchronous:
      • When invoked from a console window, the script is merely launched and the prompt returns right away, whether the script is still running or not.
    • Standard streams sys.stdin, sys.stdout and sys.stderr are NOT available.
      • Caution: Unless you take extra steps, this has potentially unexpected side effects:
        • Unhandled exceptions cause the script to abort silently.
        • In Python 2.x, simply trying to use print() can cause that to happen (in 3.x, print() simply has no effect).
        • To prevent that from within your script, and to learn more, see this answer of mine.
        • Ad-hoc, you can use output redirection:Thanks, @handle.
          pythonw.exe yourScript.pyw 1>stdout.txt 2>stderr.txt
          (from PowerShell:
          cmd /c pythonw.exe yourScript.pyw 1>stdout.txt 2>stderr.txt) to capture stdout and stderr output in files.
          If you’re confident that use of print() is the only reason your script fails silently with pythonw.exe, and you’re not interested in stdout output, use @handle’s command from the comments:
          pythonw.exe yourScript.pyw 1>NUL 2>&1
          Caveat: This output redirection technique does not work when invoking *.pyw scripts directly (as opposed to by passing the script file path to pythonw.exe). See eryksun‘s 2nd comment and its follow-ups below.

You can control which of the executables runs your script by default – such as when opened from Explorer – by choosing the right filename extension:

  • *.py files are by default associated (invoked) with python.exe
  • *.pyw files are by default associated (invoked) with pythonw.exe

回答 2

参见此处:http : //docs.python.org/using/windows.html

pythonw.exe“这将在启动时禁止显示终端窗口。”

See here: http://docs.python.org/using/windows.html

pythonw.exe “This suppresses the terminal window on startup.”


回答 3

如果要从其他进程(例如,从命令行)调用python脚本,请使用pythonw.exe。否则,您的用户将不断看到cmd启动python进程的窗口。它仍将以相同的方式运行您的脚本,但不会影响用户体验。

例如发送电子邮件。python.exe将弹出一个CLI窗口,发送电子邮件,然后关闭该窗口。它会以快速闪烁的形式出现,并且可能会有些令人讨厌。pythonw.exe避免这种情况,但仍发送电子邮件。

If you’re going to call a python script from some other process (say, from the command line), use pythonw.exe. Otherwise, your user will continuously see a cmd window launching the python process. It’ll still run your script just the same, but it won’t intrude on the user experience.

An example might be sending an email; python.exe will pop up a CLI window, send the email, then close the window. It’ll appear as a quick flash, and can be considered somewhat annoying. pythonw.exe avoids this, but still sends the email.


回答 4

我正在努力使它工作一段时间。将扩展名更改为.pyw后,请确保打开文件的属性,并将“打开方式”路径定向到pythonw.exe。

I was struggling to get this to work for a while. Once you change the extension to .pyw, make sure that you open properties of the file and direct the “open with” path to pythonw.exe.


回答 5

以我的经验,至少使用pygame时pythonw.exe更快。

In my experience the pythonw.exe is faster at least with using pygame.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。