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

Discussion in 'Python' started by Amir Dekel, Dec 16, 2004.

  1. Amir Dekel

    Amir Dekel Guest

    Hi everyone,

    I have two problems:

    1. How can I keep my changes in sys.path after closing the interpreter?

    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.

    Thanks,
    Amir
    Amir Dekel, Dec 16, 2004
    #1
    1. Advertising

  2. Amir Dekel <> writes:

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

    Just how flexible does this need to be? sys.path can easily be altered
    at startup from within a sitecustomize module (section 3.28 -- site --
    Python Runtime Services -- Python Library Reference) or a module specified
    by the PYTHONSTARTUP environment variable (section 3.29 -- user -- Python
    Runtime Services). Just have one of these modules append to sys.path any
    additional paths you need.

    Saving sys.path changes between interpreter runs would be more involved.
    On Windows Python loads the initial module search path from the registry.
    Python's registry entries are made during installation and are left alone
    afterwards. Changing these entries is probably not a good idea. But
    if you need sys.path persistence I can try writing an example that does
    it automatically using the atexit module and a history file.

    Lenard Lindstrom
    <>
    Lenard Lindstrom, Dec 16, 2004
    #2
    1. Advertising

  3. Maybe, set environment variable PYTHONPATH. More details at

    <http://www.python.org/doc/current/tut/node8.html>

    /Jean Brouwers



    In article <>, Lenard Lindstrom <>
    wrote:

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

    > Just how flexible does this need to be? sys.path can easily be altered
    > at startup from within a sitecustomize module (section 3.28 -- site --
    > Python Runtime Services -- Python Library Reference) or a module specified
    > by the PYTHONSTARTUP environment variable (section 3.29 -- user -- Python
    > Runtime Services). Just have one of these modules append to sys.path any
    > additional paths you need.
    >
    > Saving sys.path changes between interpreter runs would be more involved.
    > On Windows Python loads the initial module search path from the registry.
    > Python's registry entries are made during installation and are left alone
    > afterwards. Changing these entries is probably not a good idea. But
    > if you need sys.path persistence I can try writing an example that does
    > it automatically using the atexit module and a history file.
    >
    > Lenard Lindstrom
    > <>
    Jean Brouwers, Dec 16, 2004
    #3
  4. Amir Dekel

    Jeff Shannon Guest

    Lenard Lindstrom wrote:

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

    >
    >Saving sys.path changes between interpreter runs would be more involved.
    >On Windows Python loads the initial module search path from the registry.
    >Python's registry entries are made during installation and are left alone
    >afterwards. Changing these entries is probably not a good idea. But
    >if you need sys.path persistence I can try writing an example that does
    >it automatically using the atexit module and a history file.
    >
    >


    Not only can one modify the environment variable PYTHONPATH, but one can
    also use a .pth file. Under windows, when the interpreter starts up it
    will search its default sys.path for any files with extension .pth; if
    it finds any, then it will use each line of those files as a directory
    to add to sys.path. Thus, if you drop a mymodule.pth file in
    site-packages, which contains a list of the directories you're
    interested in, sys.path will automatically be amended for you every time
    Python starts.

    Jeff Shannon
    Technician/Programmer
    Credit International
    Jeff Shannon, Dec 16, 2004
    #4
  5. Amir Dekel

    Amir Dekel Guest

    Jeff Shannon wrote:

    >
    > Not only can one modify the environment variable PYTHONPATH, but one can
    > also use a .pth file. Under windows, when the interpreter starts up it
    > will search its default sys.path for any files with extension .pth; if
    > it finds any, then it will use each line of those files as a directory
    > to add to sys.path. Thus, if you drop a mymodule.pth file in
    > site-packages, which contains a list of the directories you're
    > interested in, sys.path will automatically be amended for you every time
    > Python starts.
    >


    Works great! So simple, yet people get so confused...
    Do you have a solution for my other problem?

    Thanks,
    Amir
    Amir Dekel, Dec 16, 2004
    #5
  6. Amir Dekel

    Jeff Shannon Guest

    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
    Jeff Shannon, Dec 16, 2004
    #6
  7. Amir Dekel

    Amir Dekel Guest

    Jeff Shannon wrote:
    > 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.

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


    Yet another reason not to use Windows and all it's silly system...
    Unfotunely, I am stuck with a USB ADSL modem on a family computer, which
    led to terrible headaches with my (quiet successful, actually) attempts
    to install Linux.

    Well, so be it...
    Amir Dekel, Dec 16, 2004
    #7
  8. 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
    Fernando Perez, Dec 17, 2004
    #8
  9. Jeff Shannon said unto the world upon 2004-12-16 17:54:
    > 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):


    <SNIP>

    > 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


    Hi all,

    some 'nix-world tool (probably xemacs) that I put on my WinMe box didn't
    like the lack of a HOME environment variable, either. This was easily
    solved by adding the line

    SET HOME=D:\HOME

    to my autoexec.bat file. (I don't know if this works in more recent
    versions of Windows, but I'd imagine so.) There to, I have added a line

    SET PYTHONPATH=D:\List_Of_Dirs;D:\Use_a_semi_colon_to_sep_items_on_Win

    Which puts those dirs into sys.path (at the front, I believe). I also
    used the .pth file trick because they two means differ as to where in
    the sys.path they add their entries.

    Best to all,

    Brian vdB
    Brian van den Broek, Dec 17, 2004
    #9
  10. Amir Dekel

    Amir Dekel Guest

    Jeff Shannon wrote:

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


    Both problems solved! The .pth seems like the best solution for the
    first one, and for the expanduser("~") I just added a $HOME variable to
    the Environment variables of WinXP. Simple solutions. Thanks Jeff

    Amir
    Amir Dekel, Dec 17, 2004
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ankit Aneja

    Problem with adding controls permanently

    Ankit Aneja, Oct 17, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    409
    Steven Cheng[MSFT]
    Oct 18, 2005
  2. Greg McIntyre
    Replies:
    0
    Views:
    421
    Greg McIntyre
    Sep 12, 2005
  3. Frank
    Replies:
    1
    Views:
    268
    Steve Holden
    Feb 17, 2007
  4. cnb
    Replies:
    11
    Views:
    950
    Fredrik Lundh
    Sep 16, 2008
  5. Chris Colbert
    Replies:
    2
    Views:
    1,158
    Chris Colbert
    Aug 30, 2009
Loading...

Share This Page