where is sys.path initialized?

Discussion in 'Python' started by Alex, Sep 9, 2005.

  1. Alex

    Alex Guest

    Rossum's tutorial states
    "Actually, modules are searched in the list of directories given by the
    variable sys.path which is initialized from the directory containing
    the input script (or the current directory)"

    I'm running Python standard installation (ver 2.4) on Windows XP. Where
    can I find the "input script" where the sys.path is defined?

    Alex
     
    Alex, Sep 9, 2005
    #1
    1. Advertisements

  2. Alex

    Alex Guest

    No one is answering this question. Why, is it a bit unclear or what?
    All I wanted to do was to add the path C:\Python24\mypython to the
    sys.path so that I could put all my stuff there instead of mixing it
    with the stuff that comes with the installation. And I wanted to do it
    permanently som that I wouldn't have to use sys.path.append every time
    I started Python interpreter.

    Here is what I did, and it worked, but I still don't know why. If
    somebody has an explanation please tell me.

    1. My computer -> Properties->Advanced->Environment variables->System
    variables ->New

    2. In Variable name field enter PYTHONPATH and in Variable value field
    enter C:\Python24\mypython

    3. OK, OK, OK to close all the dialog boxes.

    4. Restart the computer (it's essential to restart the computer and not
    just the Python shell).

    5. Open the Python shell and enterIf you've done exactly as I've described here you should see
    C:\Python24\mypython in the list.

    Alex
     
    Alex, Sep 9, 2005
    #2
    1. Advertisements

  3. Alex

    Robert Kern Guest

    Have patience. No one's getting paid to answer your questions.

    http://docs.python.org/tut/node8.html#SECTION008110000000000000000

    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
     
    Robert Kern, Sep 9, 2005
    #3
  4. Alex

    Peter Hansen Guest

    No, but if you really expected a response in only four hours you need a
    reality check. Many people don't receive Usenet articles for a day or
    two, possibly longer, and in any case many people don't *check* their
    news more often than once every day or two.

    As Robert suggests, have patience. Generally one should wait several
    days before giving up on a reply. Reposting is generally less than
    useless (basically it's rude), though reposting with more detail or
    another way of describing your problem is perfectly acceptable. After a
    day or two...

    -Peter
     
    Peter Hansen, Sep 10, 2005
    #4
  5. Alex

    Steve M Guest

    The PYTHONPATH environment variable is good for that. For general
    customizing beyond the path, you can make a file called:

    C:\Python24\Lib\site-packages\sitecustomze.py

    and it will be executed every time python runs. It might look like
    this:

    import sys
    sys.path.insert(0, r'C:\Python24\mypython')
    sys.setdefaultencoding('latin-1)



    Where the tutorial says the sys.path is "initialized from the directory
    containing the input script" I think the input script is the file whose
    name is the argument to the Python interpreter. E.g., if you ran
    "python c:\stuff\myscript.py" then the path is initialized relative to
    c:\stuff. If you just run the interpreter in interactive mode, it's
    current working directory when you run 'python'. To say that the path
    is initialized "from the directory" I guess only matters if any of the
    items in PYTHONPATH or the sys.path are not given as absolute pathnames
    but rather relative. If my PYTHONPATH has 'mypython' in it with no
    absolute qualifier then in the above scenario where I'm in, say, the
    c:\windows directory and I run "python c:\stuff\myscript.py" it will be
    c:\stuff\mypython that is added to the path. I think...
     
    Steve M, Sep 10, 2005
    #5
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.