我需要从macOS应用程序通过NSTask执行python脚本,但是如果应用程序是沙盒的,则脚本无法找到模块。有什么建议吗?

2024-09-26 17:54:27 发布

您现在位置: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

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


Tags: 脚本taskmacosjpgnsbundlensstringnstaskimage1path

热门问题