Linux 上的 cx_freeze PyGObject 应用程序

Posted

技术标签:

【中文标题】Linux 上的 cx_freeze PyGObject 应用程序【英文标题】:cx_freeze PyGObject application on Linux 【发布时间】:2015-12-15 09:31:20 【问题描述】:

我有一个简单的 PyGObject 应用程序:

from gi.repository import Gtk


class Window(Gtk.Window):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.set_border_width(5)

        self.button = Gtk.Button('Test')

        self.box = Gtk.Box()
        self.box.pack_start(self.button, True, True, 0)

        self.add(self.box)
        self.connect('delete-event', Gtk.main_quit)
        self.show_all()


win = Window()
Gtk.main()

我正在尝试使用以下setup.py 脚本在 Linux 上使用 cx_freeze 冻结它:

from cx_Freeze import setup, Executable

setup(name="GUI Test",
      description="GUITest",
      version="0.1",
      options="build_exe": "build_exe": "Bin/pygobject",
                             "create_shared_zip": False,
                             ,
      executables=[Executable(script="hello_pygobject.py",
                              targetName="hello",
                              appendScriptToExe=True,
                              )]
      )

我是这样运行的:python3 setup_pygobject.py build

当我尝试运行冻结的应用程序时,我收到以下错误消息:

(process:22538): Gtk-CRITICAL **: gtk_settings_get_for_screen: assertion 'GDK_IS_SCREEN (screen)' failed
Segmentation fault (core dumped)

我可能在目录中缺少一堆库,例如they have explained for Windows。我试过pmap 来查找丢失的库。作为最后的手段,我还尝试将/usr/lib64/(+一些我不记得的其他库)中的所有库复制到工作目录。

有没有找到我需要的库的好方法或修复它的更好方法?

相关问题,但适用于 Windows: Python3 + PyGobject + GTK3 and cx_freeze missing DLLs

编辑:

我尝试使用dbg 来调查核心转储。我得到了这个:

...
Core was generated by `./hello'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00007fd3d009d4ef in _gtk_settings_get_style_cascade () from /lib64/libgtk-3.so.0

当我安装单独的调试信息时,我得到了这个:

...
Core was generated by `./hello'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  _gtk_settings_get_style_cascade (settings=0x0, scale=scale@entry=1) at gtksettings.c:1764
1764      GtkSettingsPrivate *priv = settings->priv;

通过gdb 运行应用程序时,我得到了这个:

...
(process:21699): Gtk-CRITICAL **: gtk_settings_get_for_screen: assertion 'GDK_IS_SCREEN (screen)' failed

Program received signal SIGSEGV, Segmentation fault.
_gtk_settings_get_style_cascade (settings=0x0, scale=scale@entry=1) at gtksettings.c:1764
1764      GtkSettingsPrivate *priv = settings->priv;

堆栈跟踪:

