简体   繁体   English

pythonw.exe 还是 python.exe?

[英]pythonw.exe or python.exe?

Long story short: pythonw.exe does nothing, python.exe accepts nothing (which one should I use?)长话短说: pythonw.exe什么都不做, python.exe什么都不接受(我应该使用哪个?)

test.py:测试.py:

print "a"

CMD window: 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>

Please tell me what I'm doing terrible wrong.请告诉我我做错了什么。

To summarize and complement the existing answers:总结和补充现有的答案:

  • python.exe is a console (terminal) application for launching CLI-type scripts (console applications) . python.exe用于启动 CLI 类型脚本(控制台应用程序)的控制台(终端)应用程序

    • Unless run from an existing console window, python.exe opens a new console window .除非从现有的控制台窗口运行,否则python.exe打开一个新的控制台窗口

    • Standard streams sys.stdin , sys.stdout and sys.stderr are connected to the console window .标准流sys.stdinsys.stdoutsys.stderr连接到控制台窗口

    • Execution is synchronous when launched from a cmd.exe or PowerShell console window: See eryksun 's 1st comment below.cmd.exe或 PowerShell 控制台窗口启动时,执行是同步的请参阅下面的eryksun的第一条评论。

      • 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 . pythonw.exe是一个用于启动 GUI/no-UI-at-all 脚本的 GUI 应用程序。

    • 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 .标准流sys.stdinsys.stdoutsys.stderr不可用
      • 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).在 Python 2.x 中,简单地尝试使用print()会导致这种情况发生(在 3.x 中, print()根本不起作用)。
          • 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. Ad-hoc ,您可以使用输出重定向谢谢,@handle。
            pythonw.exe yourScript.pyw 1>stdout.txt 2>stderr.txt
            (from PowerShell: (来自 PowerShell:
            cmd /c pythonw.exe yourScript.pyw 1>stdout.txt 2>stderr.txt ) to capture stdout and stderr output in files . cmd /c pythonw.exe yourScript.pyw 1>stdout.txt 2>stderr.txt ) 捕获文件中的stdout 和 stderr 输出。
            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:如果您确信使用print()是您的脚本使用pythonw.exe静默失败的唯一原因,并且您对 stdout 输出不感兴趣,请使用注释中的pythonw.exe命令:
            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 ).警告:调用时,此输出重定向技术不起作用*.pyw直接脚本(如通过将脚本文件路径反对pythonw.exe )。 See eryksun 's 2nd comment and its follow-ups below.请参阅eryksun的第二条评论及其下文。

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 *.py文件默认与python.exe关联(调用)
  • *.pyw files are by default associated (invoked) with pythonw.exe *.pyw文件默认与pythonw.exe关联(调用)

If you don't want a terminal window to pop up when you run your program, use pythonw.exe ;如果您不希望在运行程序时弹出终端窗口,请使用pythonw.exe
Otherwise, use python.exe否则,使用python.exe

Regarding the syntax error: print is now a function in 3.x关于语法错误: print现在是 3.x 中的一个函数
So use instead:所以改用:

print("a")

See here: http://docs.python.org/using/windows.html请参阅此处: http : //docs.python.org/using/windows.html

pythonw.exe "This suppresses the terminal window on startup." pythonw.exe “这会在启动时抑制终端窗口。”

If you're going to call a python script from some other process (say, from the command line), use pythonw.exe .如果要从其他某个进程(例如从命令行)调用 python 脚本,请使用pythonw.exe Otherwise, your user will continuously see a cmd window launching the python process.否则,您的用户将不断看到一个cmd窗口启动 python 进程。 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. python.exe将弹出一个 CLI 窗口,发送电子邮件,然后关闭窗口。 It'll appear as a quick flash, and can be considered somewhat annoying.它会以快速闪光的形式出现,并且可以被认为有点烦人。 pythonw.exe avoids this, but still sends the email. 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.将扩展名更改为 .pyw 后,请确保打开文件的属性并将“打开方式”路径指向 pythonw.exe。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM