Adding paths to sys.path permanently, and another problem...

Jeff Shannon jeff at ccvcorp.com
Thu Dec 16 17:54:44 EST 2004


Amir Dekel wrote:

> 2. os.path.expanduser("~") always gives me "C:\\" instead of my 
> homepath. I have tried to change my homepath in WinXP using set 
> homepath(in command line), and alsaw using the "Environment Variables" 
> in WinXP system properties, non helped. I really have to fix this 
> somehow.


Well, according to os.path.expanduser()'s docstring, it uses the $HOME 
environment variable to determine how to expand ~.  I don't know what's 
standard on Windows, but I tried checking for a $HOME and found none.  
Here's (a slightly redacted copy of) what I *do* find (Win2K):

 >>> for key, val in os.environ.items():
...     print '%15s   %s' % (key, val)
...    
            TMP   C:\DOCUME~1\Jeff\LOCALS~1\Temp
       USERNAME   jeff
   COMPUTERNAME   ######
    LOGONSERVER   ######
        COMSPEC   C:\WINNT\system32\cmd.exe
     USERDOMAIN   #######
COMMONPROGRAMFILES   C:\Program Files\Common Files
PROCESSOR_IDENTIFIER   x86 Family #...
   PROGRAMFILES   C:\Program Files
PROCESSOR_REVISION   0806
        PATHEXT   .COM;.EXE;.BAT;.CMD;#....
     SYSTEMROOT   C:\WINNT
           PATH   C:\Python22\.;C:\WINNT\system32;C:\WINNT;##....
        APPDATA   C:\Documents and Settings\Jeff\Application Data
           TEMP   C:\DOCUME~1\Jeff\LOCALS~1\Temp
      HOMEDRIVE   C:
    SYSTEMDRIVE   C:
PROCESSOR_ARCHITECTURE   x86
NUMBER_OF_PROCESSORS   1
ALLUSERSPROFILE   C:\Documents and Settings\All Users.WINNT
PROCESSOR_LEVEL   6
       HOMEPATH   \
     OS2LIBPATH   C:\WINNT\system32\os2\dll;
    USERPROFILE   C:\Documents and Settings\Jeff
             OS   Windows_NT
         WINDIR   C:\WINNT
 >>>

Judging from this, I think that os.environ['USERPROFILE'] seems like it 
may do what you want, though os.environ['APPDATA'] might be useful as 
well.  Of course, if you're trying to get something to work 
cross-platform, things may be more difficult -- but that's because 
Windows doesn't normally use ~ so its use is not supported very well.  
You may be able to create a $HOME that's equivalent to $USERPROFILE...

Jeff Shannon
Technician/Programmer
Credit International




More information about the Python-list mailing list