what is Python's module search path?

Discussion in 'Python' started by Stephen Ferg, Jan 8, 2004.

  1. Stephen Ferg

    Stephen Ferg Guest

    I need a little help here.

    I'm developing some introductory material on Python for non-programmers.

    The first draft includes this statement. Is this correct?

    -----------------------------------------------------------------
    When loading modules, Python looks for modules
    in the following places in the following order:

    * Python's built-in modules, including modules in the standard library
    * in the /python23/Libs/site-packages directory
    * the directory from which your main module was loaded
    * in directories in PYTHONPATH

    ------------------------------------------------------------------
    Thanks in advance! -- Steve Ferg
     
    Stephen Ferg, Jan 8, 2004
    #1
    1. Advertising

  2. Stephen Ferg

    Rene Pijlman Guest

    Rene Pijlman, Jan 8, 2004
    #2
    1. Advertising

  3. Stephen Ferg

    Stephen Ferg Guest

    Thanks.

    It says """When a module named spam is imported, the interpreter
    searches for a file named spam.py in the current directory, and then
    in the list of directories specified by the environment variable
    PYTHONPATH. ... When PYTHONPATH is not set, or when the file is not
    found there, the search continues in an installation-dependent default
    path; on Unix, this is usually .:/usr/local/lib/python.

    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), PYTHONPATH and the
    installation-dependent default."""

    Is the installation-dependent default path on Windows usually
    Lib/site-packages?

    If so, then it looks like my original search order was wrong,
    and the correct search order (on Windows) is:

    ---------------------------------------------------------
    * Python's built-in modules, including modules in the standard
    library
    * the directory from which your main module was loaded
    * in directories in PYTHONPATH
    * in the /python23/Libs/site-packages directory
    --------------------------------------------------------

    Is that correct?

    Part of what is confusing me is that Lib/site-packages is pretty
    poorly documented.
     
    Stephen Ferg, Jan 8, 2004
    #3
  4. Stephen Ferg

    Rene Pijlman Guest

    Stephen Ferg:
    >It says """When a module named spam is imported, the interpreter
    >searches for a file named spam.py in the current directory, and then
    >in the list of directories specified by the environment variable
    >PYTHONPATH. ... When PYTHONPATH is not set, or when the file is not
    >found there, the search continues in an installation-dependent default
    >path; on Unix, this is usually .:/usr/local/lib/python.
    >
    >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), PYTHONPATH and the
    >installation-dependent default."""
    >
    >Is the installation-dependent default path on Windows usually
    >Lib/site-packages?


    I guess it's more than that.

    PythonWin 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on
    win32.
    >>> import sys
    >>> print sys.path

    ['','C:\\Python\\Python22\\lib\\site-packages\\Pythonwin',
    'C:\\Python\\Python22\\lib\\site-packages\\win32',
    'C:\\Python\\Python22\\lib\\site-packages\\win32\\lib',
    'C:\\Python\\Python22\\lib\\site-packages', 'C:\\Python\\Python22\\DLLs',
    'C:\\Python\\Python22\\lib', 'C:\\Python\\Python22\\lib\\lib-tk',
    'C:\\Python\\Python22']

    None of these directories qualify for any of the other categories, so
    these must all be in the installation-dependent default.

    >If so, then it looks like my original search order was wrong,
    >and the correct search order (on Windows) is:
    >
    >---------------------------------------------------------
    > * Python's built-in modules, including modules in the standard
    >library
    > * the directory from which your main module was loaded
    > * in directories in PYTHONPATH
    > * in the /python23/Libs/site-packages directory
    >--------------------------------------------------------
    >
    >Is that correct?


    Well no, I don't think so. The tutorial doesn't say that the modules in
    the standard library are searched first. It says the current directory is
    first, which you don't even mention. And the tutorial says sys.path is
    searched, which may be modified by the program.

    Why are you trying to rephrase the tutorial, when the tutorial is
    perfectly clear?

    >Part of what is confusing me is that Lib/site-packages is pretty
    >poorly documented.


    It doesn't have to be, for ordinary users. My guess is the documentation
    is in this area: http://www.python.org/sigs/distutils-sig/doc/

    --
    René Pijlman
     
    Rene Pijlman, Jan 9, 2004
    #4
    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. Ajay

    module search path

    Ajay, Jul 30, 2004, in forum: Python
    Replies:
    1
    Views:
    276
    angel
    Jul 30, 2004
  2. Ron Adam
    Replies:
    3
    Views:
    450
    Ron Adam
    Aug 2, 2005
  3. aznach
    Replies:
    1
    Views:
    246
    Thomas Guettler
    Oct 17, 2006
  4. Russ
    Replies:
    3
    Views:
    368
    Gabriel Genellina
    Feb 21, 2007
  5. Abby Lee
    Replies:
    5
    Views:
    426
    Abby Lee
    Aug 2, 2004
Loading...

Share This Page