I can't import Python modules in Xcode 11 using PythonKit
Asked Answered
R

4

9

I am now using Swift Package Manager.

Using that, I imported PythonKit into my Swift project.

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

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

Can anyone help me?

//
//  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())")
    }
}

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

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

Rajput answered 20/7, 2019 at 15:57 Comment(1)
did it solve? i am running into the same issue. I guess you are trying to use it in Single-view App in xcode.Cavill
E
11

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 -- 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 Kit Tutorial -- this guy goes through, step by step, how to implement PythonKit

Here's what worked for me:

1) Disable App Sandbox in Signing and Capabilities:

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

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).

Evanevander answered 30/7, 2019 at 13:20 Comment(4)
They load, build and run on both iOS simulator and iOS device?Schofield
Pyto built and ran on iOS simulator and iOS device at the time of writing (July 30) -- with the new Xcode updates, I imagine something broke, though haven't checked.Evanevander
Thanks, check "Disable Library Validation" solves my problemAntiquated
@Evanevander can you tell me how to use this bash script because I'm stuck in the app distribution.Naphthalene
C
5

iOS does not have a python interpreter. thats the reason it is not being able to understand any pyhtonkit api. so the way this works for MacOS it wont work for iOS. Pythonkit does not support iOS for the same reason and has not been tested for the same.

Cavill answered 20/5, 2020 at 5:3 Comment(0)
C
1

You can use my fork of PythonKit. https://github.com/kewlbear/PythonKit This Swift package embeds Python into your app.

Competency answered 28/11, 2020 at 12:56 Comment(1)
I'm using your package in iOS and it works in Debug builds not in Release like from TestFlight. Crashes when creating a PythonObjectLeggett
B
0

It used to work for me but with recent MacOS update it stopped and the the solution listed above with relaxing security settings on the application did not work for me. Only adding the python library to the application solved it. You may:

  • Add a build stage step to copy python library (in my case libpython3.8.dylib but I copied the whole folder) to the application resources. I copied it to Contents/Resources/Python. You may need to sign it.

  • Before the first call to PythonKit set an environment variable to point to the library

    let bundlePath = Bundle.main.bundlePath let python_lib_path = "\(bundlePath)/Contents/Resources/Python/libpython3.8.dylib" setenv("PYTHON_LIBRARY", python_lib_path, 0)

The plus (and minus) of this approach is that you do not depend on the Python installed on the end user machine as you distribute it yourself.

Butt answered 14/6, 2022 at 16:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.