py2app/wxpython os.getenv and path not working inside .app

Hi,

Mac Leopard 10.5.5 Intel
python 2.6.6
wxpython 2.8.11.0
py2app 0.5.2

My program is working fine in source environment. In source environment
‘os.getenv’ is working fine. When deploying .app using
py2app ‘os.getenv’ is returning none. I am also facing problems when executing
external program using wx.Process. These programs
are available on path and there is no problem when executing them using terminal
or running from my script in source environment.
I tried using full path of these external programs but they are also complaining
about their own environment vars missing.

When not giving full path of external programs, I am getting an error saying:
execvp(shaderglsl test.glsl)failed with error 2
This means .app does not understand what ‘shaderglsl’ is.

When using standard commands such “ifconfig eth0”, .app is working fine. This is confusing.

I am new to mac and absolutely node idea why .app is not able to access
environment variables.

Cheers

Prashant

This would appear to be a problem with py2app, and not wxPython.

···


Hi, I will kill all ads in google gmail.
They will all be dead and gone for all my emails to you. HA HA bye bye ads I just massacred you!!!

King wrote:

My program is working fine in source environment. In source environment
'os.getenv' is working fine.

In most cases, you should use the os.environ dictionary instead of the
os.getenv function. It's easy to get a disconnect between these two.
If you change a variable with os.putenv, for example, that will not be
reflected in os.environ.

When deploying .app using py2app 'os.getenv' is returning none. I am
also facing problems when executing
external program using wx.Process. These programs are available on
path and there is no problem when executing them using terminal
or running from my script in source environment. I tried using full
path of these external programs but they are also complaining
about their own environment vars missing.

Where would their environment variables have come from? Are they being
set up by your code before you call wx.Execute? How are you setting up
the new variables? Have you considered using the standarad Python
subprocess module instead of wx.Execute?

When not giving full path of external programs, I am getting an error
saying:
execvp(shaderglsl test.glsl)failed with error 2
This means .app does not understand what 'shaderglsl' is.

Maybe you could show us the real code. I'm not sure it will help, but
perhaps we can spot something.

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

THe default environment given to app bundles when they are launched may not be the same that you see in your Terminal, because things like .profile or .bashrc are not run before launching the application. (IIRC) There are a few ideas that come to mind to work around this:

* Reduce or eliminate your app's dependency on environment variables. You can do something like load values from a config file instead. If you need to launch other processes that need environment variables set then you can put them in the environment before launching.

* Fiddle with the app bundle such that the file executed in the bundle (yourapp.app/Contents/MacOSX/yourapp) is a shell script that exports values to the environment and then execs the real app executable.

* Add the environment variables you need in ~/.MacOSX/environment.plist. IIRC the values in this file will be put into the environment when opening app bundles.

···

On 3/10/11 4:28 AM, King wrote:

Hi,

Mac Leopard 10.5.5 Intel
python 2.6.6
wxpython 2.8.11.0
py2app 0.5.2

My program is working fine in source environment. In source environment
'os.getenv' is working fine. When deploying .app using
py2app 'os.getenv' is returning none. I am also facing problems when
executing
external program using wx.Process. These programs
are available on path and there is no problem when executing them using
terminal
or running from my script in source environment.
I tried using full path of these external programs but they are also
complaining
about their own environment vars missing.

When not giving full path of external programs, I am getting an error
saying:
execvp(shaderglsl test.glsl)failed with error 2
This means .app does not understand what 'shaderglsl' is.

When using standard commands such "ifconfig eth0", .app is working fine.
This is confusing.

I am new to mac and absolutely node idea why .app is not able to access
environment variables.

--
Robin Dunn
Software Craftsman

I am new to mac and absolutely node idea why .app is not able to access
environment variables.

This is actually a Mac issue, I don't think it's either py2app and certainly not a wxPython one.

The trick is the distinction between command line apps and GUI apps.

When you run Python from the command line, you get the environment set up there -- everything that bash (or whatever) has, system env variables, stuff from your .profile, etc.

When a py2app bundle is run, it gets the environment provided for GUI apps, which certainly doesn't include anything in your .profile, ,bashrc, etc, as bash isn't involved at all. I'm a bit surprised that the there is nothing there, but I don't expect the PATH to be set, etc -- PATH is all about command line tools.

