Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Automatically creating a HOME environ variable on Windows?

Reply
Thread Tools

Automatically creating a HOME environ variable on Windows?

 
 
jim.eggleston@gmail.com
Guest
Posts: n/a
 
      10-29-2005
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:

 
Reply With Quote
 
 
 
 
Jarek Zgoda
Guest
Posts: n/a
 
      10-29-2005
http://www.velocityreviews.com/forums/(E-Mail 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.

--
Jarek Zgoda
http://jpa.berlios.de/
 
Reply With Quote
 
 
 
 
jim.eggleston@gmail.com
Guest
Posts: n/a
 
      10-29-2005
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?

 
Reply With Quote
 
Maciej Dziardziel
Guest
Posts: n/a
 
      10-29-2005
(E-Mail Removed) 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.

 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      10-31-2005
On Sat, 29 Oct 2005 18:43:44 +0200, Maciej Dziardziel <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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!
 
Reply With Quote
 
Maciej Dziardziel
Guest
Posts: n/a
 
      10-31-2005
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?
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      10-31-2005
On Mon, 31 Oct 2005 12:26:30 +0100, Maciej Dziardziel <(E-Mail Removed)> 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!
 
Reply With Quote
 
jim.eggleston@gmail.com
Guest
Posts: n/a
 
      11-01-2005
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.

 
Reply With Quote
 
Ben Sizer
Guest
Posts: n/a
 
      11-01-2005
> (E-Mail 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.

--
Ben Sizer.

 
Reply With Quote
 
Micah Elliott
Guest
Posts: n/a
 
      11-01-2005
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, (E-Mail Removed) 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 (E-Mail Removed)
" " """
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bin = FieldStorage(fp=environ['wsgi.input'], environ=environ) gert Python 0 01-18-2009 01:37 PM
Interactive os.environ vs. os.environ in script boris.smirnov@gmail.com Python 9 02-27-2007 10:05 AM
using fstream objects in a multiuser environ. Pablo C++ 4 02-25-2005 02:50 PM
env variable set using _winreg.SetValueEx() doesn't show w/ os.environ Erick Bodine Python 1 12-16-2003 09:16 PM
os.environ and os.path.chdir Yun Mao Python 4 09-20-2003 08:07 PM



Advertisments