提出ValueError('命名空间%s不可用'%命名空间)ValueError:命名空间Gtk不可用

时间:2018-05-20 23:43:28

标签: python windows gtk cx-freeze gdk

我在python上有一个小程序,使用python 3.4。但是,我无法在Windows上使用Cx_freeze将其设置为.exe。

我收到错误:

\build\exe.win-amd64-3.4>lcp.exe
Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in <module>
    exec(code, m.__dict__)
  File "LCP.py", line 5, in <module>
  File "C:\Python34\lib\site-packages\gi\__init__.py", line 118, in require_version
    raise ValueError('Namespace %s not available' % namespace)
ValueError: Namespace Gtk not available

尝试使用cmd运行.exe时,只需双击即可正常运行。我只会看到一个打开并崩溃的屏幕。

该程序使用gi.repository并在开始时使用此代码:

import  gi
gi.require_version('Gtk', '3.0')
from    gi.repository   import  Gtk,Gdk

我的setup.py就是这样:

import os
from cx_Freeze import setup, Executable
import sys
import  gi
gi.require_version('Gtk', '3.0')
from    gi.repository   import  Gtk,Gdk



build_exe_options = { "include_msvcr": True}

# Frozen executables made by build_exe need to have everything that GTK
# loads at runtime available under the root where the built .exe goes.

common_include_files = []


# On MSYS2's MINGW64/32 enrironments, the DLLs we want are in PATH.
# Typically this means /mingw64/bin/libwhatever.dll.  An alternative way
# of forming the list of required DLLs would be to parse the relevant
# .gir files (see below). However, libpangowin32-1.0-0.dll wouldn't be
# listed.

required_dll_search_paths = os.getenv("PATH", os.defpath).split(os.pathsep)
required_dlls = [
    'libgtk-3-0.dll',
    'libgdk-3-0.dll',
    'libepoxy-0.dll',
    'libgdk_pixbuf-2.0-0.dll',
    'libpango-1.0-0.dll',
    'libpangocairo-1.0-0.dll',
    'libpangoft2-1.0-0.dll',
    'libpangowin32-1.0-0.dll',
    'libatk-1.0-0.dll',
]

for dll in required_dlls:
    dll_path = None
    for p in required_dll_search_paths:
        p = os.path.join(p, dll)
        if os.path.isfile(p):
            dll_path = p
            break
    assert dll_path is not None, \
        "Unable to locate {} in {}".format(
            dll,
            dll_search_path,
        )
    common_include_files.append((dll_path, dll))


# We need the .typelib files at runtime.
# The related .gir files are in $PREFIX/share/gir-1.0/$NS.gir,
# but those can be omitted at runtime.

required_gi_namespaces = [
    "Gtk-3.0",
    "Gdk-3.0",
    "cairo-1.0",
    "Pango-1.0",
    "GObject-2.0",
    "GLib-2.0",
    "Gio-2.0",
    "GdkPixbuf-2.0",
    "GModule-2.0",
    "Atk-1.0",
]


# Packages that need to be bundled go here.

common_packages = [
    "gi","gtk-3.0", "Gdk-3.0" # always seems to be needed
    # "cairo",   # Only needed (for foreign structs) if no "import cairo"s
]


ejecutable = Executable(
    script ="LCP.py",

    #compress = True,
    #copyDependenFile = True,
    #appendScriptToExe =True,
    #appendScriptToLibrary = True,
    #icon = "imagen"
    )


includes =['os','gi','gi.overrides']
packages = ['gi', 'os']

include_files= ['respuestasFrecuentes.txt']     #archivos extra q no sean .py


setup(
        name = 'Respuestas Frecuentes',
        author = 'Irina Marcano',
        version = '0.1.0',
        description = 'facilidad para modificar tus respuestas recientes',
        executables= [ejecutable],
        options = {"build_exe":{
            "includes": includes,
            "packages": packages,
            "optimize": 2,
            "include_files": include_files,
            #"include_msvcr": include_msvcr,
        } }
    )

