2

I try to create an executable using Cython with embedded Python (look here). I can find examples doing this on Linux, but not on Windows. The only Windows example I can find results in an executable that calls the python dll (here). But this is not what I am looking for. I want a standalone executable that does not call the python dll.

Below what I did:

First the program (hello.py):

print("hello")

I run Cython with the --embed option in an anaconda prompt:

cython hello.py --embed

This results in a c file with a main function (hello.c).

Then set the appropriate environment variables:

"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat"

Then compile and link this:

"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\x86_amd64\cl.exe"  /nologo /Ox /MD /W3 /GS- /DNDEBUG -Ic:\Anaconda3_5.2.0\include -Ic:\Anaconda3_5.2.0\PC /Tchello.c /link /OUT:"test.exe" /SUBSYSTEM:CONSOLE /LIBPATH:C:\Anaconda3_5.2.0\libs /LIBPATH:C:\Anaconda3_5.2.0\PCbuild\amd64 "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\LIB\amd64" "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\ATLMFC\LIB\amd64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.10240.0\ucrt\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\lib\um\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\8.1\lib\winv6.3\um\x64"

This results in a small (19 KB) executable (test.exe).

To run this set the PYTHONPATH

set PYTHONPATH=C:\Anaconda3_5.2.0

and call the program

test.exe

This runs whitout any problems, but Python is not embedded as it calls python36.dll. I suppose the compiling and linking must be done differently, but I don't know how. Does anybody know how to create a windows executable that embeds the Python interpreter using Cython? Any help will be greatly appreciated.

Basj
  • 41,386
  • 99
  • 383
  • 673
Frank Tap
  • 164
  • 1
  • 10
  • What do you mean by " but Python is not embedded" exactly? The program doesn't run something different? – ead Feb 13 '20 at 08:52
  • 2
    @ead I think what OP means is "Python is not statically linked into the executable" – DavidW Feb 13 '20 at 20:04
  • 2
    "embeded Python" doesn't mean the Python-library is statically linked. The first question, do you have static python library in your distribution? Here is a question about static linking on Linux: https://stackoverflow.com/q/48796014/5769463 which is different than on Windows... – ead Feb 14 '20 at 09:31
  • DavidW is right. But am I misunderstanding the meaning of embedded in https://github.com/cython/cython/wiki/EmbeddingCython? – Frank Tap Feb 14 '20 at 09:54
  • 1
    @ead By the way this is a good idea! Is there a way to statically link Python-library (python38.dll and even vcruntime140.dll etc. and maybe all the DLLs [in the list given here](https://stackoverflow.com/a/62392087/1422096)) on Windows when using `cython --embed`? – Basj Jul 18 '22 at 19:56
  • Something else @ead (I know you have high Cython knowledge from your other answers to my other questions :)), do you have an idea for this: https://stackoverflow.com/questions/73027842/cython-embed-on-windows? – Basj Jul 18 '22 at 20:00
  • this question seems like a duplicate of: https://stackoverflow.com/questions/48796014/compile-python-code-to-statically-linked-executable-with-cython?noredirect=1&lq=1 https://stackoverflow.com/questions/2581784/can-cython-compile-to-an-exe?noredirect=1&lq=1 https://stackoverflow.com/questions/62390978/minimal-set-of-files-required-to-distribute-an-embed-cython-compiled-code-and-ma?noredirect=1&lq=1 – nferreira78 Jul 25 '22 at 11:10

0 Answers0