I am managing a quite large python code base (>2000 lines) that I want anyway to be available as a single runnable python script. So I am searching for a method or a tool to merge a development folder, made of different python files into a single running script.
The thing/method I am searching for should take code split into different files, maybe with a starting __init___.py file that contains the imports and merge it into a single, big script.
Much like a preprocessor. Best if a near-native way, better if I can anyway run from the dev folder.
I have already checked out pypp and pypreprocessor but they don’t seem to take the point.
Something like a strange use of __import__() or maybe a bunch of from foo import * replaced by the preprocessor with the code? Obviously I only want to merge my directory and not common libraries.
Update
What I want is exactly mantaining the code as a package, and then being able to “compile” it into a single script, easy to copy-paste, distribute and reuse.
It sounds like you’re asking how to merge your codebase into a single 2000-plus source file– are you really, really sure you want to do this? It will make your code harder to maintain. Python files correspond to modules, so unless your main script does
from modname import *for all its parts, you’ll lose the module structure by converting it into one file.What I would recommend is leaving the source structured as they are, and solving the problem of how to distribute the program:
You could use PyInstaller, py2exe or something similar to generate a single executable that doesn’t even need a python installation. (If you can count on python being present, see @Sebastian’s comment below.)
If you want to distribute your code base for use by other python programs, you should definitely start by structuring it as a package, so it can be loaded with a single
import.To distribute a lot of python source files easily, you can package everything into a zip archive or an “egg” (which is actually a zip archive with special housekeeping info). Python can import modules directly from a zip or egg archive.