look further down sys.path for extension module

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

    #1

    look further down sys.path for extension module

    Hi,

    Is it possible to have Python look further down sys.path for a C
    extension module when an ImportError is raised because of the "dynamic
    module not implementing init function"?

    The thing is, I have a case where the cwd of an application that embedds
    the Python interpreter needs to be inserted into sys.path at index. For
    the windows version there is a zlib.dll in the app's directory, which -
    naturally - gets "import precedence" over zlib.pyd from the DLLs
    directory. For the time being, I place copy zlib.pyd into he app's
    directory to solve the problem, but it would be nice if there was a way
    to avoid having to do this.


    Any thoughts much appreciated.
    --
    Vincent Wehren


  • vincent wehren

    #2
    Re: look further down sys.path for extension module

    Vincent Wehren wrote:

    [...][color=blue]
    > the Python interpreter needs to be inserted into sys.path at index.[/color]

    This should say "[...] into sys.path at index 0.

    --
    Vincent Wehren


    Comment

    • Martin v. Löwis

      #3
      Re: look further down sys.path for extension module

      Vincent Wehren wrote:[color=blue]
      > Is it possible to have Python look further down sys.path for a C
      > extension module when an ImportError is raised because of the "dynamic
      > module not implementing init function"?[/color]

      You could use the imp module to import zlib.pyd "manually". Then, when
      an "import zlib" occurs, it looks into sys.modules, finds that zlib is
      already there, and forgets about looking into sys.path.

      Alternatively, you could rebuild zlib.pyd to be named pyzlib.pyd. You
      need to rebuild the code because you also need to rename the entry
      function, and the name of the module inside the module itself. You
      can then do

      import pyzlib as zlib

      Regards,
      Martin

      Comment

      • vincent wehren

        #4
        Re: look further down sys.path for extension module

        Martin v. Löwis wrote:[color=blue]
        > Vincent Wehren wrote:
        >[color=green]
        >> Is it possible to have Python look further down sys.path for a C
        >> extension module when an ImportError is raised because of the "dynamic
        >> module not implementing init function"?[/color]
        >
        >
        > You could use the imp module to import zlib.pyd "manually". Then, when
        > an "import zlib" occurs, it looks into sys.modules, finds that zlib is
        > already there, and forgets about looking into sys.path.[/color]

        This sounds like a viable option. We already have some special hooks to
        accomodate for using pywin32 from a network install so this would be
        something along those same lines.
        [color=blue]
        > Alternatively, you could rebuild zlib.pyd to be named pyzlib.pyd. You
        > need to rebuild the code because you also need to rename the entry
        > function, and the name of the module inside the module itself. You
        > can then do
        >
        > import pyzlib as zlib[/color]

        Yes, quickly tried that, too. It still is registered in sys.modules as
        "pyzlib" - don't known if I expected that...


        Thanks for your input!

        --
        Vincent Wehren

        Comment

        • Alex Martelli

          #5
          Re: look further down sys.path for extension module

          vincent wehren <vincent@visual trans.de> wrote:
          ...[color=blue][color=green]
          > > Alternatively, you could rebuild zlib.pyd to be named pyzlib.pyd. You
          > > need to rebuild the code because you also need to rename the entry
          > > function, and the name of the module inside the module itself. You
          > > can then do
          > >
          > > import pyzlib as zlib[/color]
          >
          > Yes, quickly tried that, too. It still is registered in sys.modules as
          > "pyzlib" - don't known if I expected that...[/color]

          The merely local renaming should not accidentally cause multiple
          repetitions of the module's loading if somewhere else the module gets
          simply imported with 'import pyzlib', of course. So, it's recoded in
          sys.modules['pyzlib'] -- anything else would be astonishing.

          Just add a further statement
          sys.modules['zlib'] = zlib
          right after this import, if you want future 'import zlib' statements to
          get 'redirected' to use the already-imported pyzlib instead.


          Alex

          Comment

          Working...