0

from this How to access environment variable values?

i already tried

import os
print(os.environ['HOME'])

it's give me this error

> Traceback (most recent call last):
  File "d:/TOD/iot/paint/OStest.py", line 2, in <module>
    print(os.environ['HOME'])
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\os.py", line 675, in __getitem__
    raise KeyError(key) from None
KeyError: 'HOME'
PS D:\TOD\iot\paint>

FYI. for A few months ago i got a problem with my Enviroment variables. i think this cause a problem

  • 1
    View available keys available in your environment variables using `os.environ`. And error you got is because `os` module could not find `HOME` key in your environment variables. – Ghanteyyy Oct 12 '20 at 07:33
  • under windows, `print (os.environ['HOMEPATH'])` gives results here (and 'HOME' does not). – mansuetus Oct 12 '20 at 07:37
  • @mansuetus I am using windows too and `os.environ['HOME']` works fine. – Ghanteyyy Oct 12 '20 at 08:03
  • @Ghanteyyy os.environ is fine so i have to add HOME in my enviroment to fix this problem ? this work before something happend to my pc. my PATH also disappear after some error happend – Rungkrit Titachotnimit Oct 12 '20 at 08:33
  • @Ghanteyyy : weird. Windows 10 & Python 3.7 here, and HOME nor HOMEPATH seem defined in windows' "environmement variables" (note : french windows here, label might be wrong). – mansuetus Oct 12 '20 at 08:58
  • @mansuetus Now it's more weird. I too have python 3.7.8 and windows 10 and both `os.environ['Home']` and `os.environ['HOME']` works fine. – Ghanteyyy Oct 12 '20 at 10:31
  • 1
    @Ghanteyyy when you watch in the "advanced parameters -> environment variables" in system property, do you have "homepath" defined ? (I do not have *home* set but homepath IS defined in os.environ in python). Then some apps seem to want you to have HOME defined here, such as https://danlimerick.wordpress.com/2011/07/11/git-for-windows-tip-setting-home-and-the-startup-directory/ – mansuetus Oct 12 '20 at 13:44
  • 1
    @mansuetus I think other program have defined `HOME` in my machine. I doubt `git` have done it. – Ghanteyyy Oct 12 '20 at 14:08

0 Answers0