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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Amir Dekel

    #1

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

    Hi everyone,

    I have two problems:

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

    2. os.path.expandu ser("~") 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 "Environmen t Variables"
    in WinXP system properties, non helped. I really have to fix this somehow.

    Thanks,
    Amir
  • Lenard Lindstrom

    #2
    Re: Adding paths to sys.path permanently, and another problem...

    Amir Dekel <adekel@ort.org .il> writes:
    [color=blue]
    > Hi everyone,
    >
    > I have two problems:
    >
    > 1. How can I keep my changes in sys.path after closing the interpreter?
    >[/color]
    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
    <len-l@telus.net>

    Comment

    • Jean Brouwers

      #3
      Re: Adding paths to sys.path permanently, and another problem...


      Maybe, set environment variable PYTHONPATH. More details at

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

      /Jean Brouwers



      In article <fz26t3kj.fsf@t elus.net>, Lenard Lindstrom <len-1@telus.net>
      wrote:
      [color=blue]
      > Amir Dekel <adekel@ort.org .il> writes:
      >[color=green]
      > > Hi everyone,
      > >
      > > I have two problems:
      > >
      > > 1. How can I keep my changes in sys.path after closing the interpreter?
      > >[/color]
      > 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
      > <len-l@telus.net>[/color]

      Comment

      • Jeff Shannon

        #4
        Re: Adding paths to sys.path permanently, and another problem...

        Lenard Lindstrom wrote:
        [color=blue]
        >Amir Dekel <adekel@ort.org .il> writes:
        >
        >
        >[color=green]
        >>Hi everyone,
        >>
        >>I have two problems:
        >>
        >>1. How can I keep my changes in sys.path after closing the interpreter?
        >>
        >>
        >>[/color]
        >
        >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.
        >
        >[/color]

        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

        Comment

        • Amir Dekel

          #5
          Re: Adding paths to sys.path permanently, and another problem...

          Jeff Shannon wrote:
          [color=blue]
          >
          > 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.
          >[/color]

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

          Thanks,
          Amir

          Comment

          • Jeff Shannon

            #6
            Re: Adding paths to sys.path permanently, and another problem...

            Amir Dekel wrote:
            [color=blue]
            > 2. os.path.expandu ser("~") 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 "Environmen t Variables"
            > in WinXP system properties, non helped. I really have to fix this
            > somehow.[/color]


            Well, according to os.path.expandu ser()'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):
            [color=blue][color=green][color=darkred]
            >>> for key, val in os.environ.item s():[/color][/color][/color]
            .... print '%15s %s' % (key, val)
            ....
            TMP C:\DOCUME~1\Jef f\LOCALS~1\Temp
            USERNAME jeff
            COMPUTERNAME ######
            LOGONSERVER ######
            COMSPEC C:\WINNT\system 32\cmd.exe
            USERDOMAIN #######
            COMMONPROGRAMFI LES C:\Program Files\Common Files
            PROCESSOR_IDENT IFIER x86 Family #...
            PROGRAMFILES C:\Program Files
            PROCESSOR_REVIS ION 0806
            PATHEXT .COM;.EXE;.BAT; .CMD;#....
            SYSTEMROOT C:\WINNT
            PATH C:\Python22\.;C :\WINNT\system3 2;C:\WINNT;##.. ..
            APPDATA C:\Documents and Settings\Jeff\A pplication Data
            TEMP C:\DOCUME~1\Jef f\LOCALS~1\Temp
            HOMEDRIVE C:
            SYSTEMDRIVE C:
            PROCESSOR_ARCHI TECTURE x86
            NUMBER_OF_PROCE SSORS 1
            ALLUSERSPROFILE C:\Documents and Settings\All Users.WINNT
            PROCESSOR_LEVEL 6
            HOMEPATH \
            OS2LIBPATH C:\WINNT\system 32\os2\dll;
            USERPROFILE C:\Documents and Settings\Jeff
            OS Windows_NT
            WINDIR C:\WINNT[color=blue][color=green][color=darkred]
            >>>[/color][/color][/color]

            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

            Comment

            • Amir Dekel

              #7
              Re: Adding paths to sys.path permanently, and another problem...

              Jeff Shannon wrote:[color=blue]
              > Amir Dekel wrote:
              >[color=green]
              >> 2. os.path.expandu ser("~") 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 "Environmen t Variables"
              >> in WinXP system properties, non helped. I really have to fix this
              >> somehow.[/color]
              >
              >
              > 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...
              >[/color]

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

              Comment

              • Fernando Perez

                #8
                Re: Adding paths to sys.path permanently, and another problem...

                Amir Dekel wrote:
                [color=blue]
                > Hi everyone,
                >
                > I have two problems:
                >
                > 1. How can I keep my changes in sys.path after closing the interpreter?[/color]

                As others said, use $PYTHONPATH
                [color=blue]
                > 2. os.path.expandu ser("~") 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 "Environmen t Variables"
                > in WinXP system properties, non helped. I really have to fix this somehow.[/color]

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

                In [2]: psource IPython.genutil s.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\$HOM EPATH.

                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(wr eg.HKEY_CURRENT _USER,
                "Software\Micro soft\Windows\Cu rrentVersion\Ex plorer\Shell
                Folders")
                homedir = wreg.QueryValue Ex(key,'Persona l')[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,'s upport for your operating system not implemented.'

                HTH,

                f

                Comment

                • Brian van den Broek

                  #9
                  Re: Adding paths to sys.path permanently, and another problem...

                  Jeff Shannon said unto the world upon 2004-12-16 17:54:[color=blue]
                  > Amir Dekel wrote:
                  >[color=green]
                  >> 2. os.path.expandu ser("~") 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 "Environmen t Variables"
                  >> in WinXP system properties, non helped. I really have to fix this
                  >> somehow.[/color]
                  >
                  >
                  >
                  > Well, according to os.path.expandu ser()'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):[/color]

                  <SNIP>
                  [color=blue]
                  > 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[/color]

                  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:\L ist_Of_Dirs;D:\ Use_a_semi_colo n_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

                  Comment

                  • Amir Dekel

                    #10
                    Re: Adding paths to sys.path permanently, and another problem...

                    Jeff Shannon wrote:
                    [color=blue]
                    >
                    > 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...
                    >[/color]

                    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

                    Comment

                    Working...