package search

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

    #1

    package search

    I have two directories, lib1 and lib2, that both contain the package
    foo, one with the submodule mod1
    and the other with the submodule mod2:

    $ ls lib*/foo/
    lib1/foo/:
    __init__.py __init__.pyc mod1.py mod1.pyc

    lib2/foo/:
    __init__.py __init__.pyc mod2.py mod2.pyc
    $

    Now this script:

    import sys
    sys.path.append ("lib1")
    sys.path.append ("lib2")
    import foo.mod1

    will find the module foo.mod1, while the same script with the two
    append-lines interchanged will not:

    import sys
    sys.path.append ("lib2")
    sys.path.append ("lib1")
    import foo.mod1

    The error is:

    import foo.mod1
    ImportError: No module named mod1

    So I guess that when Python finds the package foo, it will not look for
    it again using the rest of
    the search path, even if at that first place the module could not be
    found.

    Other languages like e.g. Java would find the module mod1 in the second
    case too (if Java doesn't
    find it in the first place, it will also look for it in the second
    place).

    This behaviour of Python is very unfortunate, because you always have
    to merge your package
    trees. In my cvs I have several packages, each in its own cvs module,
    all starting with
    de.science_comp uting. Now if I need two of them in a new project, I
    will have two package trees
    in my cvs sandbox, both starting with de.science_comp uting. Leaving the
    sandbox that way, cvs
    will work but Python imports will fail while if I merge all the package
    trees, Python will work but cvs
    won't.

    My questions:
    - Have I done anything wrong?
    - If not, is this behaviour of Python a bug or intentional?
    - If it it intentional, what is the intent?

    Thank you for your help
    Boris

  • boris

    #2
    Re: package search


    Sybren Stuvel wrote:[color=blue]
    > Simplicity and explicitness. You have two packages 'foo', and it
    > simply loads the first one it finds.[/color]

    Yes, here you are right. It is indeed simple.

    Boris

    Comment

    • imho

      #3
      Re: package search

      boris ha scritto:[color=blue]
      > I have two directories, lib1 and lib2, that both contain the package
      > foo, one with the submodule mod1
      > and the other with the submodule mod2:
      > [...]
      > Now this script:
      >
      > import sys
      > sys.path.append ("lib1")
      > sys.path.append ("lib2")
      > import foo.mod1
      >
      > will find the module foo.mod1, while the same script with the two
      > append-lines interchanged will not:
      >
      > import sys
      > sys.path.append ("lib2")
      > sys.path.append ("lib1")
      > import foo.mod1
      >
      > The error is:
      >
      > import foo.mod1
      > ImportError: No module named mod1
      > [...][/color]

      You just have to put in "__init__.p y" in "lib2" (the package directory
      you are "extending" ), the following lines:

      from pkgutil import extend_path
      __path__ = extend_path(__p ath__, __name__)

      "__path__", in each __init__ module, is a list initialized with the
      module's path, but you can extend it by appending paths where you want
      the interpreter to look for further modules.
      pkgutil.extend_ path automatically appends to __path__ all subdirectories
      of directories on sys.path named after the package.

      HTH :-)

      Diego.

      Comment

      • boris

        #4
        Re: package search


        imho wrote:[color=blue]
        >
        > You just have to put in "__init__.p y" in "lib2" (the package directory
        > you are "extending" ), the following lines:
        >
        > from pkgutil import extend_path
        > __path__ = extend_path(__p ath__, __name__)
        >
        > "__path__", in each __init__ module, is a list initialized with the
        > module's path, but you can extend it by appending paths where you want
        > the interpreter to look for further modules.
        > pkgutil.extend_ path automatically appends to __path__ all subdirectories
        > of directories on sys.path named after the package.
        >
        > HTH :-)
        >
        > Diego.[/color]

        COOL! You just saved me an awful lot of work.

        Thanks, Diego!

        Boris

        Comment

        • imho

          #5
          Re: package search

          boris ha scritto:
          [color=blue]
          >
          > COOL! You just saved me an awful lot of work.
          >
          > Thanks, Diego!
          >
          > Boris
          >[/color]

          ;-)

          Bye

          Comment

          Working...