import hook

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

    #1

    import hook

    Hi - Is it possible to override the import process so that if in my program
    I do

    import foo.bar

    Python will look for bar in a directory which isn't called foo?

    I want my module/program to be able to be run without being installed in
    site-packages, so by doing "import foo.bar", it should start looking for
    bar in the current directory which could be called "foo-0.43".

    I've tried overriding __import__, chopping out "foo." from package names,
    but that tends to break. I've also tried overriding imp.find_module () but
    Python never appears to use my version.

    Any ideas?

    Thanks

    Jeremy

    --
    Jeremy Sanders

  • Rune Strand

    #2
    Re: import hook

    Jeremy Sanders wrote:[color=blue]
    > Hi - Is it possible to override the import process so that if in my program
    > I do[/color]
    (...)[color=blue]
    >
    > Any ideas?[/color]


    Why not handle the foo.bar/version string separately and just append
    the resulting path to sys.path?

    Comment

    • Alex Martelli

      #3
      Re: import hook

      Jeremy Sanders <jeremy+complan gpython@jeremys anders.net> wrote:
      [color=blue]
      > Hi - Is it possible to override the import process so that if in my program
      > I do
      >
      > import foo.bar
      >
      > Python will look for bar in a directory which isn't called foo?
      >
      > I want my module/program to be able to be run without being installed in
      > site-packages, so by doing "import foo.bar", it should start looking for
      > bar in the current directory which could be called "foo-0.43".
      >
      > I've tried overriding __import__, chopping out "foo." from package names,
      > but that tends to break. I've also tried overriding imp.find_module () but
      > Python never appears to use my version.
      >
      > Any ideas?[/color]

      Yes, PEP 302 (which despite being marked as "draft" has in fact been
      already mostly implemented, since it's used by the zipimport mechanism)
      allows you to perform such feats. Study it at
      <http://www.python.org/dev/peps/pep-0302/> ...


      Alex

      Comment

      • Thomas Heller

        #4
        Re: import hook

        Alex Martelli wrote:[color=blue]
        > Jeremy Sanders <jeremy+complan gpython@jeremys anders.net> wrote:
        >[color=green]
        >> Hi - Is it possible to override the import process so that if in my program
        >> I do
        >>
        >> import foo.bar
        >>
        >> Python will look for bar in a directory which isn't called foo?
        >>
        >> I want my module/program to be able to be run without being installed in
        >> site-packages, so by doing "import foo.bar", it should start looking for
        >> bar in the current directory which could be called "foo-0.43".
        >>
        >> I've tried overriding __import__, chopping out "foo." from package names,
        >> but that tends to break. I've also tried overriding imp.find_module () but
        >> Python never appears to use my version.
        >>
        >> Any ideas?[/color]
        >
        > Yes, PEP 302 (which despite being marked as "draft" has in fact been
        > already mostly implemented, since it's used by the zipimport mechanism)
        > allows you to perform such feats. Study it at
        > <http://www.python.org/dev/peps/pep-0302/> ...[/color]

        There are also other ways. You could extend __path__ of foo, and the
        pkgutil module might also be useful.

        Thomas

        Comment

        • Jeremy Sanders

          #5
          Re: import hook

          Thomas Heller wrote:
          [color=blue]
          > There are also other ways. You could extend __path__ of foo, and the
          > pkgutil module might also be useful.[/color]

          The __path__ trick worked nicely, thanks. Here is the code in case anyone is
          interested

          # Allow veusz to be run even if not installed into PYTHONPATH
          try:
          import veusz
          except ImportError:
          # load in the veusz module, but change its path to
          # the veusz directory, and insert it into sys.modules
          import __init__ as veusz
          thisdir = os.path.dirname ( os.path.abspath (__file__) )
          veusz.__path__ = [thisdir]
          veusz.__name__ = 'veusz'
          sys.modules['veusz'] = veusz

          This is part of the main program. If it can't import it (i.e. it is not
          installed), it imports the __init__ module, renames it, and corrects its
          path, then sticks it into the list of imported modules.

          Jeremy

          --
          Jeremy Sanders

          Comment

          Working...