3

Possible Duplicate:
How to concatenate multiple Python source files into a single file?

Is there a Python “pre-interpreter” to take as input a .py module containing imports and expand it so it can be run inline in an interpreter session on the command line or Telnet session? Imports of built-ins or installed modules are OK to keep, but I’d like the pre-interpreter to expand my own modules. This way, I can avoid an install while still employing modular programming techniques. For example, if I write these two modules:

myprint.py:

from math import pi
def print_pi():
    print "{0:6f}".format(pi)

main.py:

from myprint import print_pi
print_pi()

Running the pre-interpreter on main.py, the output would be:

from math import pi
def print_pi():
    print "{0:6f}".format(pi)

print_pi()

Update October 10 2012 22:36 Eastern USA:

Thanks to everyone who responded! My program's runtime host is permanently installed on board a locomotive, from where my program will query and monitor that computer and many other onboard systems. You can correctly predict that this computing environment is safety and mission critical (thus the need for my team’s monitoring software). Installing software in such an environment requires privileges, consumes resources, and imposes a small but real risk to the system. Thus, prudent but weeks-consuming checks have been implemented by the railroad to scrutinize software to be installed, including sign-off by a change-control board. This is the route we had planned for. But if we can eliminate this risk and thereby lessen the formality by running without any installation, that could be an advantage to at least consider. However, we do not want our architecture to be restricted by the non-install requirement, i.e., we don't want to have to write the whole program in one module. From your answers it seems my notion is not possible.

Community
  • 1
  • 1
Buzzy Hopewell
  • 165
  • 4
  • 14
  • 3
    You do realize that this falls down with many, many, many advanced and not-really-advanced use cases? –  Oct 10 '12 at 21:31
  • 1
    Is an install process of "Unzip the file" really that hard? – Brendan Long Oct 10 '12 at 21:32
  • What do you mean "avoid an install"? What are you trying to avoid? – BrenBarn Oct 10 '12 at 21:32
  • As Achim puts it in his answer, the concept of expanding a file simply "does not apply" to Python imports. On a side note, Python can interpret zip files (and egg files which are just zip) with a properly manifest file inside. Maybe putting all your dependencies in a zip would solve your problem? – jsbueno Oct 10 '12 at 23:48
  • Possible duplicates: [Packing python files to single .py script](http://stackoverflow.com/questions/4368040/packing-python-files-to-single-py-script) and [How to concatenate multiple python source files into single file](http://stackoverflow.com/questions/1580746/how-to-concatenate-multple-python-source-files-into-single-file) – Matthew Trevor Oct 11 '12 at 00:36

1 Answers1

2

Your example assumes that Python imports are something like C macros. That's not the case. Pythons import is much more powerful and complex. Therefore an import cannot be replaced by copying a few lines of code. The the answer is: No there is no such preprocessor. But if you specify more detailed what problem you really would like to solve, we might be help you to solve it in python.

Achim
  • 15,415
  • 15
  • 80
  • 144
  • Thanks Achim. Not sure if this will be read, since the post was quickly closed as being a duplicate. The problem I have is that my targeted computing environment is regulated not only by company policy but also by U.S. federal law. There are separate regulations applicable for installing software vs. running software, and the regulations for installing are much more stringent. If I can run my complex app through a SSH session instead of installing I will have to comply with the second set of regulations. This would not be considered an evasion. But, it is not worth coding my entire app for it. – Buzzy Hopewell Jan 08 '13 at 18:44