473,320 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

environment variable issue

Hi,

Could someone explain me what I'm doing wrong here? I'm trying to
retrieve the value of an environment variable in Ubuntu 8.04 like this:
>>import os
os.environ['USER']
'michel'

This works but this doesn't:
>>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'

Why is it working in the first case but not in the second one. I must be
missing something but it seems I'm not able to figure it out.

Thanks,
Michel

--
Michel Leunen
http://linux.leunen.com
Sep 15 '08 #1
9 6396
Michel Leunen wrote:
Could someone explain me what I'm doing wrong here? I'm trying to
retrieve the value of an environment variable in Ubuntu 8.04 like this:
>>import os
>>os.environ['USER']
'michel'

This works but this doesn't:
>>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'

Why is it working in the first case but not in the second one. I must be
missing something but it seems I'm not able to figure it out.
this might help:

http://mail.python.org/pipermail/pyt...er/507933.html

(scanning the subjects of recent and ongoing threads before posting is
always a good idea).

</F>

Sep 15 '08 #2
Michel Leunen schrieb:
Hi,

Could someone explain me what I'm doing wrong here? I'm trying to
retrieve the value of an environment variable in Ubuntu 8.04 like this:
>>import os
>>os.environ['USER']
'michel'

This works but this doesn't:
>>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'

Why is it working in the first case but not in the second one. I must be
missing something but it seems I'm not able to figure it out.
Maybe hostname is just missing? What does os.environ.keys() yield?

On *my* ubuntu, it gives
>>sorted(os.environ.keys())
['HOME', 'JAVA_HOME', 'LANG', 'LANGUAGE', 'LOGNAME', 'MAIL', 'PATH',
'PWD', 'PYTHONSTARTUP', 'SHELL', 'SHLVL', 'SSH_CLIENT',
'SSH_CONNECTION', 'SSH_TTY', 'TERM', 'USER', '_']
>>>
No HOSTNAME ...
Diez
Sep 15 '08 #3
Fredrik Lundh a écrit :
this might help:

http://mail.python.org/pipermail/pyt...er/507933.html
Yes, it helps. Thanks
>
(scanning the subjects of recent and ongoing threads before posting is
always a good idea).
I did search the web but didn't find anything related to the problem I
had. I didn't think searching the newsgroups, though.

Thanks for your help,
Michel

--
Michel Leunen
http://linux.leunen.com
Sep 15 '08 #4
Diez B. Roggisch a écrit :
>>sorted(os.environ.keys())
['HOME', 'JAVA_HOME', 'LANG', 'LANGUAGE', 'LOGNAME', 'MAIL', 'PATH',
'PWD', 'PYTHONSTARTUP', 'SHELL', 'SHLVL', 'SSH_CLIENT',
'SSH_CONNECTION', 'SSH_TTY', 'TERM', 'USER', '_']
>>>

No HOSTNAME ...
Yes, no HOSTNAME. That's exactly the problem I have. Why is there no
HOSTNAME?
Thanks to Fredrik Lundh, I know why, now.

Thanks,
Michel

--
Michel Leunen
http://linux.leunen.com
Sep 15 '08 #5
Could someone explain me what I'm doing wrong here? I'm trying to
retrieve the value of an environment variable in Ubuntu 8.04 like this:
>>import os
>>os.environ['USER']
'michel'

This works but this doesn't:
>>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'

Why is it working in the first case but not in the second one. I must be
missing something but it seems I'm not able to figure it out.
This is likely the same issue I just posted about here:

http://groups.google.com/group/comp....346439533bd23a

Your HOSTNAME variable hasn't been exported to subshells. You
can check this by looking at the output of

bash$ export -p

which will print all the variables that are exported. I suspect
you'll see $USER in the list, but not $HOSTNAME

However, for the hostname, you might be interested in
>>import platform as p
p.uname()[1]
'rubbish'

which does this in a cross-platform way without needing to fuss
with exporting environment variables.

-tkc


