从 Python setuptools 创建可启动的 GUI 脚本(没有控制台窗口!)
我目前为基于 Python 的 GUI 添加可执行文件的方式是这样的:
setup(
# ...
entry_points = {"gui_scripts" : ['frontend = myfrontendmodule.launcher:main']},
# ...
)
在 Windows 上,这将在 Python 的脚本文件夹中创建“frontend.exe”和“frontend-script.pyw”(使用 Python 2.6)。当我执行 EXE 文件时,会显示一个控制台窗口,但 PYW 文件可以正常工作而不显示。
所以我的问题是:如何使 EXE 文件在没有控制台窗口的情况下执行程序?该解决方案也应该适用于 Linux(不建议使用 py2exe ;)。
The way I currently add an executable for my Python-based GUI is this:
setup(
# ...
entry_points = {"gui_scripts" : ['frontend = myfrontendmodule.launcher:main']},
# ...
)
On Windows, this will create "frontend.exe" and "frontend-script.pyw" in Python's scripts folder (using Python 2.6). When I execute the EXE file, a console window is shown but the PYW file works correctly without showing one.
So my question is: How can I make the EXE file execute the program without the console window? The solution should work on Linux, too (don't suggest py2exe ;).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我在 setuptools 源代码中进行了一些研究,这一切都归结为 setuptools 中的一个错误(easy_install.py):
最后一个
if
语句决定是否写入 pythonw.exe 或 python.exe 的路径进入“frontend-script.pyw”的shebang。由于此 shebang 是由创建的 EXE 文件评估的,因此有必要不执行else
语句。问题是new_header[2:-1]
在我的例子中是“C:\Program Files (x86)\Python26\pythonw.exe”(带引号!),所以os .path.exists
表示由于引号的原因它不存在。我将尝试让 setuptools 开发人员纠正这个问题。剩下的问题是 pythonw.exe 的绝对路径。如果我创建 Windows setup/MSI 安装程序,shebang 将包含我的 pythonw.exe 路径(“C:\Program Files (x86)\Python26\pythonw.exe”),但用户可能已在“C:\Python26”中安装了 Python ”。报告这个问题后我会报告最终的解决方案。
我两年多前发布了这篇文章,很抱歉我还没有提供我的解决方案。不确定是否有更现代的解决方案(可能是 distribute 提供东西),但这是我当时使用的(复制粘贴):
文件
dogsync-frontend-script.pyw
文件
dogsync-frontend.exe
自动从
复制\lib\site-packages\setuptools\gui.exe
(见下文)。如果我没记错的话,这个文件会自动执行脚本-script.py[w]
。文件
setup.py
通过此设置,exe/pyw 文件将复制到
\Scripts
(在 Windows 上)并启动dogsync-frontend。 exe
将在没有控制台的情况下运行 pyw 脚本。由于 setuptools 多年来没有得到任何更新,因此该解决方案仍然有效。Alright, I investigated a bit in the setuptools source code and it all boils down to a bug in setuptools (easy_install.py):
The last
if
statement decides whether pythonw.exe's or python.exe's path is written into the shebang of "frontend-script.pyw". As this shebang is evaluated by the created EXE file, it is necessary that theelse
statement is not executed. The problem is thatnew_header[2:-1]
in my case was "C:\Program Files (x86)\Python26\pythonw.exe" (with the quotes!), soos.path.exists
said it does not exist because of the quotes.I will try to get this corrected by the setuptools developers. The remaining problem will be the absolute pythonw.exe path. If I create a Windows setup/MSI installer, the shebang will contain my pythonw.exe path ("C:\Program Files (x86)\Python26\pythonw.exe") but the user might have installed Python in "C:\Python26". I'll report the final solution after I've reported this problem.
I posted this over two years back, sorry that I didn't yet offer my solution. Not sure if there is any more modern solution (probably distribute offers something), but here's what I used back then (copy-pasted):
File
dogsync-frontend-script.pyw
File
dogsync-frontend.exe
Automatically copied from
<python installation>\lib\site-packages\setuptools\gui.exe
(see below). This file will automatically execute the script<name of EXE>-script.py[w]
if I remember correctly.File
setup.py
With this setup, the exe/pyw files are copied to
<python installation>\Scripts
(on Windows) and startingdogsync-frontend.exe
will run the pyw script without a console. Since setuptools did not get any updates for years, this solution is still working.为什么不使用 Linux 的
.pyw
文件和 Windows 的py2exe
文件?Why don't you use the
.pyw
file for Linux andpy2exe
for Windows?