在互联网上搜索了几天的解决方案后,我终于偶然发现了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 做同样的事情,我会更新相应的答案