可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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:
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:
See here: http://docs.python.org/using/windows.html
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.