Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • Is the "single .py file" a real requirement, or the best solution you came up with? To distribute "one single thing" I'd try to build a package and distribute that, see packaging.python.org/en/latest/tutorials/packaging-projects. Also, you might be able to build an exe file (see python.land/deployment/pyinstaller) Commented Jan 23 at 14:56
  • @olepinto: I actually did try to build an executable file with cython, and that failed. I tried pyinstaller, and it failed. I mean, it thought it succeeded, but it completely failed in recognizing what was going on with my code, so that the resulting executable ends up telling me No module named 'utils'. Commented Jan 23 at 15:04
  • The low-level/DIY approach would be to change the way python does imports as described in: docs.python.org/3/library/importlib.html I'd write my own "bundler" that takes a normally structured python source and turns it into a single-file with dependencies embedded inside it (e.g. as strings) and sets up import hooks to use these embedded modules appropriately. not sure if you want to go that far though! Commented Jan 23 at 18:11
  • @SamMason: I wonder if such "bundlers" exist. For languages like C and C++ there are utilities which take a hierarchy of include files and create a single big include file which is easier to deploy. Commented Jan 23 at 18:45
  • I'd guess that things like this exist, but I've not tried to do anything like this before so don't know where to start looking. pyinstaller is the closest I'm aware of, but AFAIU it also includes things you don't want (e.g. the actual Python interpreter). if you wanted to include libraries/binary code it would seem to get more complicated, but I'm not sure if you need this (your example certainly doesn't) Commented Jan 23 at 19:00