0

I'm trying to put my Python program and all of it's dependencies into a single binary that could be executed from the command line. I followed Broken Man's suggestion in this post here using Cython3 and it works if you have a simple Python script. However it doesn't work if you're using import statements in your code to include your dependencies.

I'm looking for a solution that will allow me to execute my program similar to how you would in C, e.g think ./a.out

Please don't recommend using chmod +x to convert it into an executable. That's not what I'm looking for. This isn't a desktop app either - it needs to be executed from the command line.

Can Cython/Cythonize be used to compile dependencies or is there another tool that should be used for this? The dependencies I'm relying on are a mix of libraries installed with pip and custom written files in the same directory.

FestiveHydra235
  • 473
  • 1
  • 7
  • 23
  • I think what you are looking for is `pyinstaller`. That creates an executable from your Python bytecode, plus the bytecode of all its dependencies, plus any binary extensions those dependencies need, plus a copy of the Python interpreter. Be warned, it will be pretty big and will take a noticeable while to load. – BoarGules Aug 19 '21 at 16:50
  • Concerning usage of cython for this problem: https://stackoverflow.com/a/59389683/5769463 – ead Aug 19 '21 at 17:43

1 Answers1

1

The tool you are looking for is Nuikta.

Syntax is: python -m nuitka --standalone --follow-imports programname.py where:-

--follow-import: Causes imported modules (including local imports) to be included in binary as well. --standalone: making standalone binary

Machinexa
  • 626
  • 1
  • 8
  • 17
  • Nuitka is extremely slow. With just a simple `hello world` application, and with only `click` imported as a dependency, it takes my server approx. 21 min. to compile. With `ccache` configured. You're right, it does work. But currently its beyond any practical application. Therefore I am trying to get cython to work. – Hamza Zubair Aug 21 '21 at 04:54
  • @HamzaZubair Cython doesn't handle dependencies and makes no claim to handle dependencies. It is genuinely the wrong tool for creating bundled applications. – DavidW Aug 21 '21 at 09:48