为python GTK3应用程序创建安装程序

Noa*_*Gal 8 python gtk cross-platform python-2.7 gtk3

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

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

Noa*_*Gal 10

在搜索互联网寻找解决方案几天后,我终于偶然发现了这篇博文,帮助我创建了一个.exe来运行Windows上的程序,这是我的主要目标.

为了将来参考,这些是我采取的步骤:

  • 确保安装了Python,安装了GTK + 3(从此处安装)和PyGObject(从此处安装).重要提示:安装PyGObject时,请确保选择Gtk + AND 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))
Run Code Online (Sandbox Code Playgroud)
  • 运行setup.py,最好从命令行输入python setup.py py2exe,它将在项目的文件夹中创建两个新目录:\ build\ dist.该\ DIST文件夹是我们关心的,则在其中,你会发现所有必要的DLL和你的程序的新创建的.exe文件,你的主要Python脚本(在我的情况MAIN.EXE)而得名.

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


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