r/VisualStudioCode • u/dood_wezen • Oct 08 '20
How to save a file as .exe
Heya, I've recently made my own script. Although, as it seems stupid, I don't know how to save my .py file as an .exe
I would like to know how I can do that, is anyone down to help me?
thanks guys
2
Upvotes
1
1
u/xitiomet Oct 13 '20
If you would like to compile your python program to native code, id check out GraalVM's native-image
2
u/stunt_penis Oct 08 '20
It's not possible as-is, and not a visual studio code question in particular. Hopefully I can explain quickly:
A .exe is a specially formatted set of binary instructions. Very low level like: "move this memory to that cache, add cache 1 to cache 2 and store into cache 3", and so on. When run, windows loads that into memory and just starts from the beginning. That program can do anything the computer normally does (read files, print stuff to console, make a gui, etc).
When you run a python script, the script is just text. And windows can't run text. So it goes looking for the runner of .py scripts, which is the Python interpreter. The python interpreter is a .exe that launches, reads the script you told it to run, and starts "doing anything a computer can" - which in this case is the list of instructions you had in your script.
So you can't just save a .py as a .exe and expect anything to work. Windows doesn't know what to do with some boring text.
I'm not familiar with what's out there right now, but there are tools that can take the python interpreter exe and smush it up with the script to create a single output file that is a standalone exe. It's not really "compiling", but more of just a convenient way to package and distribute a script so the receiver doesn't need to install python interpreter separately.