Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to concatenate multiple Python source files into a single file?
    primarykey
    data
    text
    <p><em>(Assume that: application start-up time is absolutely critical; my application is started a lot; my application runs in an environment in which importing is slower than usual; many files need to be imported; and compilation to <code>.pyc</code> files is not available.)</em></p> <p>I would like to concatenate all the Python source files that define a collection of modules into a single new Python source file.</p> <p>I would like the result of importing the new file to be as if I imported one of the original files (which would then import some more of the original files, and so on).</p> <p>Is this possible?</p> <p>Here is a rough, manual simulation of what a tool might produce when fed the source files for modules 'bar' and 'baz'. You would run such a tool prior to deploying the code.</p> <pre><code>__file__ = 'foo.py' def _module(_name): import types mod = types.ModuleType(name) mod.__file__ = __file__ sys.modules[module_name] = mod return mod def _bar_module(): def hello(): print 'Hello World! BAR' mod = create_module('foo.bar') mod.hello = hello return mod bar = _bar_module() del _bar_module def _baz_module(): def hello(): print 'Hello World! BAZ' mod = create_module('foo.bar.baz') mod.hello = hello return mod baz = _baz_module() del _baz_module </code></pre> <p>And now you can:</p> <pre><code>from foo.bar import hello hello() </code></pre> <p>This code doesn't take account of things like import statements and dependencies. Is there any existing code that will assemble source files using this, or some other technique?</p> <p>This is very similar idea to tools being used to assemble and optimise JavaScript files before sending to the browser, where the latency of multiple HTTP requests hurts performance. In this Python case, it's the latency of importing hundreds of Python source files at startup which hurts.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload