OpenGL Pyglet "Error: global name 'texture' not defined"
Question: The error is on line 5: glBindTexture(texture.target, texture.id)

    
    
    1. import pyglet
    2. from pyglet.gl import *
    3. class CustomGroup(pyglet.graphics.Group):
    4.    def set_state(self):
    5.        glEnable(texture.target)
    6.        glBindTexture(texture.target, texture.id)
    7.    def unset_state(self):
    8.        glDisable(texture.target)
    

NameError: global name 'texture' not defined but i imported it on line 2. the
full code is [here](http://pastebin.com/10JbiCYp "code")

Any help? I am using python 2.7.3 with pyglet and ubuntu 12.04


Answer: The `pyglet.gl` module does not define any variable `texture`, so your import
statement in line 2 does not provide anything like that. And what do you even
expect it to contain? You'll need to create your
[`Texture`](http://www.pyglet.org/doc/api/pyglet.image.Texture-class.html)
object yourself.

To do so, you could use the methods
[`pyglet.resource.image`](http://www.pyglet.org/doc/api/pyglet.resource-
module.html#image) or
[`pyglet.resource.texture`](http://www.pyglet.org/doc/api/pyglet.resource-
module.html#texture) or you could load an `AbstractImage` by calling
[`pyglet.image.load`](http://www.pyglet.org/doc/api/pyglet.image-
module.html#load) and retrieve a corresponding `Texture` object by accessing
the image's `texture` member or, say, adding it to a
[`TextureAtlas`](http://www.pyglet.org/doc/programming_guide/image_sequences_and_atlases.html#texture-
bins-and-atlases).

Why don't you add to your code:

    
    
    img = pyglet.image.load('imagefile.png')
    texture = img.texture
    

and don't forget to alter your `vertex_list`
[accordingly](http://www.pyglet.org/doc/programming_guide/vertex_attributes.html)
to make use of the texture.

