一尘不染

我无法使用PythonKit在Xcode 11中导入Python模块

swift

我现在正在使用Swift Package Manager。

使用它,我将PythonKit导入了我的Swift项目。

我现在无法使用PythonKit导入Python模块。

它要求我设置PYTHON_LIBRARY路径,但我不知道该怎么做。

谁能帮我?

//
//  ViewController.swift
//  VideoStream
//
//  Created by HeRo Gold on 7/20/19.
//  Copyright © 2019 TopAce. All rights reserved.
//

import UIKit
import PythonKit

let sys = Python.import("sys")

class ViewController: UIViewController {

    @IBOutlet weak var netflixView: WKWebView!
    let netflixURL = URL(string: "https://www.netflix.com/login")

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        let pymsl = try Python.import(name: "pymsl")

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

当我在iPhone上运行iOS应用时,这是错误消息

致命错误:找不到Python库。设置PYTHON_LIBRARY环境变量以及Python库的路径。:文件/Users/herogold/Library/Developer/Xcode/DerivedData/VideoStream-
cjytedddvtktmybclqlztmfdbekk/SourcePackages/checkouts/PythonKit/PythonKit/PythonLibrary.swift,第40行,
2019-07- 20 23:55:00.967869 + 0800 VideoStream
[31841:170718]致命错误:找不到Python库。设置PYTHON_LIBRARY环境变量以及Python库的路径。:文件/Users/herogold/Library/Developer/Xcode/DerivedData/VideoStream-
cjytedddvtktmybclqlztmfdbekk/SourcePackages/checkouts/PythonKit/PythonKit/PythonLibrary.swift,第40行


阅读 421

收藏
2020-07-07

共1个答案

一尘不染

第一个SO答案,所以请原谅格式/等。我经历了一段时间,遇到了不同的错误,但总体上是相同的问题。我希望这对您有帮助-需要考虑的一些资源:

1) Pyto-适用于iOS /
Catalyst的完全嵌入式Python环境;带有LXML和Python库移植说明
<-这是在iOS上运行后需要建模的东西,我的解决方案适用于Mac
Catalyst (预装了Python的Mac)

2)Python Kit教程
-这个人一步一步地介绍了如何实现PythonKit

这对我有用:

1)在签名和功能中禁用应用沙箱:

在“应用程序沙箱”的右上角的“签名和功能”下,有一个“
X”,单击以删除该应用程序沙箱

2) In “Hardened Runtime” under Signing and Capabilities: check “Disable
Library Validation”

Image of checkbox for Disable Library
Validation

Now, I haven’t yet submitted an app to the App Store under these provisions,
but at least my python files and libraries load / build / run!


UPDATE 05/15/2020:

For Mac Developer Distribution, you will have to sign all .so or .dylib’s
included with your app, along with the Python Interpreter and bin folder. I
made a quick bash script to run through each one when done with dev.

function signThese() {
        find . -name "*.$1" | while read line; do
                codesign --force --verbose=4 --options=runtime --timestamp --sign "Developer ID Application: [INSERT YOUR CERT HERE]" $line
        done
}

This will allow you to use AppSandbox in Signing and Capabilities, and all
Hardened Runtime Options (as in not disabling library validation).

2020-07-07