hippy
Posts: 9173
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: is it possible to compile a Python program into an executable ?

Fri Aug 21, 2020 2:49 pm

enedil wrote:
Fri Aug 21, 2020 12:18 pm
satineeraj wrote:
Fri Aug 21, 2020 4:09 am
Yes you can compile the Python Code through the compilers check best Python Compilers here: ...
but they all come with some caveats, that were already mentioned
I'm not convinced that site isn't just a 'spam vehicle' with pages copied from elsewhere used to suck people in. It seems to me pages are often created opportunistically to allow making a reply. The links often have out of date information which is inconsistent with the date of the article.

Of course; I could be wrong.

plugwash
Forum Moderator
Forum Moderator
Posts: 3737
Joined: Wed Dec 28, 2011 11:45 pm

Re: is it possible to compile a Python program into an executable ?

Sat Aug 22, 2020 1:01 am

Whether or not that particular site is out of date, I think all python compilers will have caveats, either being only a subset of the language or requiring a substantial run-time support. It's just the nature of python as a very dynamic language.

User avatar
paddyg
Posts: 2612
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: is it possible to compile a Python program into an executable ?

Sat Aug 22, 2020 7:15 am

I think that's true. If the objective is to produce a small high efficiency executable then starting with python is probably the wrong place. I still think that isn't an important objective for the job here.

WRT the geekbuzz site the only one in the list I've tried is nuitka which didn't produce very impressive results. On the other hand cython and numba, which are not mentioned, have been very fast - faster than my C on occasion. But they still need to be run using python and the code generally needs to be converted quite a bit.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

hippy
Posts: 9173
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: is it possible to compile a Python program into an executable ?

Sat Aug 22, 2020 2:17 pm

paddyg wrote:
Sat Aug 22, 2020 7:15 am
On the other hand cython and numba, which are not mentioned, have been very fast - faster than my C on occasion. But they still need to be run using python and the code generally needs to be converted quite a bit.
Cython can and does create standalone native executables as described earlier; it is entirely the same as compiling any C program after conversion.

True, unrolling Python code to its C equivalent is quite complicated, and it does depend on linking other libraries during compilation, but that's not unusual. However complicated that is; it is largely irrelevant to those who simply want to turn Python source into native executable code.

I have not had to tweak any of my Python code to get it to compile via Cython but I may have just been lucky.

As to how much of the Python installation linked in during compilation needs to be provided for a distributed compile app to run I haven't been able to determine yet as all my Pi's have it all installed. When compiling for oneself that's mostly an irrelevancy.

I am sure there may be some caveats such as not supporting all the Python language constructs, but I haven't encountered any so far. The apt supplied Cython 3 package doesn't handle Python 3.8 or 3.9 additional syntax, but neither does Thonny or the Python 3.7 which is installed on a Pi by default.

As to any extensive run-time support required; that has never worried me. Code is doing the same whether that is done within the app or supplied by libraries. There's little difference except for what libraries may need to be distributed with the app or installed as dependencies. A suitable installation script will sort that out for end-users if distributing the app.

I'm not sure why there's so much negativity whenever compiling Python gets discussed, here or elsewhere, from 'it can't be done', to 'it won't work', 'won't work well', or 'won't deliver useful improvements'. It seems to me to mostly be discouragement from those who wouldn't do it that way, don't want to see others doing it that way.

I would suggest people try it and see how they get on, judge for themselves.

enedil
Posts: 80
Joined: Sat Feb 21, 2015 4:22 pm
Location: Toruń, Poland
Contact: Website

Re: is it possible to compile a Python program into an executable ?

Sat Aug 22, 2020 5:04 pm

hippy wrote:
Sat Aug 22, 2020 2:17 pm
paddyg wrote:
Sat Aug 22, 2020 7:15 am
On the other hand cython and numba, which are not mentioned, have been very fast - faster than my C on occasion. But they still need to be run using python and the code generally needs to be converted quite a bit.
Cython can and does create standalone native executables as described earlier; it is entirely the same as compiling any C program after conversion.

True, unrolling Python code to its C equivalent is quite complicated, and it does depend on linking other libraries during compilation, but that's not unusual. However complicated that is; it is largely irrelevant to those who simply want to turn Python source into native executable code.

I have not had to tweak any of my Python code to get it to compile via Cython but I may have just been lucky.

As to how much of the Python installation linked in during compilation needs to be provided for a distributed compile app to run I haven't been able to determine yet as all my Pi's have it all installed. When compiling for oneself that's mostly an irrelevancy.

I am sure there may be some caveats such as not supporting all the Python language constructs, but I haven't encountered any so far. The apt supplied Cython 3 package doesn't handle Python 3.8 or 3.9 additional syntax, but neither does Thonny or the Python 3.7 which is installed on a Pi by default.

As to any extensive run-time support required; that has never worried me. Code is doing the same whether that is done within the app or supplied by libraries. There's little difference except for what libraries may need to be distributed with the app or installed as dependencies. A suitable installation script will sort that out for end-users if distributing the app.

I'm not sure why there's so much negativity whenever compiling Python gets discussed, here or elsewhere, from 'it can't be done', to 'it won't work', 'won't work well', or 'won't deliver useful improvements'. It seems to me to mostly be discouragement from those who wouldn't do it that way, don't want to see others doing it that way.

I would suggest people try it and see how they get on, judge for themselves.
Cython requires libpython.so - this is what we mean by required additional runtime. This is also what we call totally not standalone.

The tweaks required are not for barely running cython code, they're needed so that the compiled version can be faster more than just a bit.

The negativity comes from the fact, that people ask how to compile Python code, while they're not aware of what exactly does it mean and what benefits might come from it. Most of the times, there are more usefule optimization methods than compilation to native code. That's why people try to ask, what does the asker even need the compilation for, and how do they understand the topic. It's really like another post I've seen on this forum, where the OP asked about is it possible to run macOS on Pi, and somebody answered that somebody managed to run XNU kernel, and the OP asks "does it work with the GPIO pins and camera module?". It's just some really different concept of "working", just as compiling Python is probably understood quite differently by people that know about it and by others. I discourage people from compiling their apps (apart from learning), because most probably, they won't find what they wanted.
- What Can a Thoughtful Man Hope for Mankind on Earth, Given the Experience of the Past Million Years?
- Nothing.

Kurt Vonnegut, Cat's Cradle

hippy
Posts: 9173
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: is it possible to compile a Python program into an executable ?

Sat Aug 22, 2020 6:28 pm

enedil wrote:
Sat Aug 22, 2020 5:04 pm
Cython requires libpython.so - this is what we mean by required additional runtime. This is also what we call totally not standalone.
I do fully understand all that. Most code isn't standalone in my experience.
enedil wrote:
Sat Aug 22, 2020 5:04 pm
The tweaks required are not for barely running cython code, they're needed so that the compiled version can be faster more than just a bit.
But not needed if that's not what they are seeking.
enedil wrote:
Sat Aug 22, 2020 5:04 pm
The negativity comes from the fact, that people ask how to compile Python code, while they're not aware of what exactly does it mean and what benefits might come from it.
I tend to prefer giving what's asked for.

Probably because I loathe being asked "why?" when I ask for something. It's no one else's business why I want something and rarely affects what the answer would be. I also find it quite insulting when people take it upon themselves to presume they know better than me or to question my judgement, that I didn't simply want what I asked for and didn't have good reason to ask.

Give people what they ask for and they can have no complaints. If they want or need more they can come back and discuss that further.

By all means offer additional help, guidance and advice. But that's footnote, not a substitute for answering the question asked.

Return to “Python”