Trouble loading python file using PythonKit

I have followed the various tutorials so that I can run a python file from within Swift. I have gotten everything set up and run the file, but I get the following error.

Could not access PythonObject member

I have took the following steps so that I can access the python file.

  • In Targets -> Signing & Capabilities, set hardened runtime to Disable Library Validation
  • In Targets -> Signing & Capabilities, remove App Sandbox

From my research, those two things should be all I need to do besides adding the PythonKit package and preparing the Python file.

My code is below:

import Cocoa
import PythonKit
import Alamofire

class ViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let sys = Python.import("sys")

        print("Python \(sys.version_info.major).\(sys.version_info.minor)")
        print("Python Version: \(sys.version)")
        sys.path.append("/Users/username/")
        let example = Python.import("test")
        example.main()
    }

 }

The error occurs on the last line. The file path in sys.path.append is correct.

Mac OS: 11.2.2 Xcode version: 12.5

Terms of Service

Privacy Policy

Cookie Policy