How to compile a python program to distribute it as standalone.

95 views
Skip to first unread message

adhoC

unread,
Mar 2, 2024, 10:41:41 AMMar 2
to cython-users
I have a python program and unlike other examples are available on whole internet to compile using Cython, major examples are just compiling single python file and don't have any solution for which have multiple python files and importing each other and finally all imported in 'main.py' file so is there any solution to compile these.
I tried with compiling all files and then create executable using. 

'cl -o main.exe main.c -I C:\ProgramData\anaconda3\include'
 but the main.exe is producing import errors like 
"Traceback (most recent call last):
  File "main.py", line 5, in init main
    from second import ciption
  File " second.py", line 5, in init  second
    from librosa.filters import mel as librosa_mel_fn
ModuleNotFoundError: No module named 'librosa'"

also tried it with --embed but same results is there any roadmap or any working method to compile full python project into distributable software which have multiple python files and imports... please help even if it is little I'm lost in this for long time😢

da-woods

unread,
Mar 3, 2024, 3:15:26 AMMar 3
to cython...@googlegroups.com
The embed option is genuinely only suitable for really simple programs with no dependencies. It doesn't do what you want.

Cython was never intended "to compile full python project into distributable software". You're much better off using a tool designed for the task like PyInstaller or Cx_freeze.
--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/b35520f9-10ea-407b-ac1b-fe796c3fc0c4n%40googlegroups.com.


Stefan Behnel

unread,
Mar 3, 2024, 12:09:07 PMMar 3
to cython...@googlegroups.com
>The embed option is genuinely only suitable for really simple programs with no dependencies. It doesn't do what you want.

That's exaggerating it. It does a part of what you want, the cython_freeze script in the tools directory does another bit. It's not a click and forget kind of tool, but that doesn't mean that is useless. With a little bit of polishing, it could actually be a fairly nice tool. IMHO, it makes a lot of sense to package multiple extension modules into a single library, for example. This task should be easy. And I'm sure there's a ticket for it somewhere.

That said, if you intend to package an application as an executable, and not a Python package as a single extension module, the following is actually true most of the time:

> You're much better off using a tool designed for the task like PyInstaller or Cx_freeze.

Stefan

adhoC

unread,
Mar 3, 2024, 12:15:59 PMMar 3
to cython-users
Thank you for replying and clearing up that cython can't be able to compile those python scripts have imports. and about pyinstaller and cx_freeze i m currently using them but the level of optization is very bad, even a simple program importing Torch or tensorflow resulting to Gigabytes of executable with very high loading time. thats why I asked here if there is any workaround to make use of cython to reduce it.

Stefan Behnel

unread,
Mar 3, 2024, 12:23:50 PMMar 3
to cython...@googlegroups.com
Am 3. März 2024 16:53:06 UTC schrieb adhoC:
> even a simple
> program importing Torch or tensorflow resulting to Gigabytes of executable

That's more likely to be an issue with the *data* size of your dependencies than your own *code*. You should try to find out what exactly is getting packaged and if you can cut down the dependencies somehow by removing things you don't need.

Stefan

David Menéndez Hurtado

unread,
Mar 3, 2024, 12:29:08 PMMar 3
to cython...@googlegroups.com


On Sun, 3 Mar 2024, 18:15 adhoC, <ayrat...@gmail.com> wrote:
Thank you for replying and clearing up that cython can't be able to compile those python scripts have imports. and about pyinstaller and cx_freeze i m currently using them but the level of optization is very bad, even a simple program importing Torch or tensorflow


That is your problem. Those libraries are huge, and Cython won't help.

Depending on your application, you could export just what you need with TFLite, ONNX, or similar. There are plenty of tools in that space.

/David 


ayra tnameh

unread,
Mar 3, 2024, 2:49:58 PMMar 3
to cython...@googlegroups.com
Thanks you all for helping me 🥰 i think i need to stop searching with packaging with Cython and stick to the classic solution Pyinstaller.. thank you very much for replying and helping.

You received this message because you are subscribed to a topic in the Google Groups "cython-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cython-users/aX9fv4b2N5E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cython-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/CAJhcF%3D3A%2BmXK%2Bwrr11K3Y2SoESzmDKW4%3D6gjCJZV8zsJaJDo2Q%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages