Python Nautilus script
Diez B. Roggisch
deets at nospam.web.de
Mon Sep 15 16:00:59 EDT 2008
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
More information about the Python-list
mailing list