Automatically creating a HOME environ variable on Windows?

J

jim.eggleston

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:
 
J

Jarek Zgoda

(e-mail address removed) 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.
 
J

jim.eggleston

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?
 
M

Maciej Dziardziel

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.
 
J

Jorgen Grahn

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
 
M

Maciej Dziardziel

Jorgen said:
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?
 
J

Jorgen Grahn

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
 
J

jim.eggleston

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.
 
B

Ben Sizer

(e-mail address removed) 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.
 
M

Micah Elliott

Maciej said:
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.
 
J

jim.eggleston

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% .
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top