我无法使用PythonKit在Xcode 11中导入Python模块 [英] I can't import Python modules in Xcode 11 using PythonKit

查看:185
本文介绍了我无法使用PythonKit在Xcode 11中导入Python模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在使用Swift Package Manager.

I am now using Swift Package Manager.

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

Using that, I imported PythonKit into my Swift project.

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

I can't import the Python modules using the PythonKit now.

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

It asks me to set the PYTHON_LIBRARY path but I don't know how to do that.

有人可以帮助我吗?

//
//  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应用时,这是错误消息

This is error message when I run the iOS app on my iPhone

致命错误:找不到Python库.使用Python库的路径设置PYTHON_LIBRARY环境变量.:文件/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库的路径设置PYTHON_LIBRARY环境变量.:文件/Users/herogold/Library/Developer/Xcode/DerivedData/VideoStream-cjytedddvtktmybclqlztmfdbekk/SourcePackages/checkouts/PythonKit/PythonKit/PythonLibrary.swift,第40行

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答案,请原谅格式/等.我经历了一段时间,遇到了不同的错误,但总体上是相同的问题.希望这对您有所帮助-可以考虑一些资源:

First SO answer, so please forgive formatting / etc. I suffered through this for a while myself with different errors, but generally same issues. I hope this helps you -- a few resources to consider:

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

1) Pyto -- a fully embedded Python environment for iOS/Catalyst; with LXML and Python Library porting instructions <-- this is what you need to model after to run on iOS, my solution works for Mac Catalyst (Macs with Python preloaded)

2) Python套件教程-这个家伙一步一步地进行着,如何实现PythonKit

2) Python Kit Tutorial -- this guy goes through, step by step, how to implement PythonKit

这对我有用:

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

1) Disable App Sandbox in Signing and Capabilities:

在应用程序沙箱"右上角的签名和所有者"下功能中有一个"X",请单击以删除应用程序沙箱"

2)在增强的运行时"的签名和功能"下:选中禁用库验证"

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

用于禁用库验证的复选框的图像

现在,我还没有按照这些规定向App Store提交应用,但是至少我的python文件和库可以加载/构建/运行!

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:

UPDATE 05/15/2020:

对于Mac开发者发行版,您将必须对应用程序随附的所有.so或.dylib以及Python解释器和bin文件夹进行签名.开发完成后,我制作了一个快速的bash脚本来运行每个脚本.

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
}


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


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

这篇关于我无法使用PythonKit在Xcode 11中导入Python模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