iOS 项目中调用python脚本

  1. 导入python.framework: https://github.com/pybee/Python-iOS-support/releases
  2. 设置homepath: Py_SetPythonHome(pythonHome);
#pragma mark - private 

- (void)p_setupHomePath {
    BOOL isEvn = [self configPythonEnvironment];

    if (!isEvn) {
        return;
    }
    const char * frameworkPath = [[NSString stringWithFormat:@"%@/Resources",[self p_pythonFrameworkPath]] UTF8String];
    wchar_t  *pythonHome = _Py_char2wchar(frameworkPath, NULL);
    Py_SetPythonHome(pythonHome);
}

- (NSString*)p_pythonFrameworkPath{
    NSString *documantPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *newFrameworkPath = [documantPath stringByAppendingPathComponent:@"Python.framework"];
    return newFrameworkPath;
}

-(BOOL)configPythonEnvironment
{
    NSBundle *mainBundle = [NSBundle mainBundle];
    NSString *bundlePath = [mainBundle pathForResource:@"PythonEnvironment" ofType:@"bundle"];
    BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:bundlePath];
    if(!isExist) {
        return NO;
    }
    NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
    NSString *PythonPath = [bundle pathForResource:@"Python" ofType:@"framework"];
    isExist = [[NSFileManager defaultManager] fileExistsAtPath:PythonPath];
    if (!isExist) {
        return NO;
    }
    NSString *newFrameworkPath = [self p_pythonFrameworkPath];
    if ([[NSFileManager defaultManager] fileExistsAtPath:newFrameworkPath]) {
        return YES;
    }
    NSError * error;
    [[NSFileManager defaultManager] copyItemAtPath:PythonPath toPath:newFrameworkPath error:&error];
    if (error) {
        return NO;
    }
    return YES;;
}

注意这里的PythonEnvironment. bundle ,是对python.framework解压后,添加一些自己需要的第三方库

  1. 初始化,运行python代码:
 Py_Initialize();
 PyRun_SimpleString("print('hello world')");
  1. 运行py脚本
dispatch_queue_t queue = dispatch_queue_create(0, DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        //run python scipt
        NSString *scriptPath = [[NSBundle mainBundle] pathForResource:@"Test" ofType:@"py"];
        
        FILE *mainFile = fopen([scriptPath UTF8String], "r");
        
        PyRun_SimpleFile(mainFile, (char *)[[scriptPath lastPathComponent] UTF8String]) ;
        
    });
  1. 释放解释器
Py_Finalize();

最后的代码:


#import "PythonRunner.h"
#import 

@implementation PythonRunner

+(Boolean)run{

    
    BOOL isEvn = [self configPythonEnvironment];
    if(!isEvn){
        return false;
    }
    
    [PythonRunner p_setupHomePath];
    Py_Initialize();
    //run simple python code
    PyRun_SimpleString("print ('hello')");//say hello see debug output :)
    
    dispatch_queue_t queue = dispatch_queue_create(0, DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        //run python scipt
        NSString *scriptPath = [[NSBundle mainBundle] pathForResource:@"Test" ofType:@"py"];
        
        FILE *mainFile = fopen([scriptPath UTF8String], "r");
        
        PyRun_SimpleFile(mainFile, (char *)[[scriptPath lastPathComponent] UTF8String]) ;
        
    });
    return true;
}
+ (void)p_setupHomePath {
    
    const char * frameworkPath = [[NSString stringWithFormat:@"%@/Resources",[self p_pythonFrameworkPath]] UTF8String];
    wchar_t  *pythonHome = _Py_char2wchar(frameworkPath, NULL);
    Py_SetPythonHome(pythonHome);
}

+ (NSString*)p_pythonFrameworkPath{
    NSString *documantPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *newFrameworkPath = [documantPath stringByAppendingPathComponent:@"Python.framework"];
    return newFrameworkPath;
}
+ (BOOL)configPythonEnvironment
{
    NSBundle *mainBundle = [NSBundle mainBundle];
    NSString *bundlePath = [mainBundle pathForResource:@"PythonEnvironment" ofType:@"bundle"];
    BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:bundlePath];
    if(!isExist) {
        return NO;
    }
    NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
    NSString *PythonPath = [bundle pathForResource:@"Python" ofType:@"framework"];
    isExist = [[NSFileManager defaultManager] fileExistsAtPath:PythonPath];
    if (!isExist) {
        return NO;
    }
    NSString *newFrameworkPath = [self p_pythonFrameworkPath];
    if ([[NSFileManager defaultManager] fileExistsAtPath:newFrameworkPath]) {
        return YES;
    }
    NSError * error;
    [[NSFileManager defaultManager] copyItemAtPath:PythonPath toPath:newFrameworkPath error:&error];
    if (error) {
        return NO;
    }
    return YES;;
}
@end

参考地址:https://www.jianshu.com/p/80b5be51fb1d?tdsourcetag=s_pctim_aiomsg

你可能感兴趣的:(iOS 项目中调用python脚本)