9

我目前为基于 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 ;)。

4

2 回答 2

12

好吧,我在 setuptools 源代码中进行了一些调查,这一切都归结为 setuptools (easy_install.py) 中的一个错误:

# On Windows/wininst, add a .py extension and an .exe launcher
if group=='gui_scripts':
    ext, launcher = '-script.pyw', 'gui.exe'
    old = ['.pyw']
    new_header = re.sub('(?i)python.exe','pythonw.exe',header)
else:
    ext, launcher = '-script.py', 'cli.exe'
    old = ['.py','.pyc','.pyo']
    new_header = re.sub('(?i)pythonw.exe','python.exe',header)

if os.path.exists(new_header[2:-1]) or sys.platform!='win32':
    hdr = new_header
else:
    hdr = header

最后一条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 安装程序/MSI 安装程序,shebang 将包含我的 pythonw.exe 路径(“C:\Program Files (x86)\Python26\pythonw.exe”),但用户可能已将 Python 安装在“C:\Python26”中”。在我报告了这个问题之后,我会报告最终的解决方案。


我在两年前发布了这个,很抱歉我还没有提供我的解决方案。不确定是否有更现代的解决方案(可能分发提供了一些东西),但这是我当时使用的(复制粘贴):

文件dogsync-frontend-script.pyw

#!pythonw.exe

# This script will be executed by the primary Python version that is installed, which might as well be Python 3. But
# we want to execute it with the Python version that belongs to this script's path. So let's do a major hack:

import os
import sys
import subprocess

if sys.argv[-1] == "magic":
    from dogsync_frontend.launcher import main
    main()
else:
    # The CPython folder hierarchy is assumed here (<installation>\pythonw.exe, <installation>\Scripts\<thisscript>)
    subprocess.Popen([os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "pythonw.exe")),
                      __file__,
                      "magic"])

文件dogsync-frontend.exe

自动复制自<python installation>\lib\site-packages\setuptools\gui.exe(见下文)。<name of EXE>-script.py[w]如果我没记错的话,这个文件会自动执行脚本。

文件setup.py

from setuptools import __file__ as setupToolsFilename

if os.name == "nt":
    # Use a customized (major hack) start script instead of the one that gets automatically created by setuptools
    # when the "gui_scripts" parameter is used. This way, we don't need setuptools installed in order to run DogSync.
    shutil.copy2(os.path.join(os.path.dirname(setupToolsFilename), "gui.exe"),
                 "build-environment/windows-scripts/dogsync-frontend.exe")
    startScripts = dict(scripts = ["build-environment/windows-scripts/dogsync-frontend-script.pyw",
                                   "build-environment/windows-scripts/dogsync-frontend.exe"])
else:
    # For Linux, I don't have a solution to remove the runtime dependency on setuptools (yet)
    startScripts = dict(entry_points = {"gui_scripts" : ['dogsync-frontend = dogsync_frontend.launcher:main']})

setup(<other options>,
      **startScripts)

使用此设置,将 exe/pyw 文件复制到<python installation>\Scripts(在 Windows 上)并且启动dogsync-frontend.exe将在没有控制台的情况下运行 pyw 脚本。由于 setuptools 多年来没有得到任何更新,因此该解决方案仍然有效。

于 2010-08-22T17:06:40.550 回答
0

为什么不将.pyw文件用于 Linux 和py2exeWindows?

于 2010-08-22T15:58:07.283 回答