This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Py_Main() is totally broken on Visual Studio 2017
Type: Stage: resolved
Components: Windows Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: paul.moore, rutski, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2017-08-10 07:23 by rutski, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg300055 - (view) Author: Patrick Rutkowski (rutski) Date: 2017-08-10 07:23
Install Visual Studio 2017

Download and unpack Python-3.6.2.tgz

Open a Visual Studio command prompt
Browse to Python-3.6.2\PCBuild
Run build.bat -p x64 -c Release
Run build.bat -p x64 -c Debug
Add the PCbuild\amd64 directory to your PATH

Create a new Visual Studio Win32 GUI project called mypythonw

Copy-paste the source code of Python-3.6.2\PCBuild\WinMain.c into your mypthonw project.

Add Python-3.6.2\Include and Python-3.6.2\PC to mypythonw's include path.

Add Python-3.6.2\PCbuild\amd64 to mypythonw's library search path

Build mypthonw

Run these two commands:

pythonw -c "import ctypes; ctypes.windll.user32.MessageBoxW(0, 'Hello World!', 'Hello', 0)

mypythonw -c "import ctypes; ctypes.windll.user32.MessageBoxW(0, 'Hello World!', 'Hello', 0)

The first will show a message box, while the second will show nothing. You won't get an error message or a debugger from mypythonw.exe. It'll just silently do nothing.

There must be something in the configuration of pythonw.vcxproj that is necessary for Python embedding to work, and which makes it work in pythonw but fail in mypythonw. The two projects are executing the exact same C code, but one works and the other doesn't. What's going on here?
msg300057 - (view) Author: Patrick Rutkowski (rutski) Date: 2017-08-10 07:37
Just for kicks I tried the same Py_Main() code from a Win32 console application (instead of from a GUI application). The C code this time was

#include <Python.h>

int wmain(int argc, wchar_t** argv) {
    return Py_Main(argc, argv);
}

The resulting error message when trying to run the program is now

===========================================================
> mypythoncmd.exe -c "import ctypes; ctypes.windll.user32.MessageBoxW(0, 'Hello World!', 'Hello', 0)"

Fatal Python error: Py_Initialize: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'

Current thread 0x000009f0 (most recent call first):
OUTPUT ENDS HERE
===========================================================
msg300060 - (view) Author: Patrick Rutkowski (rutski) Date: 2017-08-10 07:56
I removed my custom built Python and installed the one provided by the python-3.6.2-amd64.exe installer instead.

The Win32 Command Line application now works, and shows the message box. The Win32 GUI Application still fails to work, the output is just nothing. As expected, pythonw works as always, it shows the popup.
msg300085 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2017-08-10 13:29
Your final file layout needs to match any of our standard ones. On my phone now so I'm not going to write them all out (will do it later if you need), but you need to put your .exe in the same location as the python.exe for the runtime you're using (or move the runtime around your exe).

Also look at the embeddable zip package. It's meant for this (though you still need a full install to build). I do this all the time.
History
Date User Action Args
2022-04-11 14:58:49adminsetgithub: 75355
2017-09-04 19:45:19steve.dowersetstatus: open -> closed
resolution: not a bug
stage: resolved
2017-08-10 13:29:37steve.dowersetmessages: + msg300085
2017-08-10 07:56:57rutskisetmessages: + msg300060
2017-08-10 07:40:59christian.heimessetassignee: christian.heimes ->

components: + Windows, - SSL
nosy: + paul.moore, tim.golden, zach.ware, steve.dower, - christian.heimes
2017-08-10 07:37:30rutskisetnosy: + christian.heimes
messages: + msg300057

assignee: christian.heimes
components: + SSL
2017-08-10 07:23:51rutskicreate