Search code examples
python-3.xopenglcx-freeze

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


I have a few python scripts that I have been able to convert to .exe relatively easily with cx_Freeze but I have hit a bump with a rubixcube I am creating with opengl.

I have two scripts;

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 is simply;

import pygame

from pygame.locals import *

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

It then saves a build folder under 'Cube' with a .exe file called 'OwnCube.exe' along with all the dll's and data files. Running OwnCube.exe in cmd gives a nonetype error.

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 

I have done this multiple times with other libraries but I can't seem to get this working with OpenGl, am I missing something?


Solution

  • i meet this problem too, and all i have done to solve this is add OpenGL.platform.win32 in includes of setup.py. i use win10. reason of this bug is :

    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 can not catch __import__ in your code, so we need to use import instead of __import__.