Python ‘Classpath’ – A Snake Digging the Panda’s Egg

I’ve just installed Python and Panda3D, then realised that… …Panda3D installs it’s own (outdated) Python release.

This may be the first of a small series of Python/Panda3D tutorials. As it turns it will be a ‘peer to peer’ tutorial, since I’m learning both Python and Panda3D as I go.

I won’t assume that you can’t write a computer program or don’t understand 3D. So I’m afraid this won’t be suitable for complete newbies. More like a crash course.

OK, after running a couple of examples, my first question was:

How does Python find imported modules?

Yes, there is a ‘classpath’:

  1. Current directory, ie, the target directory when starting our python app.
  2. PYTHONPATH environment variable
  3. .pth files; .pth files are used to add locations to the search path – if a pth files is on the search path (in an already known location), then it is read and all files listed there are also added to the path.
    This is convenient – say we have a foo/script.py script, then all we need to import custom modules is define a foo/import.pth file pointing at our custom module definition files.
  4. sys.path variable. At runtime, we can reach (and edit) this by importing sys, and print the current classpath using print sys.path.
    Changing sys.path at runtime allows dynamically adding locations to the classpath.

Most of this is  documented. The best of it,  .pth files, is apparently still undocumented.

I’m just running the sample programs so far. My guess is that Panda3D is adding its eggs to PYTHONPATH.

Import statements

Python uses both import X and import X from Y statements. The equivalent of the java import is import X from Y (I found a nice article detailing python imports)

Why bother?

Sure, if you install P3D the way I did, you don’t really need to know about sys.path to get started with the sample programs. But then, until we know how the path works, we can’t release software. Let’s be agile. We don’t want to wait another year until we release our first P3D app.

3 Responses to “Python ‘Classpath’ – A Snake Digging the Panda’s Egg”


  1. 1 Mike K. August 10, 2009 at 6:27 pm

    Good start, I’m working on the same 2 technologies (Panda and Python) and am looking forward to more on the subject!

    • 2 oogtech August 12, 2009 at 12:50 am

      Cool. I choose Panda and Python for now because i’m looking for an easy, integrated game engine to prototype games, esp. programming behaviour and social interaction. I’ve done some OpenGL before (imho great for 3D graphics, but expensive to use) and quite a bit of Java3D (Java3D lacks ‘readiness’, and is really un-fun – such a pity considering I had to use it to expose java students to VR programming).
      Got two more tutorials now. Comments and tips always welcome.


  1. 1 Bamboozle: Panda3D/Python Crash Tutorials « OOgtech Trackback on August 18, 2009 at 9:24 pm

Leave a comment