wxPython from an Embedded Interpreter

Hello,

  I have been experimenting with embedding python in C applications.
I wanted to try using wxPython since that is my GUI library of
choice. I came up with this simple example but when I run it I get an
exception:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File ".\main.py", line 8, in <module>
    import wx
  File "C:\Users\m308027\workspace\PythonEmbedded\wx_test\site\wx-2.8-
msw-unicode\wx\__init__.py", line 45, in <module>
    from wx._core import *
  File "C:\Users\m308027\workspace\PythonEmbedded\wx_test\site\wx-2.8-
msw-unicode\wx\_core.py", line 4, in <module>
    import _core_
ImportError: DLL load failed: A dynamic link library (DLL)
initialization routine failed.

If I import the same wxPython library with a regular python
interpreter it works just fine.

Here is the C code I am running:

#include <Python.h>
int main(int argc, char* argv[]) {
  Py_Initialize();
  PySys_SetPath("python27.zip;.;");

  PyRun_SimpleString("import main; main.run()");

  Py_Finalize();
  return 0;
}

and here is my python program:
import os
import site
site_dir = os.path.join( os.getcwd(), "site" )
site.addsitedir(site_dir) #put the CWD on the path so .pth files can
be added

import wx

def run():
    pass

I've looked at the embedded example but they seem to be manipulating
the wx objects on the C side as well. I have no intention of doing
that. Is there any way to get my little example working?

This typically indicates that the _core_.pyd extension module is not able to dynamically load one or more of the DLLs that it depends upon. Use the dependency walker tool to see what it needs and then make sure that those DLLs are on the PATH. You can probably also run your C program within Dependency Walker and see exactly what is failing to load and probably some more information related to the error.

One more thought comes to mind. You should make sure that your program is built with the same RTL flags as Python and wxPython (/MD) as having mismatched C libs loaded in the same program can cause similar issues.

···

On 3/19/12 8:59 AM, David Marsh wrote:

Hello,

   I have been experimenting with embedding python in C applications.
I wanted to try using wxPython since that is my GUI library of
choice. I came up with this simple example but when I run it I get an
exception:

Traceback (most recent call last):
   File "<string>", line 1, in<module>
   File ".\main.py", line 8, in<module>
     import wx
   File "C:\Users\m308027\workspace\PythonEmbedded\wx_test\site\wx-2.8-
msw-unicode\wx\__init__.py", line 45, in<module>
     from wx._core import *
   File "C:\Users\m308027\workspace\PythonEmbedded\wx_test\site\wx-2.8-
msw-unicode\wx\_core.py", line 4, in<module>
     import _core_
ImportError: DLL load failed: A dynamic link library (DLL)
initialization routine failed.

--
Robin Dunn
Software Craftsman

I checked the RTL flags and they're the same.

The dependency walker helped me find one dll that wasn't on the path
but now there are more.

Thanks for the advice.

···

On Mar 20, 2:36 pm, Robin Dunn <ro...@alldunn.com> wrote:

On 3/19/12 8:59 AM, David Marsh wrote:

> Hello,

> I have been experimenting with embedding python in C applications.
> I wanted to try using wxPython since that is my GUI library of
> choice. I came up with this simple example but when I run it I get an
> exception:

> Traceback (most recent call last):
> File "<string>", line 1, in<module>
> File ".\main.py", line 8, in<module>
> import wx
> File "C:\Users\m308027\workspace\PythonEmbedded\wx_test\site\wx-2.8-
> msw-unicode\wx\__init__.py", line 45, in<module>
> from wx._core import *
> File "C:\Users\m308027\workspace\PythonEmbedded\wx_test\site\wx-2.8-
> msw-unicode\wx\_core.py", line 4, in<module>
> import _core_
> ImportError: DLL load failed: A dynamic link library (DLL)
> initialization routine failed.

This typically indicates that the _core_.pyd extension module is not
able to dynamically load one or more of the DLLs that it depends upon.
Use the dependency walker tool to see what it needs and then make sure
that those DLLs are on the PATH. You can probably also run your C
program within Dependency Walker and see exactly what is failing to load
and probably some more information related to the error.

One more thought comes to mind. You should make sure that your program
is built with the same RTL flags as Python and wxPython (/MD) as having
mismatched C libs loaded in the same program can cause similar issues.

--
Robin Dunn
Software Craftsmanhttp://wxPython.org