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.

7
  • What do you mean by " but Python is not embedded" exactly? The program doesn't run something different? Commented Feb 13, 2020 at 8:52
  • 2
    @ead I think what OP means is "Python is not statically linked into the executable" Commented Feb 13, 2020 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: stackoverflow.com/q/48796014/5769463 which is different than on Windows... Commented Feb 14, 2020 at 9:31
  • DavidW is right. But am I misunderstanding the meaning of embedded in github.com/cython/cython/wiki/EmbeddingCython? Commented Feb 14, 2020 at 9: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) on Windows when using cython --embed? Commented Jul 18, 2022 at 19:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.