[英]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.stdin
、 sys.stdout
和sys.stderr
连接到控制台窗口。
Execution is synchronous when launched from a cmd.exe
or PowerShell console window: See eryksun 's 1st comment below.从cmd.exe
或 PowerShell 控制台窗口启动时,执行是同步的:请参阅下面的eryksun的第一条评论。
pythonw.exe
is a GUI app for launching GUI/no-UI-at-all scripts . pythonw.exe
是一个用于启动 GUI/no-UI-at-all 脚本的 GUI 应用程序。
sys.stdin
, sys.stdout
and sys.stderr
are NOT available .标准流sys.stdin
、 sys.stdout
和sys.stderr
不可用。
print()
can cause that to happen (in 3.x, print()
simply has no effect).在 Python 2.x 中,简单地尝试使用print()
会导致这种情况发生(在 3.x 中, print()
根本不起作用)。
pythonw.exe yourScript.pyw 1>stdout.txt 2>stderr.txt
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 输出。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
*.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.