why failed to embed custom python module in c++ func called by a cocoa app

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:

  1. sys.path.append
    or
    sys.path.insert
    ---doesn't work
  2. add
    __init__.py
    to
    app2
    or
    app2/app2
    or both ---doesn't work
  3. add
    .pth
    with the content
    /Users/ABC/Desktop/app2/app2
    to the
    /usr/local/ opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages
    ---doesn't work
  4. copy the
    BNRapp2.py
    file to the folder
    /usr/local/opt/python/Frameworks/ Python.framework/Versions/3.7/lib/python3.7/site-packages
    ---doesn't work
  5. copy the
    BNRapp2.py
    file to the folder
    /usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload
    or
    /usr/local/opt/ python/Frameworks/Python.framework/Versions/3.7/lib/python3.7
    ---work fine!


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']

Accepted Reply

Finally find out it is because of cocoa app's sandbox control. Turn it off and it will work fine. Or will need another method to work it out.

Replies

Finally find out it is because of cocoa app's sandbox control. Turn it off and it will work fine. Or will need another method to work it out.