Automatically creating a HOME environ variable on Windows?

Discussion in 'Python' started by jim.eggleston@gmail.com, Oct 29, 2005.

  1. Guest

    Windows doesn't have a HOME environment variable, but it does have
    HOMEDRIVE and HOMEPATH. Could Windows versions of Python automatically
    populate os.environ with HOME, where HOME =
    os.path.join(os.environ['HOMEDRIVE'], os.environ['HOMEPATH'])?

    If this was done, then modules such as pdb, which load resource files
    from HOME, would work under Windows.

    Alternatively, here is a patch to make pdb.py read .pdbrc under
    Windows.

    *** pdb_orig.py Mon Jun 16 01:26:30 2003
    --- pdb.py Sat Oct 29 11:11:07 2005
    ***************
    *** 65,72 ****
    --- 65,76 ----

    # Read $HOME/.pdbrc and ./.pdbrc
    self.rcLines = []
    + envHome = ''
    if 'HOME' in os.environ:
    envHome = os.environ['HOME']
    + elif 'HOMEDRIVE' in os.environ and 'HOMEPATH' in os.environ:
    + envHome = os.path.join(os.environ['HOMEDRIVE'],
    os.environ['HOMEPATH'])
    + if envHome:
    try:
    rcFile = open(os.path.join(envHome, ".pdbrc"))
    except IOError:
     
    , Oct 29, 2005
    #1
    1. Advertising

  2. Jarek Zgoda Guest

    napisa³(a):

    > Windows doesn't have a HOME environment variable, but it does have
    > HOMEDRIVE and HOMEPATH. Could Windows versions of Python automatically
    > populate os.environ with HOME, where HOME =
    > os.path.join(os.environ['HOMEDRIVE'], os.environ['HOMEPATH'])?


    MS recommends using %USERPROFILE%, as the above in many cases returns
    "C:\", which is wrong.

    --
    Jarek Zgoda
    http://jpa.berlios.de/
     
    Jarek Zgoda, Oct 29, 2005
    #2
    1. Advertising

  3. Guest

    Cool, even better. So what's best, having code to add HOME
    (=USERPROFILE) to os.environ, or change the various places that HOME is
    used to check for USERPROFILE?
     
    , Oct 29, 2005
    #3
  4. wrote:

    > Cool, even better. So what's best, having code to add HOME
    > (=USERPROFILE) to os.environ, or change the various places that HOME is
    > used to check for USERPROFILE?


    Best solution would be to have portable function that returns
    user home directory and knows about all platfom quirks.

    --
    Maciej "Fiedzia" Dziardziel (fiedzia (at) fiedzia (dot) prv (dot) pl)
    www.fiedzia.prv.pl

    If you lost your left arm, your right arm would be left.
     
    Maciej Dziardziel, Oct 29, 2005
    #4
  5. Jorgen Grahn Guest

    On Sat, 29 Oct 2005 18:43:44 +0200, Maciej Dziardziel <> wrote:
    > wrote:
    >
    >> Cool, even better. So what's best, having code to add HOME
    >> (=USERPROFILE) to os.environ, or change the various places that HOME is
    >> used to check for USERPROFILE?

    >
    > Best solution would be to have portable function that returns
    > user home directory and knows about all platfom quirks.


    Why is that better than Python creating a HOME in os.environ, if it doesn't
    already exist? I can think of a few reasons it's better, and a few reasons
    it's worse.

    /Jorgen

    --
    // Jorgen Grahn <jgrahn@ Ph'nglui mglw'nafh Cthulhu
    \X/ algonet.se> R'lyeh wgah'nagl fhtagn!
     
    Jorgen Grahn, Oct 31, 2005
    #5
  6. Jorgen Grahn wrote:

    >> Best solution would be to have portable function that returns
    >> user home directory and knows about all platfom quirks.

    >
    > Why is that better than Python creating a HOME in os.environ, if it
    > doesn't
    > already exist? I can think of a few reasons it's better, and a few
    > reasons it's worse.


    First, it is possible that HOME viariable already exists and has different
    meaning, (if python is used as embedded scripting language it can be
    defined by application), Second, there is a group of path related functions
    in os.path (or ntpath), including expanduser, and its better to use
    function than relay on some interpreter behaviour that may be different on
    jython or ironpython.

    --
    Maciej "Fiedzia" Dziardziel (fiedzia (at) fiedzia (dot) prv (dot) pl)
    www.fiedzia.prv.pl

    How come in Scooby Doo Fred and Daphne were always on the same team and
    Velma, Scooby and Shaggy were always on the same team? Doesn't seem quite
    right now that you think about it, does it?
     
    Maciej Dziardziel, Oct 31, 2005
    #6
  7. Jorgen Grahn Guest

    On Mon, 31 Oct 2005 12:26:30 +0100, Maciej Dziardziel <> wrote:
    > Jorgen Grahn wrote:
    >
    >>> Best solution would be to have portable function that returns
    >>> user home directory and knows about all platfom quirks.

    >>
    >> Why is that better than Python creating a HOME in os.environ, if it
    >> doesn't
    >> already exist? I can think of a few reasons it's better, and a few
    >> reasons it's worse.

    >
    > First, it is possible that HOME viariable already exists and has different
    > meaning, (if python is used as embedded scripting language it can be
    > defined by application),


    And on systems where $HOME exists, that's what this new abstraction would
    have to use internally anyway, so us Unix users won't lose anything. OK, I
    accept the reasoning.

    /Jorgen

    --
    // Jorgen Grahn <jgrahn@ Ph'nglui mglw'nafh Cthulhu
    \X/ algonet.se> R'lyeh wgah'nagl fhtagn!
     
    Jorgen Grahn, Oct 31, 2005
    #7
  8. Guest

    Having a function is definitely cleaner. Creating a HOME environment
    variable where one does not exist in the calling shell is misleading.

    There are 10 modules in the python 2.3 lib directory that contain
    os.environ['HOME']:

    lib\ftplib.py
    lib\mailbox.py
    lib\mailcap.py
    lib\netrc.py
    lib\ntpath.py
    lib\os2emxpath.py
    lib\pdb.py
    lib\posixpath.py
    lib\rfc822.py
    lib\user.py

    It's probably not a huge effort to change these (but that's easy for me
    to say ...) It would be nice to start of with having a standard way to
    find out what the home directory is.
     
    , Nov 1, 2005
    #8
  9. Ben Sizer Guest

    > napisa³(a):
    >
    > MS recommends using %USERPROFILE%, as the above in many cases returns
    > "C:\", which is wrong.


    I'm guessing this is why IDLE creates a directory in the root of my
    Win98 system whenever I use it. It would be great if this could be
    fixed for the next version.

    --
    Ben Sizer.
     
    Ben Sizer, Nov 1, 2005
    #9
  10. Maciej Dziardziel wrote:
    > > ...there is a group of path related functions in os.path (or
    > > ntpath), including expanduser, and its better to use function...


    On Nov 01, wrote:
    > Having a function is definitely cleaner. Creating a HOME environment
    > variable where one does not exist in the calling shell is
    > misleading.
    > ...
    > It would be nice to start of with having a standard way to find out
    > what the home directory is.


    I think that is what Maciej has already pointed out.

    Just to clarify then:

    os.path.expanduser('~') is the universal/portable means to find a
    user's home directory, regardless of platform. So use of HOME or
    USERPROFILE or whatever in scripts should be discouraged.

    Someone please correct me if the above is wrong. I haven't tried on a
    mac, but linux and windows seem to behave well; i.e., linux looks for
    HOME, and windows appears to combine HOMEDRIVE and HOMEPATH if HOME is
    not set. Details are in the 2.4.2 sources' "Python24/Lib/posixpath.py"
    if you're curious.

    --
    _ _ ___
    |V|icah |- lliott http://micah.elliott.name
    " " """
     
    Micah Elliott, Nov 1, 2005
    #10
  11. Guest

    Does Windows 98 have a %USERPROFILE% environment variable?
     
    , Nov 1, 2005
    #11
  12. Guest

    os.path.expanduser('~') is a bit cryptic for non-unix people.
    os.path.gethome() or something like that would be nicer. expanduser()
    should then call gethome() so the logic is in one place.

    It looks like the existing logic in expanduser() is out of date anyway.
    It should be updated to use %USERPROFILE% before %HOMEDRIVE% +
    %HOMEPATH% .
     
    , Nov 1, 2005
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Erick Bodine
    Replies:
    1
    Views:
    454
    Matt Gerrans
    Dec 16, 2003
  2. Replies:
    9
    Views:
    726
    Peter Otten
    Feb 27, 2007
  3. nancyflorida
    Replies:
    0
    Views:
    352
    nancyflorida
    Nov 12, 2007
  4. nancyflorida
    Replies:
    0
    Views:
    330
    nancyflorida
    Nov 12, 2007
  5. gert
    Replies:
    0
    Views:
    473
Loading...

Share This Page