Mac App with Python Embedded: Sandbox Blocks Python Script from Running Only in Release Mode?

I have an XPC service that embeds Python. It executes a python script on behalf of the main app.

The app and xpc service are sandboxed. All seems to work just fine in the development environment but the script fails in the released version.

I disabled writing pycache by setting the PYTHONDONTWRITEBYTECODE environment variable because pycache tries to write inside my app bundle which fails (I believe I can redirect the pycache directory with PYTHONPYCACHEPREFIX and may experiment with that later).

Specifically this line fails in the release version only (not from Xcode):

    PyObject *pModule = PyImport_Import(moduleNameHere);
   if (pModuleOwnedRef == NULL)
    {
           // this is null in release mode only.
    }

Any ideas what can be going wrong? Thanks in advance.

It’s hard to explain exactly what’s failing without knowing more about how Python is expecting to work within your app

Thanks for the reply Quinn. I'm using the released version from the repository Python-Apple-support.

I needed to set DEAD_CODE_STRIPPING to NO to get this to work in release mode.

So running in release mode I discover I get this error:

symbol not found in flat namespace '__PyFloat_Pack2'

Are there linker flags I need to add to get this to work in release mode? Thanks

It’s hard to explain exactly what’s failing without knowing more about how Python is expecting to work within your app. However, An Apple Library Primer explains the whole flat namespace thing.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

It’s hard to explain exactly what’s failing without knowing more about how Python is expecting to work within your app

Thanks for the reply Quinn. I'm using the released version from the repository Python-Apple-support.

I needed to set DEAD_CODE_STRIPPING to NO to get this to work in release mode.

Mac App with Python Embedded: Sandbox Blocks Python Script from Running Only in Release Mode?
 
 
Q