簡體   English   中英

從Python setuptools創建可啟動的GUI腳本(沒有控制台窗口!)

[英]Create launchable GUI script from Python setuptools (without console window!)

我目前為基於Python的GUI添加可執行文件的方式是:

setup(
      # ...
      entry_points = {"gui_scripts" : ['frontend = myfrontendmodule.launcher:main']},
      # ...
      )

在Windows上,這將在Python的腳本文件夾(使用Python 2.6)中創建“frontend.exe”和“frontend-script.pyw”。 當我執行EXE文件時,會顯示一個控制台窗口,但PYW文件無法正常顯示。

所以我的問題是:如何在沒有控制台窗口的情況下讓EXE文件執行程序? 該解決方案也適用於Linux(不建議使用py2exe;)。

好吧,我在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”)但用戶可能已在“C:\\ Python26”中安裝了Python ”。 在我報告此問題后,我將報告最終解決方案。


兩年前我發布了這個,對不起,我還沒有提供我的解決方案。 不確定是否有更現代的解決方案(可能分發提供的東西),但這是我當時使用的(復制粘貼):

文件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多年來沒有得到任何更新,因此該解決方案仍然有效。

為什么不使用Linux的.pyw文件和Windows的py2exe

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM