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

Fernando Perez fperez528 at yahoo.com
Thu Dec 16 19:35:22 EST 2004


Amir Dekel wrote:

> Hi everyone,
> 
> I have two problems:
> 
> 1. How can I keep my changes in sys.path after closing the interpreter?

As others said, use $PYTHONPATH

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

This is what ipython uses to try and get that information in a portable manner. 
Note that it uses _winreg, which is part of the win32 extensions.

In [1]: import IPython.genutils

In [2]: psource IPython.genutils.get_home_dir
def get_home_dir():
    """Return the closest possible equivalent to a 'home' directory.

    For Posix systems, this is $HOME, and on NT it's $HOMEDRIVE\$HOMEPATH.

    Currently only Posix and NT are implemented, a HomeDirError exception is
    raised for all other OSes. """ #'

    if os.name == 'posix':
        return os.environ['HOME']
    elif os.name == 'nt':
        # For some strange reason, win9x returns 'nt' for os.name.
        try:
            return os.path.join(os.environ['HOMEDRIVE'],os.environ['HOMEPATH'])
        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.'

HTH,

f




More information about the Python-list mailing list