Wednesday, February 1, 2012

How to install OpenGL in Python? (and some more python stuff)

I wanted to install PyOpenGL and PyGlut. Just because i love to do some programming whenever possible, and OpenGL graphics project is in my hands now.
So, when i go to the Sourceforge page of PyOpenGL, they, even under the 'For the impatient', had given that it's important to install virtualenv before you setup PyOpenGL. I was guessing the meaning of virtualenv and my guesses turned true when i saw the page, which said, "virtualenv is a tool to create isolated Python environments". This means, i can create a virtual environment for every other kind of python project i do, so that when i update my version of python, the projects done with older versions of python will also work. Oh, whatever. So now i had to install virtualenv.
But virtualenv said, the easiest way to install it was to use pip or easy_install. Now, since pip was more recent and built on easy_install, i decided to install pip. Now, here begins the journey.

This site gives proper instructions on how to install pip, a client to PythonPackageIndex (PyPI). The easiest and comfortable way, however, is to do this:

curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python

But this might not work always, especially in ubuntu, when you can do it in two separate steps:
And you'll have to add sudo, probably.

wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py sudo 


python get-pip.py

Now, when i did this, i got the following message:


An error occured while trying to run get-pip.py. Make sure you have setuptools o
r distribute installed


So, now i had to install setuptools. Turns out this installs easy_install too, so i decided to go for it. This site had real complex instructions to install, but installation went smoothly:

sh setuptools-0.6c9-py2.4.egg --prefix=~

Add sudo to the above command if it doesn't work.

Now, back to where i was, I had to run get-pip.py again.

python get-pip.py

Now, this is butter-smooth. For some reason, i'm in love with package managers, they make life so easy, despite making us miss details about lot of intricacies of software installation process :)

sudo pip install virtualenv

Okay. Now virtualenv was ready. Next step.

Reading up about virtualenv, i found it intriguing. So, I read up this small primer from a guy and found it easy to understand :


@bin~>pwd
/home/siddhartha/bin
@bin~>virtualenv --no-site-packages PyOpenGLProject
The --no-site-packages flag is deprecated; it is now the default behavior.
New python executable in PyOpenGLProject/bin/python
Installing setuptools............done.
Installing pip...............done.
@bin~>ls PyOpenGLProject/
bin include lib



This creates a virtualenv, with name PyOpenGLProject. Now, lets change to that directory and activate the virtualenv..


@PyOpenGLProject~>source bin/activate
(PyOpenGLProject)@PyOpenGLProject~>deactivate
@PyOpenGLProject~>


That is how you activate the virtualenv and deactivate. The command source bin/activate will run a script bin/activate and will define a function called deactivate, which can be called at any later moment to deactivate the virtual environment. The implementation of this whole concept is amazingly simple, yet efficient. Its surprising how simple stuff can be :)

Before we install this PyOpenGL, we must take care that python2.6-dev is installed on your computer. Even Numeric (NumPy) should be installed.

Now we'll reactivate the virtualenv and install PyOpenGL.

(PyOpenGLProject)@PyOpenGLProject~>pip install OpenGLContext-full

 Well, guess what? You're done. This is much easier than it is in any other way! Love Python, Love Linux!

No, we are not done, right? We'll run a program!

Copy This Program, paste in Gedit and save it in the same folder as your PyOpenGLProject virtualenv directory, save it as something like program.py.








Switch off the desktop effects, if any (Right Click the Desktop->change desktop wallpaper->visual effects->none)
Type

python program.py

sit tight! (press 's' when the program runs.)

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Fix my problem, it was for me to use python -m pip install

    ReplyDelete