[Pythonmac-SIG] py2app and stripped os.environ

Ronald Oussoren ronaldoussoren at mac.com
Tue Mar 15 18:09:28 CET 2011


On 15 Mar, 2011, at 11:57, Christopher Barker wrote:

> On 3/15/11 6:11 AM, Ronald Oussoren wrote:
>>> Once deployed as .app using py2app, I am not getting all the env vars that are
>>> available to users.
> 
>> That's on OSX issue, and not a problem in py2app.  Most of the environment variables you see from the command-line are initialized by the shell and/or Terminal.app and are not available to GUI applications. You'd see the same issue with a native application.
> 
> Indeed, and we did explain that to the OP on the wxPython list.

It would have been nice if he'd mentioned that in his post here.

> 
> However, it seems that it is fairly common that folks write applications that call command line utilities. In this case, they need the environment to be set up the way that it is in the user's default shell. I suggest that it might be nice to have an option for py2app to build an app that will initialize the environment that a user would see in their standard shell.

That is non-trivial, especially when the user has specified a different shell in the Terminal preferences.  To get the right environment you'd probably have to:

* Reverse engineer the default shell created by Terminal.app
* Parse the environment plist file (I don't recall the name, but have an PyObjC example that uses is)
* Run 'login -f USER' in a subprocess, then run the "env" command using that shell

All of this should be done in a bootstrap script (in py2app), and code should then be added to the distutils command to ensure that the script gets included when an option is specified (lets say, "--emulate-shell-environment")

All of this is definitely possible, but I don't know when I'll get around to doing it.   

If someone can write a function that fetches the information I can easily integrate it into py2app. This should be a function that takes no arguments and updates os.environ, something like:

.. def _emulate_shell_environment():
..     import os
..     os.environ['KEY'] = 'value'

BTW. I've filed issue #15 for this, to ensure the request doesn't get lost (<https://bitbucket.org/ronaldoussoren/py2app/issue/15/py2app-should-have-way-to-emulate-shell>)

Ronald



More information about the Pythonmac-SIG mailing list