1

In the JavaScript ecosystem, "compilers" exist which will take a program with a significant dependency chain (of other JavaScript libraries) and emit a standalone JavaScript program (often, with optimizations applied).

Does any equivalent tool exist for Python, able to generate a script with all non-standard-library dependencies inlined? Are there other tools/practices available for bundling in dependencies in Python?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
codykochmann
  • 347
  • 1
  • 10
  • 1
    check this out: http://stackoverflow.com/questions/5458048/how-to-make-a-python-script-standalone-executable-to-run-without-any-dependency – Totem Apr 20 '15 at 20:10
  • Close to what I'm attempting, but py2exe would break my code. I need something that compiles everything into Python code so I can mold the architecture into what I require. – codykochmann Apr 23 '15 at 14:58
  • @codykochmann: Try ask the question again, as some trigger-happy moderators closed this question and it is not possible to post an answer then please link in here comments. – Mikko Ohtamaa Jul 08 '15 at 00:13
  • If you made the question shorter, it would be easier to get at its core meaning -- at present, it's hard to tell what distinguishes it from others. Right now, it takes a lot of effort for someone to understand what you're actually asking. – Charles Duffy Jul 08 '15 at 00:20
  • 1
    ...that said, I can give you an answer: No, this isn't possible. You could do it for pure-Python modules, but native C modules (which almost any nontrivial program uses) can't be inlined with Python code. – Charles Duffy Jul 08 '15 at 00:22
  • (Also, taking out the misleading term "compiled" helps to avoid folks closing this inappropriately; JavaScript-to-JavaScript compilers are a thing, but if someone in the Python ecosystem hears "compiler", they're going to assume you're talking about something else). – Charles Duffy Jul 08 '15 at 00:27
  • 1
    Take a look at modulewalker and snakefood. – Alyssa Haroldsen Jul 08 '15 at 00:28

1 Answers1

0

Since your goal is to be cross-architecture, any Python program which relies on native C modules will not be possible with this approach.

In general, using virtualenv to create a target environment will mean that even users who don't have permission to install new system-level software can install dependencies under their own home directory; thus, what you ask about is not often needed in practice.


However, if you wanted to do things that are consider evil / bad practices, pure-Python modules can in fact be bundled into a script; thus, a tool of this sort would be possible for modules with only native-Python dependencies!

If I were writing such a tool, I might start the following way:

  • Use pickle to serialize content of modules on the "sending" side
  • In the loader code, use imp.create_module() to create new module objects, and assign unpickled objects to them.
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441