Question

I have tried using py2exe to make an executable of my PyOpenGL project, and I did... The problem is, it is not running... The exe is not running and i am getting this log:

Traceback (most recent call last):
File "gl.py", line 1, in <module>
ImportError: No module named OpenGL.GL

setup.py looks like this:

from distutils.core import setup
import py2exe

setup(windows=['gl.py'],
      options={
          "py2exe": {
              "includes": ["ctypes", "logging"],
              "excludes": ["OpenGL"],
              }
          }
      )

My project looks like this:

from OpenGL.GL import *
from OpenGL.GLU import *
import random
from math import * 

import pygame

import Image
import sys
import time


sys.path += ['.']


pygame.init() 
pygame.display.set_mode((800,600), pygame.OPENGL|pygame.DOUBLEBUF)

def jpg_file_write(name, number, data):
    im = Image.frombuffer("RGBA", (800,600), data, "raw", "RGBA", 0, 0)
    fnumber = "%05d" % number
    im.save(name + fnumber + ".jpg")


glEnable(GL_DEPTH_TEST)


def createAndCompileShader(type,source):
    shader=glCreateShader(type)
    glShaderSource(shader,source)
    glCompileShader(shader)



    result=glGetShaderiv(shader,GL_COMPILE_STATUS)

    if (result!=1): 
        raise Exception("Greska u kompajliranju... \nLog:\n"+glGetShaderInfoLog(shader))
    return shader


vertex_shader=createAndCompileShader(GL_VERTEX_SHADER,"""
varying vec3 v;
varying vec3 N;

void main(void)
{

   v = gl_ModelViewMatrix * gl_Vertex;
   N = gl_NormalMatrix * gl_Normal;

   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

}
""");

fragment_shader=createAndCompileShader(GL_FRAGMENT_SHADER,"""
varying vec3 N;
varying vec3 v;

void main(void)
{
   vec3 L = gl_LightSource[0].position.xyz-v;

   // "Lambert's law"? (see notes)
   // Rather: faces will appear dimmer when struck in an acute angle
   // distance attenuation

   float Idiff = max(dot(normalize(L),N),0.0)*pow(length(L),-2.0); 

   gl_FragColor = vec4(0.5,0,0.5,1.0)+ // purple
                  vec4(1.0,1.0,1.0,1.0)*Idiff; // diffuse reflection
}
""");



program=glCreateProgram()
glAttachShader(program,vertex_shader)
glAttachShader(program,fragment_shader)
glLinkProgram(program)


try:
    glUseProgram(program)   
except OpenGL.error.GLError:
    print glGetProgramInfoLog(program)
    raise

done = False

t=0 



glNewList(1,GL_COMPILE)

glBegin(GL_QUADS)

glColor3f(1,1,1)

glNormal3f(0,0,-1)
glVertex3f( -1, -1, -1)
glVertex3f(  1, -1, -1)
glVertex3f(  1,  1, -1)
glVertex3f( -1,  1, -1)

glNormal3f(0,0,1)
glVertex3f( -1, -1,  1)
glVertex3f(  1, -1,  1)
glVertex3f(  1,  1,  1)
glVertex3f( -1,  1,  1)

glNormal3f(0,-1,0) 
glVertex3f( -1, -1, -1)
glVertex3f(  1, -1, -1)
glVertex3f(  1, -1,  1)
glVertex3f( -1, -1,  1)

glNormal3f(0,1,0) 
glVertex3f( -1,  1, -1)
glVertex3f(  1,  1, -1)
glVertex3f(  1,  1,  1)
glVertex3f( -1,  1,  1)

glNormal3f(-1,0,0)     
glVertex3f( -1, -1, -1)
glVertex3f( -1,  1, -1)
glVertex3f( -1,  1,  1)
glVertex3f( -1, -1,  1)                      

glNormal3f(1,0,0)        
glVertex3f(  1, -1, -1)
glVertex3f(  1,  1, -1)
glVertex3f(  1,  1,  1)
glVertex3f(  1, -1,  1)

glEnd()
glEndList()

while not done:

    t=t+1

    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(90,1,0.01,1000)
    gluLookAt(sin(t/260.0)*4,cos(t/260.0)*4,cos(t/687.0)*3,0,0,0,0,1,0)

    glClearColor(0.0, 0.0, 0.0, 1.0)
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

    glMatrixMode(GL_MODELVIEW)



    ld=[sin(t/16.0)*4.0,sin(t/20.0)*4.0,cos(t/16.0)*4.0]



    glLightfv(GL_LIGHT0,GL_POSITION,[ld[0],ld[1],ld[2]]);



    glColor3f(1,1,1)

    glLoadIdentity()


    for i in range(-5,5):
        for j in range(-5,5):
            for k in range(-5,5):
                glPushMatrix()

                glTranslate(i,j,k)
                glScale(0.1,0.1,0.1)
                glCallList(1)
                glPopMatrix()

    pygame.display.flip()

Does anyone know what might be causing this problem?

Was it helpful?

Solution

When you exclude the package in py2exe you need to then manually add it to the resulting "dist" directory. That is, you would copy the whole of the OpenGL package from your site-packages directory into the py2exe dist directory.

I've done an experiment with PyOpenGL 3.1.0b2 and py2exe using explicit includes such that only those parts of PyOpenGL you use are included. The results are written up in a blog post. For your purposes (using Pygame) you shouldn't need to worry about the lack of GLUT/GLE DLLS, and on a real Win32 machine it may simply work as expected (the tests failed on my VM).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top