即使在可可应用程序中将自定义python模块路径附加到sys.path之后,为什么仍会收到ModuleNotFoundError?

时间:2019-07-18 13:43:47

标签: python c++ xcode

我试图将python模块嵌入可可应用程序的c ++中,并卡在ModuleNotFoundError中。

我使用xcode,app1和app2创建了2个项目。第一个是命令行工具应用程序,第二个是可可应用程序。同时,我分别为它们创建了2个.py文件,并获得2个文件夹,如下所示。

BNRcallApp1.cppBNRcallApp2.cpp的代码基本相同,而第2个.py文件则完全相同,而app1可以正常工作,而app2一直在那儿大喊大叫是ModuleNotFoundError。

我在这里想念什么?有什么帮助吗?非常感谢!

  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

我已经尝试了以下建议:

  1. sys.path.appendsys.path.insert-不起作用

  2. __init__.py添加到app2app2/app2或两者都--不起作用

  3. .pth和内容/Users/ABC/Desktop/app2/app2添加到/usr/local/ opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages ---不起作用

  4. BNRapp2.py文件复制到文件夹/usr/local/opt/python/Frameworks/ Python.framework/Versions/3.7/lib/python3.7/site-packages                                                 ---不起作用

  5. BNRapp2.py文件复制到文件夹/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中                                                 ---很好!

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
           {
               ...
           }
       }
       ...
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
           {
               ...
           }
       }
       ...
BNRapp1.py & 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'] 

我希望使用sys.path.append导入c ++中的python模块,但失败并出现ModuleNotFoundError

1 个答案:

答案 0 :(得分:0)

最后发现这是由于可可应用的沙箱控件所致。关闭它,它将正常工作。否则将需要另一种方法来解决。