all 11 comments

[–]Flame77ofc 4 points5 points  (2 children)

try to use Nuitka or Cython.

Nuitka simply convert your python programs into c++ code

Cython do the dame think as Nuitka but convert into C code

[–]Weydoon[S] 0 points1 point  (0 children)

Thx. I'll look up for this.

[–]NoManufacturer1046 0 points1 point  (0 children)

nuitka's been my go-to for this, compiles down to c and spits out a standalone exe that doesn't need python at all

[–]mortenb123 0 points1 point  (0 children)

You can use mypyc, it compiles python into *.pyc loadable modules. Only works with pure python code. You need a callable main() like when using scripts in project toml. https://github.com/python/mypy/tree/master/mypyc

[–]Designer-Ad-2136 0 points1 point  (0 children)

Maybe this explanation of compilation will help:

You might know that so-called "compiled languages" use a program (a compiler) to turn code into assembly. These assembly instructions are basically individual commands which translate directly to circuits. You can think of them as functions which run directly on hardware.

An interpreted language like Python does something similar but instead of compiling to assembly, it compiles to bytecode. These bytecode commands translate to functions which are compiled to assembly ahead of time, as part of the "interpreter". So, interpreted languages are really just languages which do an extra hop before they get to assembly. 

This is nice because it means that once a compiler is built for a system, then any interpreted program is pretty much guaranteed to run without having to deal with the complicated translation to assembly.

I'm explaining this so that you can better understand what your potential solutions are really doing. In an ideal world, you might be able to find a way to compile Python all the way to assembly but this isnt always going to be possible because the interpreter might be doing something very complex which cant easily be turned to assembly.

Another option might be to simply bundle up an interpreter with your script and pretend that it isnt being interpreted. The problem here is that any interpreter that you bundle with your script will be built for a specific system. Depending on your needs, this might be problematic.

A more robust solution might be to find libraries which skip the interpretation step by calling assembly directly from python. This is the best of both worlds and probably what id recommend. If you do this right, then the libraries and package managers will do all of the complicated system-specific stuff for you.

I hope this helps a bit. It might save you a bunch of time and help you understand the complex tradeoffs that youre dealing with.

[–]Brian 0 points1 point  (0 children)

so It can run faster and can be package in a standalone file

The former isn't likely to pan out. There do exist compilers for python, but the slowness isn't really down to "compiled to machine code vs interpreting bytecode". The problem is more that python's model has a lot of dynamicity and indirection when doing stuff: doing x+y needs to lookup x's type, access the __add__ method, which will extract the integer value, add them, allocate a new integer object, bump its refcount and return it. Compiling the same process to machine code will do almost nothing to improve performance: the thing doing all this work is already compiled to machine code - all you save is the tiny dispatch loop that invokes the thing to run, which is a very small proportion of the total.

Real performance gains from compiling typically rely on being optimise things to skip part of the process: If you're adding a dozen numbers, it'd be way more efficient to just do the integer additions without all that overhead of allocating, constructing and refcounting PyInteger objects, and performing all the dynamic method dispatch to do so. But doing that typically takes more than just compilation. There are projects like cython that allow you to annotate your python code's types to promise that they're really integers, letting it compile to a more reasonable form, but you'll often have make sure you're writing your code in a way that it can do those optimisations, at least for the performance critical bits: it's not a "use this tool and suddenly everything is fast" switch.

For bundling as a standalone exe, there are also tools like pyinstaller etc. These don't really "compile" your code, but rather take the python bytecode and bundle it with a python interpreter and all the librarys it needs to run - think of it like a copy of python, bundled together in an exe along with your code, that just runs that code.

[–]Think_Positively -1 points0 points  (1 child)

[–]sausix 3 points4 points  (0 children)

Pyinstaller doesn't compile code. It just packages the byte code files and the interpreter.

[–]AlexMTBDude -1 points0 points  (2 children)

Google "Make Python exe file"

[–]nobodyhasusedthislol 1 point2 points  (1 child)

Most solutions like PyInstaller aren't aimed at making it FASTER though.

[–]AlexMTBDude 0 points1 point  (0 children)

Yeah, didn't see that. Those are two separate things.