import hook

Discussion in 'Python' started by Jeremy Sanders, Jun 11, 2006.

  1. 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
    http://www.jeremysanders.net/
     
    Jeremy Sanders, Jun 11, 2006
    #1
    1. Advertising

  2. Jeremy Sanders

    Rune Strand Guest

    Jeremy Sanders wrote:
    > Hi - Is it possible to override the import process so that if in my program
    > I do

    (...)
    >
    > Any ideas?



    Why not handle the foo.bar/version string separately and just append
    the resulting path to sys.path?
     
    Rune Strand, Jun 11, 2006
    #2
    1. Advertising

  3. Jeremy Sanders <> wrote:

    > 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?


    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
     
    Alex Martelli, Jun 11, 2006
    #3
  4. Alex Martelli wrote:
    > Jeremy Sanders <> wrote:
    >
    >> 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?

    >
    > 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/> ...


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

    Thomas
     
    Thomas Heller, Jun 11, 2006
    #4
  5. Thomas Heller wrote:

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


    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
    http://www.jeremysanders.net/
     
    Jeremy Sanders, Jun 18, 2006
    #5
    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. David Pifer via .NET 247

    Hook JScript Client Events to Server Controls

    David Pifer via .NET 247, May 2, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,794
    David Jessee
    May 2, 2004
  2. Noam Raphael
    Replies:
    0
    Views:
    282
    Noam Raphael
    Jun 21, 2004
  3. Torsten Mohr

    import hook, overwrite import?

    Torsten Mohr, Jan 26, 2005, in forum: Python
    Replies:
    2
    Views:
    438
    Steve Holden
    Jan 27, 2005
  4. Andrea Crotti

    simple import hook

    Andrea Crotti, Nov 10, 2011, in forum: Python
    Replies:
    0
    Views:
    137
    Andrea Crotti
    Nov 10, 2011
  5. Andrea Crotti
    Replies:
    0
    Views:
    154
    Andrea Crotti
    Jan 19, 2012
Loading...

Share This Page