Pregunta

I am working on a simple(?) embedded Python project. I have a custom package that has been installed into site-packages with 'setup.py install', e.g.:

in C:\Python27\Lib\site-packages\:

mypackage\
    __init__.py
    subpackage\
        __init__.py
        subpackage.py
    .... 
    mymodule.py

Just doing some simple embedding calls, I am getting some behavior that doesn't match what I get in a cmd window running Python. Specifically:

PyRun_SimpleString("import mypackage") //Success (return == 0)
PyRun_SimpleString("from mypackage import subpackage") //Success
PyRun_SimpleString("from mypackage import mymodule") //Fail (return == -1)

...whereas all of these work just fine in a cmd window (no ImportError, and I can get expected results on e.g. dir(mymodule)

I'm aware that the interpreter that results from Py_Initialize() is a little different from what you get in a cmd window, notably sys.path...having read some of the other answers on SO I tried inserting '' as the first element of sys.path: PyRun_SimpleString("import sys\nsys.path.insert(0,'')") prior to the failing import, but no luck, still returns -1. Also tried appending sys.path with 'C:\Python27\Lib\site-packages\mypackage', but still no luck importing 'mymodule' (mymodule.py).

Based on other examples on SO and other sites, I've tried a few variants on import, e.g.

__import__('mypackage',globals(), locals(), fromlist=['mymodule'])
__import__('mypackage.mymodule',globals(), locals(), fromlist=['mymodule'])

Also tried PyImport_ImportModuleEx and as with PyRun_SimpleString, it worked for everything except "from mypackage import mymodule".

Furthermore: this scenario works just fine under MacOS/Python 2.7. It's just under Windows that it's failing.

Any ideas where this could be going off the rails?

UPDATE: some additional information: 'subpackage.py' imports an extension library (let's call it 'utilites.pyd'). I'm able to import other ".py" modules that do not import this.

¿Fue útil?

Solución

Taking the rare opportunity to answer my own question...

Once I narrowed the problem down to modules that were importing the utilities.pyd extension, I did some digging around with the Dependency Walker tool and discovered that there was an error loading MSVCR90.DLL (file not found). In the old days, you could just drop that DLL into your application's path and it would usually work.

But nowadays (Vista and after), it's a bit more involved. After reading this interesting blog post about an older MSVCR DLL, I figured I would try updating the <dependency> section of the manifest of my .pyd file to point to the same MSVCR90.DLL as Python27.DLL which resides in the \Windows\WinSxS (side-by-side) folder.

After that, it worked like a charm. Now I just need to figure out how to automatically include the dependency section during python setup.py mypackage, but that's another question :-)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top