Swift PythonKit with error for no module found

97 views Asked by At

I'm trying to run some python code from Xcode but wasn't sure why it can't find the module. All the tutorial I found only shows importing the simple .py file that contains function only no import on the top. I'm using python3. TIA

import SwiftUI
import PythonKit

struct ContentView: View {
    
    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundStyle(.tint)
            Text("Hello, world!")
        }
        .onAppear() {
            runPython()
        }
        .padding()
    }
    
    func runPython() {
        let sys = Python.import("sys")
        sys.path.append(Config.Path.basePath)
        let test = Python.import("example")
        let response = test.hello()
        print(response)
    }
}

example.py

import pandas

def hello():
    return "Hello Python"

Error:

PythonKit/Python.swift:706: Fatal error: 'try!' expression unexpectedly raised an error: Python exception: No module named 'pandas'

1

There are 1 answers

0
14079_Z On BEST ANSWER

Figured it out that I had pandas install in the wrong Python version.

$ python3.11 -m pip install pandas

fixed it.