I have python (3.0) code base for backend.
I want to deploy this code as bundle on other machine so that it does not require to install dependencies.
Note 1: on my remote machine i have python installed not an issue.
Note 2: This app is not containerized (docker)
Note 3: After bundling it i have to run program on command line with some input argument to the program
(Like for Node Js based app we use Webpack and it just bundles all the js and dependencies into one JS file which can be run from any machine without having node_modules.)
I see some are there like 'python-webpack' but they are unmaintained.
Sadly the answer (as of 2024) is No, we do not.
There's 4 main reasons why.
- Lots and lots and lots of python modules are actually just a bunch of C++ code wrapped up inside python. Can't really "inline" a python module when the module isn't actually python. Effectively all the JavaScript modules that are bundled by webpack/vite/browserify/babel are pure JavaScript. Not all NPM modules are pure JS, but if you try to bundle a non-pure-JS module its not going to work well.
- Python's scoping of modules is bad. In JavaScript there isn't really the concept of a "module", a JavaScript module is just a JavaScript file. Python is completely different, a module is something special, and its a runtime-global object and name. Python modules can check how they're being called (e.g.
__name__ == '__main__').
- Python packaging is kind of a mess. Package names aren't the same as module names, a single package can have multiple modules, there's many different structures and publishing formats, like egg files, and wheel files, and tarballs. A python module has to be parsed before it can be used, and it can take a lot of work. In contrast (and to oversimplify) JavaScript publishing is basically just uploading a JavaScript file somewhere. Literally in deno
import 'https://path/to/a/file.js' will work just fine. Bundling in JS is a matter of collecting all those JS files recursively, no format conversion necessary.
- Python makes surprisingly frequent use of dynamic imports... VERY dynamic imports. Technially dynamic imports are supported and used by JavaScript as well, but they're much more rare and everyone kind of knows that dynamic imports won't reliably be picked up by a bundler so they write the code with that in mind. In python, all of the code is written without a bundler in mind. Some python code has nested try-excepts, like trying to import numpy (a C/C++ package) just to see if the user has numpy installed. I've seen horrific cases of dynamic imports inside of eval-ed strings (its more common than you'd think/hope). Bundlers are all but entirely defeated by dynamic imports.
Its still possible for someone to try and make a python bundler despite all these things, but as far as I know nobody has.