[IPython-user]: IPython install under Win2000
Fernando Perez
Fernando.Perez at colorado.edu
Mon Jul 18 15:52:11 CDT 2005
Jerry McRae wrote:
> I agree that USERPROFILE would make more sense. That would allow
> people to designate a different starting folder for running cmd.exe.
> Of course I rarely do this, since I have a shortcut that runs IPython
> every time I want to access DOS commands. (:-)
OK, after reading all the responses, here's the solution I've implemented:
def get_home_dir():
"""Return the closest possible equivalent to a 'home' directory.
We first try $HOME. Absent that, on NT it's $HOMEDRIVE\$HOMEPATH.
Currently only Posix and NT are implemented, a HomeDirError exception is
raised for all other OSes. """
isdir = os.path.isdir
env = os.environ
try:
homedir = env['HOME']
if not isdir(homedir):
# in case a user stuck some string which does NOT resolve to a
# valid path, it's as good as if we hadn't foud it
raise KeyError
return homedir
except KeyError:
if os.name == 'posix':
raise HomeDirError,'undefined $HOME, IPython can not proceed.'
elif os.name == 'nt':
# For some strange reason, win9x returns 'nt' for os.name.
try:
homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH'])
if not isdir(homedir):
homedir = os.path.join(env['USERPROFILE'])
if not isdir(homedir):
raise HomeDirError
return homedir
except:
try:
# Use the registry to get the 'My Documents' folder.
import _winreg as wreg
key = wreg.OpenKey(wreg.HKEY_CURRENT_USER,
"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")
homedir = wreg.QueryValueEx(key,'Personal')[0]
key.Close()
return homedir
except:
return 'C:\\'
elif os.name == 'dos':
# Desperate, may do absurd things in classic MacOS. May work
under DOS.
return 'C:\\'
else:
raise HomeDirError,'support for your operating system not
implemented.'
The important change is taken from the pythoncard/python-list suggestion, to
do an isdir() check first, before using any variable. This will at least
catch broken configs like $HOME=='%SOMETHING%' [1]. I am NOT going to go
hunting a recursive chain of expansions in environment variables, neither am I
willing to use Alan's one-off test, which is too much of a special hack.
I'm keeping HOMEDRIVE/HOMEPATH first in the priority list, because I don't
want the behavior to change for existing users. It would be an unpleasant
surprise if an ipython update were to spring up a new config somewhere else
out of the blue.
Unless someone complains soon, this will go into SVN later today.
And many thanks to all for the feedback on this issue.
Best,
f
[1] For the record, yes: I consider an environment variable which expands to
an unevaluated macro a broken config, period.
More information about the IPython-user
mailing list