Windows installer from python source code without access to source code

Eryk Sun eryksun at gmail.com
Fri Mar 31 13:54:42 EDT 2023


On 3/31/23, Jim Schwartz <jschwar at sbcglobal.net> wrote:
> I want a windows installer to install my application that's written in
> python, but I don't want the end user to have access to my source code.

Cython can compile a script to C source code for a module or
executable (--embed). The source can be compiled and linked normally.
For example, the following builds a "hello.exe" executable based on a
"hello.py" script.

    > cython -3 --embed hello.py
    > set "PYI=C:\Program Files\Python311\include"
    > set "PYL=C:\Program Files\Python311\libs"
    > cl /I"%PYI%" hello.c /link /libpath:"%PYL%"
    > copy hello.exe embed
    > embed\hello.exe
    Hello, World!

I extracted the complete embeddable distribution of Python 3.11 into
the "embed" directory. You can reduce the size of the installation, if
needed, by minimizing the zipped standard library and removing pyd
extensions and DLLs that your application doesn't use.

The generated "hello.c" is large and not particularly easy to read,
but here are some snippets [...]:

    [...]
    /* Implementation of 'hello' */
    static PyObject *__pyx_builtin_print;
    static const char __pyx_k_main[] = "__main__";
    static const char __pyx_k_name[] = "__name__";
    static const char __pyx_k_test[] = "__test__";
    static const char __pyx_k_print[] = "print";
    static const char __pyx_k_Hello_World[] = "Hello, World!";
    [...]
      /* "hello.py":1
     * print("Hello, World!")             # <<<<<<<<<<<<<<
     */
      __pyx_tuple_ = PyTuple_Pack(1, __pyx_kp_u_Hello_World);
            if (unlikely(!__pyx_tuple_)) __PYX_ERR(0, 1, __pyx_L1_error)
    [...]
      /* "hello.py":1
     * print("Hello, World!")             # <<<<<<<<<<<<<<
     */
      __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_print, __pyx_tuple_,
                                      NULL);
            if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 1, __pyx_L1_error)
    [...]
    int wmain(int argc, wchar_t **argv) {
    [...]
        if (argc && argv)
            Py_SetProgramName(argv[0]);
        Py_Initialize();
        if (argc && argv)
            PySys_SetArgv(argc, argv);
    [...]
              m = PyInit_hello();
    [...]
        if (Py_FinalizeEx() < 0)
            return 2;
    [...]
        return 0;
    [...]


More information about the Python-list mailing list