I wrote a simple wxPython app which I package with py2app, turning it into a self-contained double-clickable .app bundle which I can give to other people to run.
My app uses some locale-specific settings — specifically, it wants to parse dates in text form — so it lets the user specify what locale to use, and it calls locale.getdefaultlocale() to prepopulate this choice.
I tested this by running the python code directly (in the system-installed Python, not packaged) and it worked fine, then I packaged it with py2app and tested the resulting self-contained app with “open dist/foo.app” and it worked fine. Then I gave it to somebody else and they complained it immediately threw an exception; I tried double-clicking my .app in the Finder and lo and behold, locale.getdefaultlocale() is indeed returning None (the exception resulted from me making method calls on that object).
I wrote a simple test app that dumps the environment and the value of locale.getdefaultlocale(), and ran it in the same 3 environments. Results:
– invoke directly from Terminal as “python envdump.py”: dumps my full environment
– package with py2app, invoke from Terminal as “open dist/envdump.app”: dumps my full environment
– invoke that same dist/envdump.app by opening it from Finder: environment is much more limited, and yes, locale.getdefaultlocale() returns (None, None), because LANG is unset.
The complete list of environment variables set by the Finder is: ARGVZERO, Apple_PubSub_Socket_Render, COMMAND_MODE, DISPLAY, EXECUTABLEPAT, HOME, LOGNAME, PATH, PYTHONPATH, RESOURCEPATH, SHELL, SSH_AUTH_SOCK, TMPDIR,USER,__CF_USER_TEXT_ENCODING.
I can easily make my code tolerate the lack of LANG/locale.getdefaultlocale(), but I’d like to get that information somehow.
The values for environment variables for applications launched (by double-clicking, say) are not the same as when run under a terminal window shell. There is a way to specify those by creating
~/.MacOSX/environment.plist: see here for more details. But that’s probably not what you want to do. If you need to create a default for your app, you can add it to the app’s plist as described here.