2

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.

Sanjesh M
  • 341
  • 2
  • 6
  • 22
  • 1
    You mean like creating an executable? – code11 Jan 29 '21 at 17:25
  • 1
    Does this answer your question? [How can I make a Python script standalone executable to run without ANY dependency?](https://stackoverflow.com/questions/5458048/how-can-i-make-a-python-script-standalone-executable-to-run-without-any-dependen) – code11 Jan 29 '21 at 17:26
  • 1
    Docker is not an option? – OneCricketeer Jan 29 '21 at 17:28
  • yes kind of executable like in nodejs the bundled js file can be run from other machine if node js is installed. Here my program take inputs from command prompt. – Sanjesh M Jan 29 '21 at 17:35
  • I tried PyInstaller and it creates exe file and i have to run my final bundled file on command prompt with two input arguments – Sanjesh M Jan 29 '21 at 23:12
  • What was the problem when you tried to run the exe file from the command prompt and provided two arguments? – mkrieger1 Jan 30 '21 at 00:43
  • with argument error: dateutil\zoneinfo\__init__.py:26: UserWarning: I/O error(2): No such file or directory CRITICAL:root:No valid argument found or invalid json format. – Sanjesh M Jan 30 '21 at 04:44
  • With no argument: Traceback (most recent call last): File "myPyFile.py", line 89, in IndexError: list index out of range [36044] Failed to execute script myPyFile – Sanjesh M Jan 30 '21 at 04:48
  • so arguments should be enclosed in double quotes...I am able to run my generated single exe file on command prompt with required argument. – Sanjesh M Jan 31 '21 at 03:59
  • does this exe file run inside workspace (' / dist / myfile.exe') only ????. Because if i copy this exe out of workspace then on running it just execute without any error and does not generate my output at all. – Sanjesh M Jan 31 '21 at 04:02

1 Answers1

0

Sadly the answer (as of 2023) 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.

Jeff Hykin
  • 1,846
  • 16
  • 25