0

I've been learning C++ recently and wondering whether it is possible to convert a .py file to .bin file just like we compile C++ to .bin in Linux.

I found pyInstaller to convert it to exe file but what I need is to export it to a .bin file just like we get when we perform g++ test.cpp -o a.out or a.bin etc.

Any help would be appreciated.

jaychandra
  • 353
  • 11
  • 4
    Does this answer your question? [Is it feasible to compile Python to machine code?](https://stackoverflow.com/questions/138521/is-it-feasible-to-compile-python-to-machine-code) – Andreas detests censorship Jul 12 '20 at 15:22

1 Answers1

2

The shortest answer is no, because python needs it engine in order to interpret your code.

The longer answer is yes but not exactly.
Like you said there are solutions like py to exe or py to elf and many other solutions in order to make your python script run as a native executable.
Most of these solution pack the parts of python your code need and the code itself so you won't need python installed on the machine running the code.

You cannot just compile python like you are doing with C++ because it is an interpreted language.
(There are other ways to "compile" python like R-python, but it's not exactly regular python and are more complex)

And finally about the file extension:

  1. if you are running on linux the extension doesn't matter because the OS checks the type of the file before running it.
  2. if you are running on windows the extension does matter, but .bin files won't run on your system. If you want to run a program on you system it need to be in PE (portable executable) format- i.e- .exe (programs), .dll (libraries) or .sys (drivers).
Mogi
  • 511
  • 1
  • 3
  • 16