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

标签 python ios swift xcode swift-pythonkit

我现在正在使用 Swift 包管理器。

使用它,我将 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 应用程序时的错误消息

Fatal error: Python library not found. Set the PYTHON_LIBRARY environment variable with the path to a Python library.: file /Users/herogold/Library/Developer/Xcode/DerivedData/VideoStream-cjytedddvtktmybclqlztmfdbekk/SourcePackages/checkouts/PythonKit/PythonKit/PythonLibrary.swift, line 40
2019-07-20 23:55:00.967869+0800 VideoStream[31841:170718] Fatal error: Python library not found. Set the PYTHON_LIBRARY environment variable with the path to a Python library.: file /Users/herogold/Library/Developer/Xcode/DerivedData/VideoStream-cjytedddvtktmybclqlztmfdbekk/SourcePackages/checkouts/PythonKit/PythonKit/PythonLibrary.swift, line 40

最佳答案

第一个 SO 回答,所以请原谅格式化/等。我自己经历了一段时间,出现了不同的错误,但通常是相同的问题。我希望这对您有所帮助 - 需要考虑的一些资源:

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

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

这是对我有用的:

1) 在签名和功能中禁用 App Sandbox:

In top right corner of App Sandbox, under Signing & Capabilities there is an "X", click that to remove App Sandbox

2) 在 Signing and Capabilities 下的“Hardened Runtime”中:勾选“Disable Library Validation”

Image of checkbox for Disable Library Validation

现在,我还没有根据这些规定向 App Store 提交应用程序,但至少我的 python 文件和库加载/构建/运行!


2020 年 5 月 15 日更新:

对于 Mac Developer Distribution,您必须签署应用程序中包含的所有 .so 或 .dylib,以及 Python Interpreter 和 bin 文件夹。完成开发后,我制作了一个快速的 bash 脚本来运行每个脚本。

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
}


这将允许您在签名和功能中使用 AppSandbox,以及所有强化的运行时选项(如不禁用库验证)。

关于python - 我无法使用 PythonKit 在 Xcode 11 中导入 Python 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57126337/

相关文章:

ios - Swift 中的 EXC_BAD_ACCESS 错误代码

ios - Swift:如何从函数返回类类型

c# - 每天首次调用app时添加文字提示?

ios - 通过网络代理工具下载或查看作为多部分请求(PNG、PDF)发送的文件?

ios - 如何将数据转换为URL()?

xcode - 在 swift 中使用 xcpretty 命令运行 xcodebuild 时出错

python - 如何使用 API 与 Google Colab 交互?

python - 从句子中查找并选择所需的单词?

将数据库从 sqlite 更改为 mysql 时,Python manage.py 迁移错误

python - IDLE 未集成在桌面中