(gdb) backtrace
#0  0x00007fffed62c4ef in _gtk_settings_get_style_cascade (settings=0x0, scale=scale@entry=1) at gtksettings.c:1764
#1  0x00007fffed6422d0 in gtk_style_context_init (style_context=0x904080 [GtkStyleContext]) at gtkstylecontext.c:425
#2  0x00007fffef86556b in g_type_create_instance (type=9442240) at gtype.c:1870
#3  0x00007fffef8480ab in g_object_new_internal (class=class@entry=0x901490, params=params@entry=0x0, n_params=n_params@entry=0) at gobject.c:1774
#4  0x00007fffef849991 in g_object_newv (object_type=object_type@entry=9442240, n_parameters=n_parameters@entry=0, parameters=parameters@entry=0x0) at gobject.c:1921
#5  0x00007fffef84a2c4 in g_object_new (object_type=9442240, first_property_name=first_property_name@entry=0x0) at gobject.c:1614
#6  0x00007fffed63f709 in gtk_style_context_new () at gtkstylecontext.c:994
#7  0x00007fffed46086f in _gtk_style_new_for_path (screen=0x0, path=path@entry=0x83dd70) at deprecated/gtkstyle.c:853
#8  0x00007fffed4608ee in gtk_style_new () at deprecated/gtkstyle.c:887
#9  0x00007fffed463569 in gtk_widget_get_default_style () at deprecated/gtkstyle.c:4057
#10 0x00007fffed6dfde5 in gtk_widget_init (widget=<optimized out>) at gtkwidget.c:4510
#11 0x00007fffef865527 in g_type_create_instance (type=9319840) at gtype.c:1864
#12 0x00007fffef8480ab in g_object_new_internal (class=class@entry=0x900b90, params=params@entry=0x0, n_params=n_params@entry=0) at gobject.c:1774
#13 0x00007fffef849991 in g_object_newv (object_type=9319840, n_parameters=0, parameters=0x0) at gobject.c:1921
#14 0x00007fffefece19c in pygobject_constructv () at /home/skontar/Python/Examples/GUI/Bin/pygobject/gi._gi.so
#15 0x00007fffefed57c1 in pygobject_init () at /home/skontar/Python/Examples/GUI/Bin/pygobject/gi._gi.so
#16 0x00007ffff79fec4c in wrap_init () at /home/skontar/Python/Examples/GUI/Bin/pygobject/libpython3.4m.so.1.0
#17 0x00007ffff79adf31 in PyObject_Call () at /home/skontar/Python/Examples/GUI/Bin/pygobject/libpython3.4m.so.1.0
#18 0x00007ffff7a60a9f in PyEval_EvalFrameEx () at /home/skontar/Python/Examples/GUI/Bin/pygobject/libpython3.4m.so.1.0
#19 0x00007ffff7a65de6 in PyEval_EvalCodeEx () at /home/skontar/Python/Examples/GUI/Bin/pygobject/libpython3.4m.so.1.0
#20 0x00007ffff79d65f8 in function_call () at /home/skontar/Python/Examples/GUI/Bin/pygobject/libpython3.4m.so.1.0
#21 0x00007ffff79adf31 in PyObject_Call () at /home/skontar/Python/Examples/GUI/Bin/pygobject/libpython3.4m.so.1.0
#22 0x00007ffff79c3344 in method_call () at /home/skontar/Python/Examples/GUI/Bin/pygobject/libpython3.4m.so.1.0
#23 0x00007ffff79adf31 in PyObject_Call () at /home/skontar/Python/Examples/GUI/Bin/pygobject/libpython3.4m.so.1.0
#24 0x00007ffff7a057e0 in slot_tp_init () at /home/skontar/Python/Examples/GUI/Bin/pygobject/libpython3.4m.so.1.0
#25 0x00007ffff7a03276 in type_call () at /home/skontar/Python/Examples/GUI/Bin/pygobject/libpython3.4m.so.1.0
#26 0x00007ffff79adf31 in PyObject_Call () at /home/skontar/Python/Examples/GUI/Bin/pygobject/libpython3.4m.so.1.0
#27 0x00007ffff7a5de0a in PyEval_EvalFrameEx () at /home/skontar/Python/Examples/GUI/Bin/pygobject/libpython3.4m.so.1.0
#28 0x00007ffff7a65de6 in PyEval_EvalCodeEx () at /home/skontar/Python/Examples/GUI/Bin/pygobject/libpython3.4m.so.1.0
#29 0x00007ffff7a65e8b in PyEval_EvalCode () at /home/skontar/Python/Examples/GUI/Bin/pygobject/libpython3.4m.so.1.0
#30 0x00007ffff7a5936a in builtin_exec () at /home/skontar/Python/Examples/GUI/Bin/pygobject/libpython3.4m.so.1.0
#31 0x00007ffff7a64f1f in PyEval_EvalFrameEx () at /home/skontar/Python/Examples/GUI/Bin/pygobject/libpython3.4m.so.1.0
#32 0x00007ffff7a65de6 in PyEval_EvalCodeEx () at /home/skontar/Python/Examples/GUI/Bin/pygobject/libpython3.4m.so.1.0
#33 0x00007ffff7a65e8b in PyEval_EvalCode () at /home/skontar/Python/Examples/GUI/Bin/pygobject/libpython3.4m.so.1.0
#34 0x000000000040192c in  ()
#35 0x00007ffff738f700 in __libc_start_main (main=0x4011e0, argc=1, argv=0x7fffffffe598, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffffe588)
    at libc-start.c:289
#36 0x0000000000401c69 in  ()

编辑 2:

相关包:

python3.x86_64            3.4.2-6.fc22
python3-gobject.x86_64    3.16.2-1.fc22   

cx-Freeze (4.3.4)

【问题讨论】:

您可以尝试通过gdb 运行它以获取有关分段错误性质的更多详细信息(或使用 gdb 检查核心转储)。 @proycon 我试过了(以前从未这样做过)。但我发现输出没有任何用处。 是的,尽管至少您现在知道段错误是由 gtk3 库中的空指针引起的(settings == NULL 所以settings-&gt;priv 段错误)。不幸的是,我自己并不熟悉 cx_Freeze。这可能很难破解。 段错误时的堆栈跟踪是什么?也许您可以找出设置指针的来源,并找到解决方法来为其分配一些东西。 @sibaz 我并没有真正考虑修复 PyGObject。它在从 Python 运行时有效,但在 cx_freezed 时无效。我能够为 Tkinter、wxPython 和 PyQt 做同样的事情,在所有情况下,它都是一些缺少的库。所以我认为这是类似的情况,但在这种情况下,我无法理解缺少什么。维基页面上的 Windows 解释也让我得出了这个结论。无论如何,我包括了踪迹。 【参考方案1】:

所以问题不在于缺少库(因为它们无论如何都在路径中),而是setup.py 脚本中缺少包。正确的设置如下:

from cx_Freeze import setup, Executable

setup(name="GUI Test",
      description="GUITest",
      version="0.1",
      options="build_exe": "build_exe": "Bin/pygobject",
                             "create_shared_zip": False,
                             "packages": ["gi"],
                             ,
      executables=[Executable(script="hello_pygobject.py",
                              targetName="hello",
                              appendScriptToExe=True,
                              )]
      )

【讨论】:

很好地找到它。对不起,我没有更多帮助:-) @sibaz 感到自豪,你间接地让我走向了正确的方向:-)。

以上是关于Linux 上的 cx_freeze PyGObject 应用程序的主要内容,如果未能解决你的问题,请参考以下文章

Python 3.5.3 上的 cx_Freeze 错误

cx_freeze 问题与 mac 上的相对路径

Windows cx_freeze 上的“AttributeError:‘NoneType’对象没有属性写入”

CX_Freeze 和 guiqwt 适用于 windows 而不是 linux

Python 2.7 Cx_Freeze: ImportError: No module named __startup__

Python 2.7 Cx_Freeze: ImportError: No module named __startup__