Hi all, I am trying embed custom python module in c++ func called by a cocoa app recently and get stuck in the `ModuleNotFoundError`.
I created 2 projects using xcode, app1 & app2. The first one is an Command Line Tool app and the second one is a Cocoa app. Meanwhile, I created 2 .py file for them separately and get 2 folders look like below.
The code of the
BNRcallApp1.cpp
and the BNRcallApp2.cpp
are basically the same and The 2 .py
file are exactly the same, while the app1 works fine and the app2 keeps on yelling that there is a ModuleNotFoundError.I have already tried the following instructions:
orsys.path.append
---doesn't worksys.path.insert
- add
to__init__.py
orapp2
or both ---doesn't workapp2/app2
- add
with the content.pth
to the/Users/ABC/Desktop/app2/app2
---doesn't work/usr/local/ opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages
- copy the
file to the folderBNRapp2.py
---doesn't work/usr/local/opt/python/Frameworks/ Python.framework/Versions/3.7/lib/python3.7/site-packages
- copy the
file to the folderBNRapp2.py
or/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload
---work fine!/usr/local/opt/ python/Frameworks/Python.framework/Versions/3.7/lib/python3.7
What am I missing here? Is there any particular configures needed to be set when it comes to Cocoa app? Any help? Thanks a lot!
------------------------------------------------------------------------
***Directory: /Users/ABC/Desktop/app1
app1's folder:
---app1
---BNRapp1.py
---BNRcallCPP.h
---BNRcallCPP.mm (objective-c call c++)
---BNRcallApp1.hpp
---BNRcallApp1.cpp (c++ call python module)
---...
---app1.xcodeproj
------------------------------------------------------------------------
***Directory: /Users/ABC/Desktop/app2
app2s' folder:
---app2
---BNRapp2.py
---BNRcallCPP.h
---BNRcallCPP.mm
---BNRcallApp2.hpp
---BNRcallApp2.cpp
---...
---app2.xcodeproj
---app2Tests
---app2UITests
------------------------------------------------------------------------
***The main code of the BNRcallApp1.cpp:
...
{ Py_Initialize();
if(Py_IsInitialized())
{
PyObject *pModule=NULL;
PyObject *pFunc=NULL;
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('/Users/ABC/Desktop/app1/app1')");
PyRun_SimpleString("print(sys.path)");
pModule=PyImport_ImportModule("BNRapp1");
if(pModule)
{
...
}
else
{
...
}
}
else
{
...
}
}
...
------------------------------------------------------------------------
***The main code of the BNRcallApp2.cpp:
...
{ Py_Initialize();
if(Py_IsInitialized())
{
PyObject *pModule=NULL;
PyObject *pFunc=NULL;
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('/Users/ABC/Desktop/app2/app2')");
PyRun_SimpleString("print(sys.path)");
pModule=PyImport_ImportModule("BNRapp2");
if(pModule)
{
...
}
else
{
...
}
}
else
{
...
}
}
...
------------------------------------------------------------------------
***the code of the BNRapp1.py & the BNRapp2.py:
def feedback(user):
return "hello!"
------------------------------------------------------------------------
***Both the "print('sys.path')" command shows basically the same result:
['/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python37.zip', '/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7', '/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload', '/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages',
'/Users/ABC/Desktop/app1/app1']
['/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python37.zip', '/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7', '/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload', '/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages',
'/Users/ABC/Desktop/app2/app2']