kewlbear/Python-iOS

Importing Modules #2

IvanNextToJunior opened this issue · 4 comments

I would like to know how to import Python modules. I tried to use it like this:

override func viewDidLoad() {
super.viewDidLoad()
PythonSupport.initialize()
}
extension PlayerViewController: PlayerDelegate {

func play() {
    
    guard
        let url = URL(string: audioString)
    
    else {
        return
    }
    url.insertPythonPath()

Basically, you add the path to the module, then import it.

let sys = Python.import("sys")
sys.path.insert(1, pathToModule)
let module = Python.import("module")

See https://github.com/kewlbear/YoutubeDL-iOS/blob/811ea597014bc483f44737a1d80c54d0896c36b0/Sources/YoutubeDL/YoutubeDL.swift#L147 for more sophisticated example.

Thank you! And what is the major version of your PythonKit package? I put 0.0.1 in swift package manager, but it gave me an error

You should not use my fork. Use https://github.com/pvieito/PythonKit.

Thank you