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

Fernando Perez fperez528 at
Fri Dec 17 01:35:22 CET 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 == 'posix':
        return os.environ['HOME']
    elif == 'nt':
        # For some strange reason, win9x returns 'nt' for
            return os.path.join(os.environ['HOMEDRIVE'],os.environ['HOMEPATH'])
                # Use the registry to get the 'My Documents' folder.
                import _winreg as wreg
                key = wreg.OpenKey(wreg.HKEY_CURRENT_USER,
                homedir = wreg.QueryValueEx(key,'Personal')[0]
                return homedir
                return 'C:\\'
    elif == 'dos':
        # Desperate, may do absurd things in classic MacOS. May work under DOS.
        return 'C:\\'
        raise HomeDirError,'support for your operating system not implemented.'



More information about the Python-list mailing list