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.