gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:40:01 25 4
gpt4 key购买 nike

我现在正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com