Swift は PythonKit を使用して Python を呼び出します

Xcode プロジェクトを開きます。次に、「ファイル→パッケージの追加」を選択し、パッケージの依存関係リンクを入力します。

https://github.com/pvieito/PythonKit.git

https://github.com/kewlbear/Python-iOS.git

 

 

Python-iOS パッケージを使用すると、iOS アプリケーションで Python モジュールを使用できるようになります。

使用法:

import PythonSupport

PythonSupport.initialize()

PythonKit は、Python と対話するための Swift フレームワークです。

使用法:

import PythonKit

let sys = Python.import("sys")

print("Python \(sys.version_info.major).\(sys.version_info.minor)")
print("Python Version: \(sys.version)")
print("Python Encoding: \(sys.getdefaultencoding().upper())")

電話の例

テスト.py

#!/usr/bin/python3
#coding=utf8

def hello():
    print('hello world')
    return 'hello'

迅速:

import PythonKit
import PythonSupport

class PythonManager {
    func initManager() {
        //初始化
        PythonSupport.initialize()

        //导入sys模块
        let sys = Python.import("sys")
        print("Python \(sys.version_info.major).\(sys.version_info.minor)")
        print("Python Version: \(sys.version)")
        print("Python Encoding: \(sys.getdefaultencoding().upper())")
        print("Python Path: \(sys.path)")

        //运行python脚本
        PythonSupport.runSimpleString("print('hello')")

        //运行test.py文件
        let python = Python.import("test")
        let result = python.hello()
        print(result)
    }
}

おすすめ

転載: blog.csdn.net/watson2017/article/details/132585951