beeware / Python-Apple-support

A meta-package for building a version of Python that can be embedded into a macOS, iOS, tvOS or watchOS project.
MIT License
1.12k stars 161 forks source link

Error PythonLibrary.loadSymbol in version with TestFlight #210

Closed pavlo-kravchenko closed 6 months ago

pavlo-kravchenko commented 6 months ago

Describe the bug

I get error in app from TestFlight PythonLibrary.loadSymbol(name:legacyName:type:) ()

But all work good on device if I install app from Xcode

Steps to reproduce

  1. Install app from TestFlight
  2. Run Python.import("sys")

Expected behavior

Not crash

Screenshots

Снимок экрана 2024-03-17 в 14 26 25 Снимок экрана 2024-03-17 в 14 27 38

Environment

  • iPhone 14 Pro
  • iOS 16.6
  • Python version 3.9

Logs

#2  (null) in specialized static PythonLibrary.loadSymbol<A>(name:legacyName:type:) ()
#3  (null) in specialized static PythonLibrary.loadSymbol<A>(name:legacyName:type:) ()
#4  (null) in one-time initialization function for Py_Initialize ()
#5  (null) in _dispatch_client_callout ()
#6  (null) in _dispatch_once_callout ()
#7  0x0000000104856944 in Py_Initialize.unsafeMutableAddressor at /***/SourcePackages/checkouts/PythonKit/PythonKit/PythonLibrary+Symbols.swift:41
#8  0x0000000104856944 in PythonInterface.init() at /***/SourcePackages/checkouts/PythonKit/PythonKit/Python.swift:678
#9  0x0000000104856944 in one-time initialization function for Python at /***/SourcePackages/checkouts/PythonKit/PythonKit/Python.swift:663
#10 (null) in _dispatch_client_callout ()
#11 (null) in _dispatch_once_callout ()
#12 0x0000000104851f14 in Python.unsafeMutableAddressor at /***/SourcePackages/checkouts/PythonKit/PythonKit/Python.swift:663
#13 0x00000001047eb1ec in closure #2 in LibShell.run(command:appendToHistory:) at *let sys = Python.import("sys")*
#14 (null) in thunk for @escaping @callee_guaranteed () -> () ()

Additional context

Code swift

            let scriptsDirectory = FileManager.default.urls(for: .documentDirectory, in: .allDomainsMask)[0]
            DispatchQueue.main.async {
                do {
                    let sys = Python.import("sys")
                    if #available(iOS 16.0, *) {
                        sys.path.append(scriptsDirectory.path())
                    } else {
                        sys.path.append(scriptsDirectory.path)
                    }

                    // Add arguments
                    arguments[2...arguments.count - 1].forEach { arg in
                        sys.argv.append(arg)
                    } 

                    if let script = try? Python.import("hello.py") {
                        let response = script.main()
//Result from main() func
                    }
                } catch {
//Error
                }
            }

Code python

import sys

def main():
    return f"Hello Swift, I'm Python {sys.argv[1]} end"
freakboy3742 commented 6 months ago

Thanks for the report. It looks like you're using this package using a Python Swift wrapper (I'm guessing PythonKit), which isn't a configuration we officially support. We're happy to incorporate any fixes that are needed to support PythonKit (or any other Swift integration library), but we're not in a position to provide much help for this configuration.

The only thing I can think of that might be related to the error you're seeing is binary stripping. Xcode has a build setting "Enable Testability", which is enabled for debug builds (the default for Xcode-local builds), and disabled for release builds (required for submission to the App Store). This setting strips the compiled binary of "unused" symbols to minimise the size of the final executable - however, because of the dynamic nature of Python, all symbols appear as "unused" from the perspective of the compiler. It is therefore necessary to enable the "Enable Testability" setting on release builds. This setting is enabled when you use this package in a Briefcase app (the officially supported usage), but won't be if you've built your own Xcode project from scratch.

pavlo-kravchenko commented 6 months ago

Thanks a lot This really helped

khpeterson commented 5 months ago

Thank you for this tip, this was key for me as well.