pythonw和python_pythonw.exe or python.exe?

可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):

问题:

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 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.

回答1:

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")

回答2:

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

回答3:

pythonw.exe "This suppresses the terminal window on startup."

回答4:

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.

回答5:

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值