Python Nautilus script

J. Cliff Dyer jcd at sdf.lonestar.org
Mon Sep 15 16:52:54 EDT 2008


On Mon, 2008-09-15 at 22:00 +0200, Diez B. Roggisch wrote:
> Michel Leunen schrieb:
> > Diez B. Roggisch a écrit :
> > 
> >> There shouldn't be a difference between a shell-script and a 
> >> python-script. Environment-variables are a unix-process-thing, and 
> >> thus the rules that govern them apply to *all* processes - the shell 
> >> is one of these, there is nothing special to it.
> >>
> >> If the shell-script gets the variable, the python-script will as well.
> > 
> > Yes, that's what I thought too but try this: open a terminal and type
> > 
> > $ echo $HOSTNAME
> > 
> > you will get the name of your computer.
> > Now try this instead:
> > 
> > $ python
> >  >>> import os
> >  >>> os.environ['HOSTNAME']
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in <module>
> >   File "/usr/lib/python2.5/UserDict.py", line 22, in __getitem__
> >     raise KeyError(key)
> > KeyError: 'HOSTNAME'
> >  >>>
> 
> Which is the exact right thing to happen if the HOSTNAME is not exported.
> 
> The echo above is executed IN THE CURRENT SHELL environment. If it 
> weren't - why would there be any distinction between local and exported 
> variables at all?
> 
> If you put
> 
> 
> > It appears that's because HOSTNAME is not exported.
> > But in the case of Nautilus script, how to workaround this issue?
> 
> I don't know for sure if the shell has something build-in that makes it 
> spawn shell-subprocesses with a different environment than other processes.
> 
> However, if you want you can do something like this:
> 
> #!/bin/bash
> export VARIABLE_NAME
> python /the/python/script.py
> 
> You create a shell-script that exports the environment first, and then 
> invokes python.
> 
> Diez
> --

Alternatively, export the variable when you create it, in .bashrc or
wherever it is getting created.  That's probably the Right Thing to
Do(tm) in this case.

Cheers,
Cliff




More information about the Python-list mailing list