Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do we have any bundler tool (like webpack) for python backend code base [closed]

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.

like image 539
Sanjesh M Avatar asked Oct 30 '25 04:10

Sanjesh M


1 Answers

Sadly the answer (as of 2024) is No, we do not.

There's 4 main reasons why.

  1. 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.
  2. 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__').
  3. 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.
  4. 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.

like image 50
Jeff Hykin Avatar answered Oct 31 '25 19:10

Jeff Hykin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!