Monday, March 08, 2010

Pyglet: Helpful Tips

In my last article I introduced Pyglet, a small and elegant cross-platform library that implements OpenGL, 2D sprites and a complete windowing interface in minimal Python code. In this article I will outline a few issues I had setting up the library. I hope that in this way I will save others time and headache.

These issues came about due to the fact I am running a 64-bit version of Windows 7. It seems that many programmes are still not ready for 64-bit operating systems, and if they are it can still be quite confusing getting and installing the correct versions.

To start with, the Pyglet download page makes available a Windows installer in MSI format. This failed to find my Python installation. Apparently this is a common issue; the solutions I found online involved copying registry entries from one place to another. Unfortunately the specific entries mentioned were not to be found on my system, so I abandoned this approach.

A simpler solution was to grab the source distribution and run "python install" to do all the work.

This appeared to do the trick, but the example applications I tried failed with various errors. Then I resorted to the test suite (very nice that one is included) and this could not even create a window. Houston, we have a problem!

From the support group I determined that Pyglet supports 64-bit Windows but does not support 64-bit Python. You need to have 32-bit Python running on 64-bit Windows to use Pyglet. With the correct version of Python 2.6.4 installed, everything went fine. It sure would be nice if somewhere, in the install docs or FAQ or on the download page, this was mentioned.

My second issue had to do with the declaration on the homepage that Pyglet has "No external dependencies or installation requirements". We have just seen that the last part of this is not true, but neither is the first part, in any real sense. A couple sentences later we find out that "pyglet can optionally use AVbin to play back audio formats such as MP3, OGG/Vorbis and WMA, and video formats such as DivX, MPEG-2, H.264, WMV and Xvid".

What is going on here is a matter of expectation. Other Python graphics toolkits have one or more external dependencies that require installing or compiling third-party libraries. Sometimes this can be quite an involved process. Further, these dependent libraries are not always free or open source. So long as one is using only uncompressed media, Pyglet is free from these dependencies. So, relative to other windowing libraries, this is in fact a Pyglet advantage.

However, to do any real work one will soon need to support some of the common compressed file formats and codecs that AVbin gives access to. (It does so by wrapping the extensive FFmpeg library.)

The AVbin site has one download for Windows, a file named "". Hmmm.... does this mean it will not work on 64-bit Windows? I thought I'd give it a try. The ZIP contains only a readme and a DLL file. The former says to place the latter in your "\windows\system32" folder. I did this but applications that require AVbin still failed to work.

Pyglet has a Google Group to provide support. It averages about 150 messages a month. It is easy to get spoiled by groups that provide six answers within 3 hours; this is not one of those, so patience is required. My first query was answered in 12 hours, so I dashed off another request for information. This time it took 24 hours before I was told I would need to put the DLL in my "WOW64" folder. I found the similarly named "\windows\SysWOW64" folder. I put the DLL there and it worked perfectly.

The lesson to be learned is that if you want a 32-bit DLL to work in Windows 7 64-bit then put the file in SysWOW64.

There are two things that should be improved here. First, the zip file should be named in a way that makes it clear that it will in fact work on win64. Second, the readme needs more complete installation instructions.

Getting back to Pyglet itself, it would be marvellous if the correct version of the DLL could be included with the installation, saving a step and making the programme truly self-contained. (Actually, this is in fact the case for the full installer version, but not the manual process I needed to use.)

Better yet would be if AVbin could be dynamically linked as a Python library (a PYD file). This would have the distinct advantage of being loadable from elsewhere in the file system. No mucking about with Windows folders would be necessary and it would then be easier to distribute finished applications to clients.

Does someone want to create a PYD from AVbin? I have no idea what that entails. But it would be a real boon!

(I think that one disadvantage is that PYD files are specific to Python versions. This creates a maintenance issue.)



robin said...

Actually, I do have a netbook with LINUX for running and testing apps. My lack of familiarity with the environment stems from the fact I have used LINUX flavours for decades, but never as intensely as required to keep everything I need to know in my head.

Sleet01 said...

I found your page when I ran into the same issue. Your solution worked perfectly; thanks!

For those just getting started, you should know that Idle and Pyglet interact very poorly; if you need an interactive interpreter you should run c:\Python2X\python from a cmd window or you'll likely get exceptions thrown when you try to instantiate a window.

Post a Comment