【xcode】ios项目中运行python

安装ios的python解释器

https://github.com/pybee/Python-iOS-support/releases
下载之后有两个framework文件夹,供后面使用
在这里插入图片描述

创建ios项目

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

新建Cocoa Touch Class

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

将下载好的两个框架文件夹链接进项目中

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

创建PythonEnvironment.bundle

在这里插入图片描述
在这里插入图片描述

之后将Python.framework添加进bundle中

在这里插入图片描述

之后编写代码

PythonRunner.m

#import "PythonRunner.h"
#import <Python/Python.h>

@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 :)
    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

PythonRunner.h

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface PythonRunner : NSObject
+(Boolean)run;+ (void)p_setupHomePath;+ (BOOL)configPythonEnvironment;
@end

NS_ASSUME_NONNULL_END

main.m

#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "PythonRunner.h"

int main(int argc, char * argv[]) {
    NSString * appDelegateClassName;
    @autoreleasepool {
        // Setup code that might create autoreleased objects goes here.
        appDelegateClassName = NSStringFromClass([AppDelegate class]);
        PythonRunner.run;
    }
    return UIApplicationMain(argc, argv, nil, appDelegateClassName);
}

build

在这里插入图片描述
失败

添加链接库

在这里插入图片描述

再次build

在这里插入图片描述

在这里插入图片描述

成功

另:运行.py文件

在项目中创建py文件

在这里插入图片描述
在这里插入图片描述

编写测试代码
a=1
b=2
c=a+b
print(c)
print("hello")

在PythonRunner.m的run函数中添加如下代码
     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]) ;

     });
build

在这里插入图片描述

成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值