0

Ok I'm fairly new to swift and am trying to create an app with a button that uses python. I have seem code looking like

//
//  ContentView.swift
//  Shared
//
//  Created by Ulto4 on 10/17/21.
//

import SwiftUI
import PythonKit

struct ContentView: View {
    @State private var showDetails = false
    @State var result : String = " "
   
    var body: some View {
        HStack{
        Text("Hello, world!")
            .padding()
            Button(action : {
                self.coolPerson()
            }, label: {
                Text("Respones")
            })
        Text("\(result)")
        
    }
    }
    func coolPerson(){
        let sys = Python.import("sys")
        sys.path.append("/Users/ulto4/Documents/vsc")
        let example = Python.import("ahhhhh")
        let response = example.hi()
        result = response.description
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
            .preferredColorScheme(.dark)
    }
}

The code in the python file is


def hello():
    return "cool"

However, when I click the button I get this error



2021-10-17 17:53:16.943097-0700 GAAIN[27059:2277939] PythonKit/PythonLibrary.swift:46: Fatal error: Python library not found. Set the PYTHON_LIBRARY environment variable with the path to a Python library.
(lldb) 

I also clicked the .xcodeproj and deleted the Apple Sandbox. But it still doesn't work. Since I'm fairly new I don't know how to do this. Any help would be appreciated.

EDIT: As per the comments, IOS doesn't support PythonKIT

3
  • PythonKit requires Swift 5 or higher and has been tested on macOS, Linux and Windows. no iOS support Commented Oct 18, 2021 at 1:04
  • Thanks. Is there anyway to make a quick change from IOS to macOs in Swift? Commented Oct 18, 2021 at 1:09
  • Just create a new MacOS project and copy your code into it Commented Oct 18, 2021 at 1:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.