Python os.environ[“HOME”] works on idle but not in a script

前端 未结 3 1835
甜味超标
甜味超标 2021-02-07 08:18

I am working on a simple Python (2.7.3) script, but I have to get the user\'s Home Folder. I tried:

import os
home_folder = os.environ[\"HOME\"]
<
3条回答
  •  你的背包
    2021-02-07 08:43

    Windows has no HOME environment variable. It uses USERPROFILE instead. To solve the problem you can define a new variable "HOME" typing on your console:

    set HOME=%USERPROFILE%
    

    as a copy of the USERPROFILE variable.

    You can check that they are identical typing:

    echo %USERPROFILE%
    echo %HOME%
    

    It will work for the current session.

提交回复
热议问题