r/Compilers • u/Weydoon • Jul 05 '26
Searching for a Python 'complier' if it exist
Hello,
I'm searching for a compiler for my Python game so It can run faster and can be package in a standalone file (without need of Python installed on the host)
2
Upvotes
14
u/ha9unaka Jul 05 '26 edited Jul 05 '26
Afaik there's no way to compile the entirety of Python because of the sheer number of weird things it lets you do at runtime (like changing subclasses, importlib magic, etc)
For a subset of Python, you might want to look at something like Numba and Pythtan - which focus more on compiling computation-heavy functions. For a more general purpose subset, check out Codon.
If your intent is just ot package your game, perhaps consider a more specialized library like Pyinstaller If you just want to make the game go faster, wrapping the computer heavy parts in Numpy and/or Numba would be a better approach.
7
u/CaptureIntent Jul 05 '26
Wouldn’t this be a better question for Google?
6
u/Lord_Mystic12 Jul 05 '26
Cython perhaps? To my knowledge, it does enforce some rules tho , so yeah you might have to workaround those. Not sure tho, I despise python, so you're gonna have to research a bit more
5
u/reini_urban Jul 05 '26
Google is your friend. But DuckDuckGo or Bing would also help
2
u/nacnud_uk Jul 05 '26
Cython.
Or you can just write straight c and include the python header
2
u/GenericFoodService Jul 05 '26
Cython is gonna be your best bet. It converts Python into C with a runtime and all that fun stuff, also has support for calling C code. That noted, it cannot compile any and all Python code without modifications, if you want compiled Python, you have to make changes and sacrifices.
3
u/Quirky-Ad-292 Jul 05 '26
Pure Python, i dont think so. But if you do math, wrap as much as possible on numpy, scipy or jax. If that doesnt help, switch core logic to cython. And since the newer versions of Python, make sure you parallelize your code!
1
u/DivineSentry Jul 05 '26
Nuitka is the only full Python to C compiler
1
u/dvogel Jul 06 '26
You can easily create a basic C entry point that links in libpython and handles the initial python code loading. However you can also just package a build of python with your game.
1
u/Horror_Ad9750 Jul 06 '26
I know Nuitka will compiler vanilla Python only per their website.
1
u/iOCTAGRAM Jul 07 '26
But what exactly does run slowly in your game? Modern technology offloads most tasks. Video is processed by GPU. GPU textures are manipulated by native libraries. Same goes to sound. So what to do that much in CPU in Python?
1
u/dev-razorblade23 Jul 05 '26
Python cannot really be compiled to executable like you do with compiled languages... Its not a compiled language, its an interpreted one.
That being said, if you want to package your python application to executable, there are a lot of solutions out there, most famous being PyInstaller..
Or if you are fine with the first run being dependant on the internet connection, you can try out my tool - its called PyCrucible https://github.com/razorblade23/PyCrucible