pyp - Simple Preprocessor for Python

Return to David's Python Resources
Missing C's '#include' statement? Yearn no longer.

pyp is a simple preprocessor that interprets markups (eg '##include') in python files, and generates expanded python files from these markups.


OK - here's an example.

If you're familiar with the PythonCard graphical toolkit, you'll know that its resource editor works with files in the form Each of these files contain a Python data literal, being a dict containing dicts and lists of the various resource components (windows, buttons, fields etc).

Why is this an issue?

Well, you could just go with standard PythonCard practice, and let your window class inherit the logic which assumes that if your module's name is, your resource file will be called, and load that in. But this has 2 problems:
  1. If you want to run your python program from outside of the directory where resides, PythonCard's code may not find, which will cause your program to fail.
  2. PythonCard loads resources at run-time. If you want to bundle your program into a standalone executable for Windows, using Py2EXE or McMillan's Installer,  this could be a problem. Paths can be confused (result: program failure), or you may simply not want your resource defs to be sitting there in plain view.
My solution is to 'pre-process' my Python code, and generate a Python module with the resource definitions included in it.

For example, here's my version of PythonCard's demo program:
from PythonCardPrototype import model

minimalRes = \

class Minimal(model.Background):

def on_menuFileExit_select(self, event):

if __name__ == '__main__':
app = model.PythonCardApp(Minimal, None, minimalRes)
Notice the '##include'. This works just like the #include statement in C, recursive even.

When I run pyp over this code, it generates a python file with the resource definitions included. Result is a file that can be run from anywhere, and can be easily packaged into a standalone EXE. Problem now nonexistent.

So as you can see, only '##include' is supported at the moment. Maybe I'll add some more directives, perhaps even a '#define', '#if', '#ifdef' etc. But even as it is, pyp can prove useful in many situations.

Download pyp here.

If you come up with any patches, suggestions etc, please email them to me at
When mailing me, you'll get a 'please confirm' response to my first message. Reply to that, and you'll be permanantly added to my whitelist.

Privacy Policy