Sep 15 '08 #6
Tim Chase a écrit :
Your HOSTNAME variable hasn't been exported to subshells. You can check
this by looking at the output of

bash$ export -p

which will print all the variables that are exported. I suspect you'll
see $USER in the list, but not $HOSTNAME
Actually HOSTNAME seems to be exported as well:

$ export -p | grep HOSTNAME
declare -x HOSTNAME="LinuxPC"

I don't know what to think of this but it's the beginning of an explanation.

Thanks,
Michel

--
Michel Leunen
http://linux.leunen.com
Sep 15 '08 #7
Michel Leunen schrieb:
Tim Chase a écrit :
>Your HOSTNAME variable hasn't been exported to subshells. You can
check this by looking at the output of

bash$ export -p

which will print all the variables that are exported. I suspect
you'll see $USER in the list, but not $HOSTNAME

Actually HOSTNAME seems to be exported as well:

$ export -p | grep HOSTNAME
declare -x HOSTNAME="LinuxPC"

I don't know what to think of this but it's the beginning of an
explanation.
Not on my machine, and *if* it is exported, python gets it.

deets@absinth:~$ export -p | grep HOSTNAME
deets@absinth:~$ export HOSTNAME
deets@absinth:~$ export -p | grep HOSTNAME
declare -x HOSTNAME="absinth"
deets@absinth:~$ python -c "import os; print os.environ.get('HOSTNAME')"
absinth
deets@absinth:~$
Diez
Sep 15 '08 #8
Michel Leunen a écrit :
Actually HOSTNAME seems to be exported as well:

$ export -p | grep HOSTNAME
declare -x HOSTNAME="LinuxPC"

I don't know what to think of this but it's the beginning of an
explanation.
Oops, my mystake, I forgot I made an export HOSTNAME before trying the
export -p command.
Sorry, you're right, HOSTNAME is NOT in the list of exported variable.

--
Michel Leunen
http://linux.leunen.com
Sep 15 '08 #9
On Sep 16, 5:09 am, Tim Chase <python.l...@tim.thechases.comwrote:
This is likely the same issue I just posted about here:

http://groups.google.com/group/comp....thread/thread/...
[snip]

It's also a bit like a thread I posted to back in August (which dealt
with the difference between bash's shell-variable 'HOSTNAME' and
environment-variables).
http://groups.google.com/group/comp....038742c95cc28a
Sep 16 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: sebastien.hugues | last post by:
Hi I would like to retrieve the application data directory path of the logged user on windows XP. To achieve this goal i use the environment variable APPDATA. The logged user has this name:...
28
by: Christian | last post by:
Another question from a not even newbie: In Unix you can set an environment variable with the command export PYTHONPATH but I would like to set the variable from at .py script. So my question...
13
by: Jimmy Cracker | last post by:
Is it completely impossible in UNIX to push an environment variable to the parent shell? I would like to do something like this: main(int argc, char *argv) { char *var; var = (char...
4
by: | last post by:
Hi all, I am trying to append a certain string to the PATH environment variable programmatically. I am able to read what is in the variable using the System.Environment method...
15
by: Rob Nicholson | last post by:
A consequence of the ASP.NET architecture on IIS has just hit home with a big thud. It's to do with shared variables. Consider a module like this: Public Module Functions Public GlobalName As...
6
by: yaron | last post by:
Hi, my application use environment variable, let call it FOO. how can i add the FOO environment variable to my project or my solution, so the line string foo =...
0
by: Joe HM | last post by:
Hello - I am putting together a little ConsoleApplication that is supposed to check for an Environment Variable and create it if it does not exist. I found some code that will add a new...
5
by: Henaro | last post by:
Hello~ I am having trouble setting environment variables in C++ on win32. The code that is not working is: char prxy; char pf_cmd1 = "set http_proxy="; ....
3
by: Parthiban s | last post by:
Recently I migrated a MS Access app from mdb data tables to oracle db. It all works fine when connected locally to server but errors out in citrix for only two screens with below error. unable to...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.