在不出现 Windows 控制台的情况下运行 Python 脚本 [英] Run Python script without Windows console appearing

查看:48
本文介绍了在不出现 Windows 控制台的情况下运行 Python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 Windows XP 中运行 Python 脚本而不暂时出现命令外壳?我经常需要使用 Python 自动化 WordPerfect(用于工作),即使我的脚本没有输出,如果我在没有 WP 的情况下执行它,空壳仍然会在消失之前弹出一秒钟.有什么办法可以防止这种情况吗?也许是某种输出重定向?

Is there any way to run a Python script in Windows XP without a command shell momentarily appearing? I often need to automate WordPerfect (for work) with Python, and even if my script has no output, if I execute it from without WP an empty shell still pops up for a second before disappearing. Is there any way to prevent this? Some kind of output redirection perhaps?

推荐答案

pythonw.exe 将在没有命令提示符的情况下运行脚本.问题在于 Python 解释器 Python.exe 与控制台子系统链接以生成控制台输出(因为这是 90% 的情况)——pythonw.exe 与 GUI 子系统链接,而 Windows 不会创建控制台除非它要求一个,否则它的输出窗口.

pythonw.exe will run the script without a command prompt. The problem is that the Python interpreter, Python.exe, is linked against the console subsystem to produce console output (since that's 90% of cases) -- pythonw.exe is instead linked against the GUI subsystem, and Windows will not create a console output window for it unless it asks for one.

本文讨论 GUI用 Python 编程,也暗指 pythonw.exe.它还有助于指出,如果您的 Python 文件以 .pyw 而不是 .py 结尾,标准的 Windows 安装程序将正确设置关联并在 pythonw.exe 中运行您的 Python.

This article discusses GUI programming with Python, and also alludes to pythonw.exe. It also helpfully points out that if your Python files end with .pyw instead of .py, the standard Windows installer will set up associations correctly and run your Python in pythonw.exe.

就您而言,这听起来不是问题,但对 pythonw.exe 的依赖使您的应用程序特定于 Windows -- 存在其他解决方案可以在 Mac OS X 上实现此目的.

In your case it doesn't sound like a problem, but reliance upon pythonw.exe makes your application Windows-specific -- other solutions exist to accomplish this on, say, Mac OS X.

这篇关于在不出现 Windows 控制台的情况下运行 Python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