使用 cx_Freeze 将 OpenGl 脚本从 .py 转换为 .exe 时出现 NoneType 错误

NoneType error converting OpenGl script from .py to .exe using cx_Freeze

我有一些 python 脚本,我可以使用 cx_Freeze 相对轻松地将它们转换为 .exe,但是我在使用 opengl 创建的 rubixcube 时遇到了问题。

我有两个脚本;

import sys
from cx_Freeze import setup, Executable
#go to dir in cmd, run python setup.py build or; setup.py build... will create a folder build with name.exe
setup(
    name = "Cube",
    version = "1.1",
    description = "Cube",
    executables = [Executable("OwnCube.py", base = "Console")])

OwnCube.py 简直是;

import pygame

from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *

然后它会在 'Cube' 下保存一个构建文件夹,其中包含一个名为 'OwnCube.exe' 的 .exe 文件以及所有 dll 和数据文件。 运行 OwnCube.exe 在 cmd 中给出非类型错误。

G:\Programs\PersonalPrograms\PythonScripts\Cube\build\exe.win32-3.6>OwnCube.exe
Traceback (most recent call last):
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 14, in run
    module.run()
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26, in run
    exec(code, m.__dict__)
  File "OwnCube.py", line 5, in <module>
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenGL\GL\__init__.py", line 3, in <module>
    from OpenGL import error as _error
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenGL\error.py", line 12, in <module>
    from OpenGL import platform, _configflags
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenGL\platform\__init__.py", line 35, in <module>
    _load()
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenGL\platform\__init__.py", line 29, in _load
    plugin = plugin_class()
TypeError: 'NoneType' object is not callable 

我已经在其他库中多次执行此操作,但我似乎无法在 OpenGl 中使用它,我是否遗漏了什么?

经过多次拔毛。

原来我需要在设置中包含一个选项行以包含 OpenGL 库。然后我必须将环境变量设置为 TCL_LIBRARY 和 TK_LIBRARY 或手动设置。我在脚本中这样做如下:

import sys
import os
from cx_Freeze import setup, Executable



build_exe_options = {"includes": ["OpenGL"]}
os.environ['TCL_LIBRARY'] = r'G:\python path\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'G:\python path\tcl\tcl8.6'
#go to dir in cmd, run python setup.py build or; setup.py build... will create a folder build with name.exe
setup(
    name = "Cube",
    version = "1.1",
    description = "Cube",
    options = {"build_exe": {"packages": ["OpenGL"]}},
    executables = [Executable("OwnCube.py", base = "Console")]
    )

我真的不明白为什么会这样,我的猜测是在将 OpenGL 库编译为 .dll 或 .so 文件时找不到 OpenGL 内部调用的库 tkinter 或 tcl。

希望这对未来的搜索者有所帮助。

我也遇到了这个问题,为了解决这个问题,我所做的就是在 setup.py 的 includes 中添加 OpenGL.platform.win32。我用win10。 这个错误的原因是:

def load( self ):
    """Attempt to load and return our entry point"""
    try:
        return **importByName**( self.import_path )
    except ImportError as err:
        return None
def importByName( fullName ):
    """Import a class by name"""
    name = fullName.split(".")
    moduleName = name[:-1]
    className = name[-1]
    module = __import__( ".".join(moduleName), {}, {}, moduleName)
    return getattr( module, className )`

cx_freeze 无法在您的代码中捕获 __import__,因此我们需要使用 import 而不是 __import__