Python2.7+PyOpenGL运行报错:NameError: global name 'BaseFunction' is not defined
NameError: global name 'BaseFunction' is not defined的问题 Hey there! Let's break down this frustrating error you're hitting while following thenewboston's Pygame tutorial. That NameError referencing BaseFunction is nearly always a sign of a broken or incompatible PyOpenGL installation in your Anaconda-Spyder 3.3.4 (Python 2.7) environment.
First, let's recap the error trace you shared to align on the issue:
Traceback (most recent call last): File "<ipython-input-8-72d57f8b0dbe>", line 1, in <module> glEnd() File "/home/cadu/anaconda2/lib/python2.7/site-packages/OpenGL /latebind.py", line 61, in __call__ return self.wrapperFunction( self.baseFunction, *args, **named ) File "/home/cadu/anaconda2/lib/python2.7/site-packages/OpenGL/GL/exceptional.py", line 45, in glEnd return BaseFunction( ) NameError: global name 'BaseFunction' is not defined
What's causing this?
The exceptional.py module in PyOpenGL tries to use BaseFunction but hasn't imported it properly. This usually happens for one of these reasons:
- The PyOpenGL package was corrupted during installation
- You installed a PyOpenGL version that drops support for Python 2.7 (since Python 2.7 is end-of-life, newer versions don't support it)
- There's a dependency conflict in your Anaconda environment
Step-by-step fix:
Uninstall the broken PyOpenGL installation
Open your Anaconda prompt and run:conda uninstall -y pyopengl pyopengl-accelerate # If conda doesn't locate the packages, use pip instead: pip uninstall -y pyopengl pyopengl-accelerateReinstall a Python 2.7-compatible version
The last PyOpenGL versions that support Python 2.7 are3.1.5. Install them with:pip install pyopengl==3.1.5 pyopengl-accelerate==3.1.5We use pip here because conda's repository might not have these older, Python 2.7-friendly versions readily available.
Verify the fix
In Spyder, run this minimal test code to confirmglEnd()works without errors:from OpenGL.GL import glBegin, glEnd, GL_LINES glBegin(GL_LINES) glEnd()
Quick tweak for your cube code:
I noticed your glRotatef(20, 0, 0, 0) line—setting all axis parameters to 0 means the cube won't rotate at all. Try changing it to glRotatef(20, 1, 1, 0) to get a smooth spinning effect once the error is fixed!
内容的提问来源于stack exchange,提问作者Carlos_kuro

