Newbie thwarted by sys.path on Vista

Discussion in 'Python' started by Michael M Mason, Aug 1, 2009.

  1. I'm running Python 3.1 on Vista and I can't figure out how to add my own
    directory to sys.path.

    The docs suggest that I can either add it to the PYTHONPATH environment
    variable or to the PythonPath key in the registry. However, PYTHONPATH
    doesn't exist, and updating the registry key has no effect (and in any case
    the contents aren't the same as sys.path).

    So where does sys.path get its value from, and how do I change it?

    --
    Michael
     
    Michael M Mason, Aug 1, 2009
    #1
    1. Advertising

  2. Michael M Mason

    Jon Clements Guest

    On 1 Aug, 22:58, "Michael M Mason" <> wrote:
    > I'm running Python 3.1 on Vista and I can't figure out how to add my own
    > directory to  sys.path.
    >
    > The docs suggest that I can either add it to the PYTHONPATH environment
    > variable or to the PythonPath key in the registry.  However, PYTHONPATH
    > doesn't exist, and updating the registry key has no effect (and in any case
    > the contents aren't the same as sys.path).
    >
    > So where does sys.path get its value from, and how do I change it?
    >
    > --
    > Michael


    sys.path is just a list. So in your 'main' module where you do most of
    your imports, just append or prepend the path you desire (the search
    order is left to right). Although, I believe under Windows creating a
    system level or user level PYTHONPATH environment variable will enable
    Windows to pick it up. Not 100% sure as I don't have a Windows machine
    handy.
     
    Jon Clements, Aug 2, 2009
    #2
    1. Advertising

  3. >>>>> "Michael M Mason" <> (MMM) wrote:

    >MMM> I'm running Python 3.1 on Vista and I can't figure out how to add my own
    >MMM> directory to sys.path.


    >MMM> The docs suggest that I can either add it to the PYTHONPATH environment
    >MMM> variable or to the PythonPath key in the registry. However, PYTHONPATH
    >MMM> doesn't exist,


    Then create it.
    --
    Piet van Oostrum <>
    URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
    Private email:
     
    Piet van Oostrum, Aug 2, 2009
    #3
  4. Michael M Mason

    David Lyon Guest

    On Sat, 1 Aug 2009 22:58:53 +0100, "Michael M Mason"
    <> wrote:
    > I'm running Python 3.1 on Vista and I can't figure out how to add my own
    > directory to sys.path.
    >
    > The docs suggest that I can either add it to the PYTHONPATH environment
    > variable or to the PythonPath key in the registry. However, PYTHONPATH
    > doesn't exist, and updating the registry key has no effect
    >
    > So where does sys.path get its value from, and how do I change it?


    The simplest hack (worst - but most direct) is that sys.path is a list
    and you can use it like any other list. (add, delete, change items in it)

    It gets loaded from site.py (in the standardard library) at startup.

    Anything else you'll have to ask somebody else.

    David
     
    David Lyon, Aug 2, 2009
    #4
  5. Michael M Mason

    Dave Angel Guest

    Michael M Mason wrote:
    > <div class="moz-text-flowed" style="font-family: -moz-fixed">I'm
    > running Python 3.1 on Vista and I can't figure out how to add my own
    > directory to sys.path.
    >
    > The docs suggest that I can either add it to the PYTHONPATH
    > environment variable or to the PythonPath key in the registry.
    > However, PYTHONPATH doesn't exist, and updating the registry key has
    > no effect (and in any case the contents aren't the same as sys.path).
    >
    > So where does sys.path get its value from, and how do I change it?
    >

    sys.path gets its values from several places. The ones I think I know
    of are:

    current directory (which uses "" rather than the expected ".")
    directories listed in PythonPath environment variable
    Windows-system directory
    relative to the executable (python.exe or pythonw.exe) that's
    actually running
    relative to the user directory (docs&settings/username/Application
    Data ....

    If there's no PythonPath variable, it just uses those other items. I
    have no idea what it gets from the registry entries.

    Anyway, I'd suggest adding it to PythonPath, and if it's empty, just
    create it with the directory you need.

    I'm hoping you know you can also add to sys.path directly during script
    initialization. It's just a list, and is writeable.
     
    Dave Angel, Aug 2, 2009
    #5
  6. "Dave Angel" <> wrote in message
    news:...
    > Michael M Mason wrote:
    >> <div class="moz-text-flowed" style="font-family: -moz-fixed">I'm running
    >> Python 3.1 on Vista and I can't figure out how to add my own directory to
    >> sys.path.


    Thanks to Jon, Piet, David and Dave for the responses.

    > sys.path gets its values from several places.


    Ah, I'd misunderstood the docs: I thought it came from just one place (which
    I couldn't find).

    > Anyway, I'd suggest adding it to PythonPath, and if it's empty, just
    > create it with the directory you need.


    Thanks--that worked!

    > I'm hoping you know you can also add to sys.path directly during script
    > initialization. It's just a list, and is writeable.


    Yes, but I'm mainly playing in IDLE and I was getting a bit fed up of
    repeatedly typing
    import sys
    sys.path.append('C:/Users/Michael/Code/Python')
    import mystuff

    --
    Michael
     
    Michael M Mason, Aug 2, 2009
    #6
  7. Michael M Mason wrote:
    >
    > "Dave Angel" <> wrote in message
    > news:...
    >> Michael M Mason wrote:
    >>> <div class="moz-text-flowed" style="font-family: -moz-fixed">I'm
    >>> running Python 3.1 on Vista and I can't figure out how to add my own
    >>> directory to sys.path.

    >
    > Thanks to Jon, Piet, David and Dave for the responses.
    >
    >> sys.path gets its values from several places.

    >
    > Ah, I'd misunderstood the docs: I thought it came from just one place
    > (which I couldn't find).
    >
    >> Anyway, I'd suggest adding it to PythonPath, and if it's empty, just
    >> create it with the directory you need.

    >
    > Thanks--that worked!

    Be careful, I'm screwed things up on several occasions by placing a file
    on PYTHONPATH that overrides a file in the standard library, test.py
    being my favourite!
    >
    >> I'm hoping you know you can also add to sys.path directly during
    >> script initialization. It's just a list, and is writeable.

    >
    > Yes, but I'm mainly playing in IDLE and I was getting a bit fed up of
    > repeatedly typing
    > import sys
    > sys.path.append('C:/Users/Michael/Code/Python')
    > import mystuff
    >

    --
    Kindest regards.

    Mark Lawrence.
     
    Mark Lawrence, Aug 2, 2009
    #7
  8. "Mark Lawrence" <> wrote in message
    news:...
    > Be careful, I'm screwed things up on several occasions by placing a file
    > on PYTHONPATH that overrides a file in the standard library, test.py being
    > my favourite!


    Thanks. Sure enough, I've already got my own test.py but I hadn't
    discovered it was a problem yet...

    --
    Michael
     
    Michael M Mason, Aug 2, 2009
    #8
  9. Michael M Mason wrote:
    > "Mark Lawrence" <> wrote in message
    > news:...
    >> Be careful, I'm screwed things up on several occasions by placing a
    >> file on PYTHONPATH that overrides a file in the standard library,
    >> test.py being my favourite!

    >
    > Thanks. Sure enough, I've already got my own test.py but I hadn't
    > discovered it was a problem yet...
    >

    Typical, tried to reproduce it and can't! Still at least you've been
    warned.

    --
    Kindest regards.

    Mark Lawrence.
     
    Mark Lawrence, Aug 2, 2009
    #9
    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. Jeff Wagner

    sys.path.append('/my/new/path')

    Jeff Wagner, Nov 24, 2003, in forum: Python
    Replies:
    3
    Views:
    538
    Patrick Useldinger
    Nov 24, 2003
  2. Replies:
    3
    Views:
    407
  3. wen
    Replies:
    10
    Views:
    990
    Peter Hansen
    Aug 19, 2005
  4. Michael
    Replies:
    5
    Views:
    934
    Bjoern Schliessmann
    Nov 3, 2007
  5. RichardOnRails
    Replies:
    2
    Views:
    105
    RichardOnRails
    Nov 6, 2008
Loading...

Share This Page