首页 > 解决方案 > 我需要通过 macOS 应用程序中的 NSTask 执行 python 脚本,但如果应用程序被沙盒化,则脚本无法找到模块。有什么建议吗?

问题描述

我想通过 macOS 应用程序中的 NSTask 执行 python 脚本。我的代码如下:

NSString *image1Path = [[NSBundle mainBundle] pathForResource:@"a1" ofType:@"jpg"];

NSString *image2Path = [[NSBundle mainBundle] pathForResource:@"a2" ofType:@"jpg"];

NSString *predictorPath = [[NSBundle mainBundle] pathForResource:@"shape_predictor_68_face_landmarks" ofType:@"dat"];

NSString *pythonScriptPath = [[NSBundle mainBundle] pathForResource:@"faceMorph" ofType:@"py"];

NSTask *task = [NSTask new];
   
[task setExecutableURL:[NSURL fileURLWithPath:@"/usr/bin/python"]];
   
[task setArguments:@[pythonScriptPath,@"-p1",image1Path,@"-p2",image2Path,@"-a",@"0.5",@"-ex",@"/Users/fofo/a3.jpg",@"-predictorpath",predictorPath]];
    

[task launch];
[task waitUntilExit];

如果我禁用沙盒,脚本运行得很好。但是,如果我启用沙盒,则输出如下:

Traceback (most recent call last):
  File "/Users/fofo/Library/Developer/Xcode/DerivedData/FaceMorph_macOS-cmfgvhivheuzioacarvvcyjpokyp/Build/Products/Debug/FaceMorph macOS.app/Contents/Resources/faceMorph.py", line 4, in <module>
    import cv2
ImportError: No module named cv2

非常感谢任何建议。谢谢。

标签: pythonobjective-cmacoscocoaappstore-sandbox

解决方案


推荐阅读