2

I'm using PythonKit in my Swift project for MacOS. At the moment I'm using Python 2.7 but from MacOs 12.3 it isn't no more supported so I'm trying to migrate to Python 3 but it doesn't work.

func applicationDidFinishLaunching(_ notification: Notification) {
    if #available(OSX 12, *) {
        PythonLibrary.useVersion(3)
    }
    else {
        PythonLibrary.useVersion(2)
    }
            
    let sys = Python.import("sys")
    print("Python \(sys.version_info.major).\(sys.version_info.minor)")
    print("Python Version: \(sys.version)")
    print("Python Encoding: \(sys.getdefaultencoding().upper())")
    sys.path.append(Bundle.main.resourceURL!.absoluteURL.path)

    let checker = Python.import("checkLibrary")
    _ = Array(checker.check())
}

This is the error message:

PythonKit/PythonLibrary.swift:46: Fatal error: Python library not found. Set the PYTHON_LIBRARY environment variable with the path to a Python library.

The code fail on MacOs 12 on line 9th line (let sys = Python.import("sys")), so I can't interact so sys in any way. I've already tried to disable sandbox and Hardened Runtime but is seems useless.

1 Answer 1

3

I was having the same issue.

  • where python3
  • which python3
  • type -a python3

I could clearly see that Python3 was present using any of the above commands from terminal. Python3 wasnt something that I directly installed, (probably something I added during an install of XCode) but I could see it located at "/usr/bin/python3"

I had already removed the sandbox and disabled the hardened runtime. Adding the environment variable to XCode did not work and Google ultimately told me to do what had already been done.

Finally, I decided to just perform a fresh install, following the blog as guidance.

https://www.dataquest.io/blog/installing-python-on-mac/#installing-python-mac

https://www.python.org/downloads/macos/ (direct URL for Python download)

After installing, everything worked as expected.

  • PythonLibrary.useVersion(3)

  • PythonLibrary.useLibrary(at: "/usr/local/bin/python3")

I could use either of the above methods, without adding the environment variable to the XCode scheme.

Hopefully that helps

Sign up to request clarification or add additional context in comments.

1 Comment

it works even with the python virtual environment I have tested it.

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.