There was discussion about this sometime in the last few months on the pythonmac list -- I'd look for that, and maybe ask a question there about how to solve your particular problems.

I tried using full path of these external programs but they are also
complaining
about their own environment vars missing.

ouch, ugly!

I wonder if subprocess would work better, but I suppose you may need to somehow get a full command line environment set up to run these commands -- maybe /usr/bin/env would help? You may be able to run env inside bash, and get the users bash environment that way. ugly, I know -- I'd check the pythonmac list for a better way.

When not giving full path of external programs, I am getting an error
saying:
execvp(shaderglsl test.glsl)failed with error 2
This means .app does not understand what 'shaderglsl' is.

When using standard commands such "ifconfig eth0", .app is working fine.
This is confusing.

that implies that there is some PATH environment variable set -- I wonder why getenv() didn't work?

HTH,

   -Chris

···

On 3/10/11 4:28 AM, King wrote:

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

The program is shader compiler and there are many render engines that can use theses shaders. User can set render engine in preferences
to use. There are many render engines and they must be installed separately. These engines has their own environment vars.

  1. My app is NOT setting a single environment var.
  2. The environment vars of engines are not available using os.getenv once deployed as .app.
  3. os.expandvars is also suffering from same issue. vars are not getting expanded because they are not available.
  4. You cannot start render engine by using short executable name. It seems .app doesn’t understand what it is.
  5. If you give full path name of render engine executable, it starts the engine but engine complains about missing env vars of it’s own.
  6. There are many rendering engines and they set a hell lot of env vars when installed. It would be difficult to maintain the list of engines and their
    env vars and set them manually.
  7. The only option is to fiddle with py2app and solve the problem in general rather then engine dependent. This can be possible is all the system/environment vars is available inside .app.

Prashant

King wrote:

The program is shader compiler and there are many render engines that
can use theses shaders. User can set render engine in preferences
to use. There are many render engines and they must be installed
separately. These engines has their own environment vars.

And where do they put them? In your local .bashrc or .bash_profile? Or
are they stored somewhere else?

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

I have two engines installed and they put their vars in .bash_profile. I have no idea about others.

Prashant

There is no such thing as "all the system/environment variables" -- what environment variables are available is environment dependent -- always!

So py2app is not broken.

You are also right that setting up the environment specific for a user's needs in your app is not a good approach.

It seems to me that your utilities are expecting a properly set up command line shell -- i.e. stuff set in .profile, or whatever. In that case, I'd get the environment by running the shell from your app -- if done right, it should initialize itself, and you can see what's there -- as I said in a earlier post "/usr/bin/env" may help here.

As for py2app -- I suppose one could have an option that it would start up, run the users shell, then make sure all those Environment variables are set. If you write that code, you could offer up a patch. However, this seems a bit dangerous to me, though I can't articulate why.

Good topic for the pythonmac list.

-Chris

···

On 3/10/11 5:15 PM, King wrote:

7. The only option is to fiddle with py2app and solve the problem in
general rather then engine dependent. This can be possible is all the
system/environment vars is available inside .app.

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Hi Chris,

I have got some idea about you suggestion, unfortunately I am not well verse with *nix platform. When working in source environment ‘os.environ’ has

all the vars including the one required/set by render engines. Once .app is deployed, ‘os.environ’ doesn’t return these vars (missing). I posted this topic in pythonmac list but so far no response.

Robin suggested to look inside (yourapp.app/Contents/MacOSX/

yourapp), but it’s a binary executable rather then shell script. I tried opening in komodo edit.

I am also ready to apply any hack until problem is getting solved but can’t think of any.

Prashant

Hi Chris,

I have got some idea about you suggestion, unfortunately I am not well
verse with *nix platform. When working in source environment
'os.environ' has

all the vars including the one required/set by render engines. Once .app
is deployed, 'os.environ' doesn't return these vars (missing). I posted
this topic in pythonmac list but so far no response.

Robin suggested to look inside (yourapp.app/Contents/MacOSX/

yourapp), but it's a binary executable rather then shell script. I tried
opening in komodo edit.

No, I said that you could try making the yourapp.app/Contents/MacOSX/yourapp *be* a shell script that sets the variables you need and then execs the real yourapp binary. But that would be a hack of last resort IMO.

I am also ready to apply any hack until problem is getting solved but
can't think of any.

