PythonKit library not working after releasing it to testflight

I have integrated pythonkit in my code to run some python code. It works perfectly, If I am running via Xcode.

But, when I uploaded it to testflight and trying to run the same code from testflight build, it is crashing with crash report having below details:

PythonKit/PythonLibrary.swift:59: Fatal error: 'try!' expression unexpectedly raised an error: Python library not found. Set the PYTHON_LIBRARY environment variable with the path to a Python library.

@pvieito Can you please help me about how to set PYTHON_LIBRARY env variable to make it working in release mode? Any help is appreciated.

See this sample project with explanations in the README.

@sspringer Thanks for reply.

I already had followed all of these steps. Its even working perfectly fine when I am running it via xcode(in debug mode). But its failing while running it on production/release mode. I dont know how to set the python library in release mode

It’s just setting an environment variable.

…But to be honest, when working with PythonKit I also experienced a fail on one machine without obvious reason, and it does not seem to be working on Windows, so we do not use PythonKit any more, which is a shame. Moreover the conversion of nested structures is a problem (e.g. converting an array of triples). PythonKit would need some stewardship and/or a more active community around it, it is a wonderful tool. The call of throwing functions should be more elegant.

I think I have encountered the same problem.
It can work on debug run to my iphone,but can't work on release or TestFlight.
Where the error occurred:
PythonKit- PythonInterface- init-builtins = PythonObject(PyEval_GetBuiltins())
on PythonLibrary+Symbols:

  • Py_IncRef(pointer)*

About my iOS_project:I used both PythonKit and Python Apple support
And did the following work:
guard let stdLibPath = Bundle.main.path(forResource: "python-stdlib", ofType: nil) else { return }
guard let stdDynloadPath = Bundle.main.path(forResource: "python-stdlib/lib-dynload", ofType: nil) else { return }
setenv("PYTHONHOME", stdLibPath, 1)
setenv("PYTHONPATH", "(stdLibPath):(stdDynloadPath)", 1)

And add Sign Python Binary Modules to Xcode - taget - Build Phases like the USAGE on Python Apple support

Now I also hope someone can help solve this problem,It has been bothering me for two months now :exploding_head: :exploding_head: :exploding_head:,If you have already resolved it, please share your approach. Many thanks

I have same question,It work when I set:
Setting ->Enable Testability -> release -Yes
I'm not sure why. It's probably related to file read-write permissions?

But now it make a new question,
when I distribute on app store connect,show error:

Asset validation failed
Invalid bundle structure. The “” binary file is not permitted. Your app cannot contain standalone executables or libraries, other than a valid CFBundleExecutable of supported bundles. For details, visit: (ID: 74345377-b2bb-466b-bc4d-e3edb8a8f429)

Including all the files:
All the .io in python-stdlib/lib-dynload,and python-stdlib/config-3.10-iphoneos, python-stdlib/config-3.10-iphonesimulator.arm64 , python-stdlib/config-3.10-iphonesimulator.x86_64

I hope someone knows what's going on。

1 Like

This fixed it for me.