[Python-Dev] Py_Main() seems to be a NOOP

Patrick Rutkowski patrick.rutkowski at gmail.com
Thu Aug 10 02:26:23 EDT 2017

I'm working on Windows with Python 3.6. I'm trying to make a wWinMain() GUI
application that uses an embedded python interpreter. I'm having various
issues with being unable to load extension modules, but I won't go into that
now because I've tracked my issue down to a much simpler test case.

To begin, consider the source code of pythonw.exe:

    /* Minimal main program -- everything is loaded from the library. */

    #include "Python.h"

    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>

    int WINAPI wWinMain(
        HINSTANCE hInstance,      /* handle to current instance */
        HINSTANCE hPrevInstance,  /* handle to previous instance */
        LPWSTR lpCmdLine,         /* pointer to command line */
        int nCmdShow              /* show state of window */
        return Py_Main(__argc, __wargv);

Now consider a simple python script, which I save in source.py

    f = open('C:\\Users\\rutski\\Desktop\\hello.txt', 'w')
    f.write('Hello World!\n')

and run with

    > pythonw.exe source.py

This produces the file `hello.txt` with the expected output. Now I create a
new Visual Studio solution. I make it Win32 project, choose "Windows
Application" and "Empty Project." I create a new source file main.c and I
copy-paste the source code of from pythonw (with Py_Main and all that). Now I
add the following settings:

    C:\Users\rutski\Documents\python\PCbuild\amd64 --- Library Search Directory
    C:\Users\rutski\Documents\python\Include       --- Include Directory
    C:\Users\rutski\Documents\python\PC            --- Include
Directory (for pyconfig.h)

I choose "Debug | x64" and hit build. I pop open cmd.exe, browse to where
mything.exe is, and execute

    > mything.exe source.py

But this time nothing happens. No hello.txt gets created. I get no crash
window or error message. I do not get thrown into a debugger. I just get no
result. Am I missing some build flags here or something?

I'm running the __exact__ same C code that pythonw.exe is, but mine isn't
working. What gives?

I can't even seem to get Py_Main() to execute some python code from within my
own application, so trying to write my own embed code is basically hopeless.

If anybody could shed any light on this it would be much appreciated!

More information about the Python-Dev mailing list