Home:ALL Converter>How to compile a cython program to a standalone executable?

How to compile a cython program to a standalone executable?

Ask Time:2022-01-09T07:58:25         Author:neehack

Json Formatter

Say, I have the following cython module renamed_module.pyx. This module contains all my cython code which include C and Python functions. Normally in development below is how I compile and run renamed_module.pyx.

  1. A python file called setup.py that calls cython to convert my pyx modules into C code.

Setup.py code:

from distutils.core import setup
from Cython.Build import cythonize

setup(
    name="appname",
    ext_modules=cythonize(['renamed_module.pyx', 'other1.pyx', 'other2.pyx',  'other3.pyx']),
    language_level=3)
  1. I have another python file called run_renamed_module.py with the following code:

run_renamed_module.py:

import renamed_module
import os
import sys

sys.path.insert(0, os.path.abspath("."))

renamed_module.startingfunction()
  1. Finally I compile it as following which works perfectly: python Setup.py build_ext --inplace && python run_renamed_module.py

Question

Now, I would like to convert my renamed_module.pyx into a standalone executable or a *.exe file that would open my cython GUI App.

After doing some research, I was able to first convert my renamed_module.pyx code into C code using cython renamed_module.pyx --embed -o renamed_module_comp.c

and then compile it to a binary file using gcc -c -DMS_WIN64 -shared -pthread -Wall -IC:/Users/[username]/AppData/Local/Programs/Python/Python39/Include -LC:/Users/[username]/AppData/Local/Programs/Python\Python39\libs -o app.exe renamed_module_comp.c.

With these two steps, I fall into no errors and they compile just fine. But now when I attempt to execute app.exe, I get the following error:

This app can't run on your PC. To find a version for your PC, check with the software publisher.

As reported/commented by other developers on the web, app.exe seem to be a DLL file. So, I tried to copy app.exe into an external folder, open python terminal from that directory, and call import app. With that I get:

ImportError: DLL load failed while importing app: %1 is not a valid Win32 application.

Unfortunately I don't know where to go from here. Any direction is really appreciated.

OS: Windows 10 x64
Python Version: Python 3.9.1
Cython Version: Cython version 0.29.23
GCC Version: gcc.exe (GCC) 9.2.0
GUI Libs: PyQT5 - Tkinter and pysimplegui 

Summary of the question: I basically, want to compile my cython GUI app into a standalone executable program.

Author:neehack,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/70637418/how-to-compile-a-cython-program-to-a-standalone-executable
yy