那个setup.py花了我几天时间才开始制作,因为它遇到了麻烦。到目前为止,甚至没有从文件夹上的库复制DLL工作。我一直坚持这个错误,我不知道现在该做什么。我正在使用Windows 10,在Windows 8上它也不会工作并给出与开始时所述相同的错误。在.py上打开程序可以很好地工作,甚至可以使用python 3.6打开,减去一些错误,因为它缺少gi和gtk。

注意当使用python setup.py构建时,我收到以下消息:

    Missing modules:
? UserList imported from pygtkcompat.pygtkcompat
? __main__ imported from bdb, pdb
? _dummy_threading imported from dummy_threading
? _posixsubprocess imported from subprocess
? _winreg imported from platform
? ce imported from os
? gi.repository.Atk imported from pygtkcompat.pygtkcompat
? gi.repository.GLib imported from gi.overrides.Dee, gi.overrides.GObject, gi.overrides.Gio, pygtkcompat.pygtkcompat
? gi.repository.GObject imported from gi.overrides.GExiv2, gi.overrides.Gedit, gi.overrides.Gtk, gi.overrides.Vips, pygtkcompat.generictreemodel, pygtkcompat.pygtkcompat
? gi.repository.GUdev imported from pygtkcompat.pygtkcompat
? gi.repository.Gdk imported from lcp__main__, pygtkcompat.pygtkcompat
? gi.repository.GdkPixbuf imported from pygtkcompat.pygtkcompat
? gi.repository.GdkX11 imported from gi.overrides.Gdk
? gi.repository.Gio imported from gi.overrides.Ggit, pygtkcompat.pygtkcompat
? gi.repository.GooCanvas imported from pygtkcompat.pygtkcompat
? gi.repository.Gst imported from gi.overrides.GstPbutils, pygtkcompat.pygtkcompat
? gi.repository.GstAudio imported from pygtkcompat.pygtkcompat
? gi.repository.GstBase imported from pygtkcompat.pygtkcompat
? gi.repository.GstController imported from pygtkcompat.pygtkcompat
? gi.repository.GstInterfaces imported from pygtkcompat.pygtkcompat
? gi.repository.GstPbutils imported from pygtkcompat.pygtkcompat
? gi.repository.GstVideo imported from pygtkcompat.pygtkcompat
? gi.repository.Gtk imported from lcp__main__, pygtkcompat.generictreemodel, pygtkcompat.pygtkcompat
? gi.repository.Pango imported from pygtkcompat.pygtkcompat
? gi.repository.PangoCairo imported from pygtkcompat.pygtkcompat
? gi.repository.Poppler imported from pygtkcompat.pygtkcompat
? gi.repository.Vte imported from pygtkcompat.pygtkcompat
? gi.repository.WebKit imported from pygtkcompat.pygtkcompat
? gi.repository.cairo imported from gi.overrides.Gdk
? grp imported from shutil, tarfile
? java.lang imported from platform
? org.python.core imported from copy
? os.path imported from os, pkgutil, py_compile, shutil, unittest.util
? posix imported from os
? pwd imported from http.server, posixpath, shutil, tarfile, webbrowser
? termios imported from tty
? vms_lib imported from platform
This is not necessarily a problem - the modules may not be needed on this platform.

我还不知道如何删除大部分内容,因为我的程序只使用了我需要gi才能访问的gtk和gdk。

1 个答案:

答案 0 :(得分:0)

我也遇到了这个问题,并花了很多时间寻找解决方案。

  1. 将此部分添加到上面编写的cx_freeze的setup.py中:

    for ns in required_gi_namespaces:
        subpath = "Lib\girepository-1.0\{}.typelib".format(ns)
        fullpath = os.path.join(sys.prefix, subpath)
        assert os.path.isfile(fullpath), (
            "Required file {} is missing" .format(
                fullpath,
        ))
        common_include_files.append((fullpath, subpath)) 
    
  2. 创建一个名为'GI_TYPELIB_PATH'的新环境变量,并将复制的gi_repository文件夹添加到您的项目\ lib目录中,并添加为其值。或使用os.path.dirname(sys.executable)

  3. 在运行时动态添加此路径