Did you try setting the environment variables in ~/.MacOSX/environment.plist? It's an XML file so be sure to look up the expected format if you don't already have a file you can add to. You may have to log out and back in for the changes to take effect.

If you don't want to have to rely on the users to put the values in their ~/.MacOSX/environment.plist file then your application can do something like look at the environ to see if the necessary values are present, if not then ask the user what paths should be used and then save them in a configuration file. Then when you run the subprocesses your app can just put those values into the environ given to the child process.

···

On 3/12/11 1:12 AM, King wrote:

--
Robin Dunn
Software Craftsman

I made some progress.
If you app name is test.app then you must be having a file here with same as you app:

test.app/Contents/MacOS/test

  1. Rename ‘test’(executable) as ‘launch’.
  2. Create a script name ‘test’ (No extension, same name as of you app and set executable permission)
  3. Inside script write this:

DIR=$(cd $(dirname “$0”); pwd)
cd $DIR
open launch

First line is required or else you will get an error saying The file Users/MacAdmin/launch does not exist’.
I can’t use “./launch” instead of ‘open’ or else same behavior as mentioned above posts(missing vars).

This solution is working fine and inside .app ‘os.getenv’ & os.expandvars’ is also working fine. The only problem is when opening .app, terminal shows
up and I have to find an option to hide it.

cd to current directory option is taken from this post:
http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-in
I am hoping that it’ll work in all cases.

Prashant

That first line is a bit of overkill. This should be the same:

DIR=$(dirname "$0")

Also, I think that using open causes a new child process for the 'launch' executable. Using exec instead should replace the current process and is usually the pattern that *nix launcher scripts follow.

DIR=$(dirname "$0")
exec $DIR/launch

···

On 3/13/11 12:20 AM, King wrote:

I made some progress.
If you app name is test.app then you must be having a file here with
same as you app:

test.app/Contents/MacOS/test

1. Rename 'test'(executable) as 'launch'.
2. Create a script name 'test' (No extension, same name as of you app
and set executable permission)
3. Inside script write this:

DIR=$(cd $(dirname "$0"); pwd)
cd $DIR
open launch

--
Robin Dunn
Software Craftsman

Thanks Robin,
It’s working and no terminal window is displayed but again I am not getting all user env vars which I am getting when using ‘launch’ instead of ‘exec’.
Another hack I can think of is to get all the vars(env command) in script and one by one (loop iteration) set them inside the script.(name and value pair). I am hoping it would work.

Prashant

you might try /usr/bin/env rather than either 'launch' or "exec", though I think that starts a new process as well.

-Chris

···

On 3/14/11 9:56 AM, King wrote:

Thanks Robin,
It's working and no terminal window is displayed but again I am not
getting all user env vars which I am getting when using 'launch' instead
of 'exec'.

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Hi Chris,
I am sorry but I didn’t get it. (Don’t know much about *nix platform and shell scripting)

Prashant

Ok,

This is working script and solution for the problem as of now.

#!/bin/bash
DIR=$(dirname “$0”)
exec $DIR/launch

Prashant

well, the "env" command can be used to get the current environment, but it's real purpose to is to be able to run a process with a particular environment. So I thought it might be helpful here.

However, it looks like you're got a solution that works.

It might be worth a note to the pythonmac list - it might be nice to have this as a standard option in py2app -- people do seem to have the need for it once in a while.

-Chris

···

On 3/14/11 10:05 AM, King wrote:

Hi Chris,
I am sorry but I didn't get it. (Don't know much about *nix platform and
shell scripting)

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Well. it’s also broken half way. When you start the app using terminal ‘open test.app’, it works but when you start by clicking .app in ‘Finder’,
again I am not getting all the user env vars. Posted again in pythonmac but no response so far.
This is really getting into my nerves now…

Prashant

Ok, one more time: Try setting the values in ~/.MacOSX/environment.plist

Or: Ask the user what values to put into the child process' environment and save the values in a config file for the next time you need them.

···

On 3/15/11 1:24 AM, King wrote:

Well. it's also broken half way. When you start the app using terminal
'open test.app', it works but when you start by clicking .app in 'Finder',
again I am not getting all the user env vars. Posted again in pythonmac
but no response so far.
This is really getting into my nerves now.....

--
Robin Dunn
Software Craftsman