views:

62

answers:

2

I am writing a console application in python that will consist of a handful of modules, each with a couple hundred lines of code.

For development it would be nice to modularize the program, but for distribution I like the idea of being able to post the program as a single python script.

Are there any good scripts out there for flattening multiple python modules?

I know that eventually I should brave the complicated mess that is setuptools, dpkg, etc... but I'm not ready to invest that effort yet.

+4  A: 

A zipfile (with just the .pyc or .pyo files in it, ideally) would suffice, especially if you're distributing code supporting a specific X.Y version of Python (any Z in X.Y.Z will do, i.e., if you support Python 2.6, that will work in 2.6.1, 2.6.2, and so on). Just make the zipfile part of the PYTHONPATH, just as if it was a directory, and you're good to go.

If you support many different Python versions (in the X.Y sense) you can make a zipfile per version, it's still pretty simple.

Alex Martelli
That gets it down to two files, right? The main `.py` file, and an accompanying `.zip`.
Craig McQueen
Just the zip could suffice in today's python -- you can zip the main script too, and run it with Python's `-m` commandline switch. A zipfile also accepts an arbitrary prefix that could be your main script, so you'd run with just `python thezipfile.zip` -- the prefix would then be also responsible for adding the zipfile to sys.path before moving on to the "main script proper" execution.
Alex Martelli
@Alex Martelli: You're advocating not distributing the source??
John Machin
Alex Martelli
@Alex Martelli: The size of compressed source last bothered me when I was downloading from an RCPM at 300 bps using an acoustically-coupled modem i.e. not very recently. What is a bother to a package developer is users sending in tracebacks that don't include the source lines.
John Machin
Alex Martelli
Thanks for the suggestions guys, but none of these really do what I was asking for, and I was already aware of the zip file option. I'm looking for something that will parse my top-level script and substitute the import statements with the actual imported files in a (at least moderately) python safe way.
Drew Wagner
@Drew, such code would be pretty nightmarish to write with any modicum of generality (rewriting every toplevel name to skirt around possible clashes, for starters -- and how do you deal with attempts of dynamic access to such names...?), considering the very dubious advantages (if any) that it could offer over a zipfile. Thus I doubt anybody embarked on such a herculean undertaking with dubious (if any) returns.
Alex Martelli
A: 

No code to do this seems to exist.

Drew Wagner