【问题标题】:Creating an installer for a python GTK3 application为 python GTK3 应用程序创建安装程序
【发布时间】:2014-08-19 11:03:18
【问题描述】:

我刚刚使用 Gtk3 为 GUI 开发了一个 Python 2.7 应用程序。 我的问题是,我现在如何为我的最终用户创建适用于 Windows、Mac 和 Linux 的安装程序(可能是三种不同的安装程序),以便轻松下载应用程序,而无需下载 python 和 GTK 等。

我以前从未从 python 脚本创建安装程序。我听说有一些用于此目的的工具(py2exe?pyinstaller?),但我不知道如何以及如何与它们打包以使其能够使用 Gtk3。

【问题讨论】:

  • 如果你想为 Linux 做一个安装程序,你会发现有不同类型的包..(对于 ach linux,对于基于 debian 的......等等)..如果你说 linux,你的意思是 GNU -Linux Ubuntu 或 Debian、Mint 等我向您推荐我对 this 线程的回答。只需几分钟。
  • 对 GNU-Linux 使用 Debian Package。但请注意,这仅适用于基于 Debian/Ubuntu 的发行版!还有更多种类的分布。

标签: python python-2.7 gtk cross-platform gtk3


【解决方案1】:

在互联网上搜索了几天的解决方案后,我终于偶然发现了this blog-post,它帮助我创建了一个在 Windows 上运行程序的 .exe,这是我的主要目标。

为了将来参考,我采取了以下步骤:

  • 确保您已安装 Python,并安装了 GTK+3 (install from here) 和 PyGObject (install from here)。 重要提示:安装 PyGObject 时,请确保同时选择 Gtk+ Gstreamer(出于某种原因,py2exe 必须拥有它才能获取所有 dll)

    李>
  • 在与您的项目相同的目录中,创建一个包含以下脚本的新 python 文件。确保将“main.py”更改为您自己的主脚本文件(启动您的应用程序的文件)的名称:


setup.py

from distutils.core import setup 
import py2exe 
import sys, os, site, shutil

site_dir = site.getsitepackages()[1] 
include_dll_path = os.path.join(site_dir, "gnome") 

gtk_dirs_to_include = ['etc', 'lib\\gtk-3.0', 'lib\\girepository-1.0', 'lib\\gio', 'lib\\gdk-pixbuf-2.0', 'share\\glib-2.0', 'share\\fonts', 'share\\icons', 'share\\themes\\Default', 'share\\themes\\HighContrast'] 

gtk_dlls = [] 
tmp_dlls = [] 
cdir = os.getcwd() 
for dll in os.listdir(include_dll_path): 
    if dll.lower().endswith('.dll'): 
        gtk_dlls.append(os.path.join(include_dll_path, dll)) 
        tmp_dlls.append(os.path.join(cdir, dll)) 

for dll in gtk_dlls: 
    shutil.copy(dll, cdir) 

# -- change main.py if needed -- #
setup(windows=['main.py'], options={ 
    'py2exe': { 
        'includes' : ['gi'], 
        'packages': ['gi'] 
    } 
}) 

dest_dir = os.path.join(cdir, 'dist') 

if not os.path.exists(dest_dir):
    os.makedirs(dest_dir)

for dll in tmp_dlls: 
    shutil.copy(dll, dest_dir) 
    os.remove(dll) 

for d  in gtk_dirs_to_include: 
    shutil.copytree(os.path.join(site_dir, "gnome", d), os.path.join(dest_dir, d))

  • 运行 setup.py,最好从命令行键入 python setup.py py2exe,它会在您的项目文件夹中创建两个新目录:\build\dist\dist 文件夹是我们关心的文件夹,您将在其中找到所有必要的 Dll,以及您的程序新创建的 .exe,以您的主要 python 脚本命名(在我的例子中为 main.exe) .

  • 重要的附加说明:如果您在项目中使用任何非 python 文件(在我的情况下为 css 文件),请务必将其复制并粘贴到 \dist 目录,否则应用将无法找到它!


据我所知,此解决方案仅适用于分发 Windows(这是我的主要目标),但未来我将尝试对 Linux 和 OSX 做同样的事情,我会更新相应的答案

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-17
    • 1970-01-01
    • 2013-05-13
    • 2012-03-13
    • 2013-01-19
    • 2011-01-26
    • 1970-01-01
    • 2021-08-12
    相关资源
    最近更新 更多