Importing binary modules

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • andre.dos.anjos@gmail.com

    #1

    Importing binary modules

    How can I, in python (linux, python version >= 2.4.2), dynamically set
    the path and import a binary module that depends on libraries which
    are not declared in LD_LIBRARY_PATH or any other automated linker path
    when python starts?

    This is a an example:

    Suppose I have a new python module, called MyNewModule.py. It resides
    in '/my/module/dir'. Internally, it loads libMyNewModule. so, which
    implements most of the functionality for MyNewModule, so the user can
    say:

    import sys
    sys.path.append ('/my/module/dir')
    import MyNewModule

    And everything works fine.

    The problem appears when libMyNewModule. so depends on another library,
    say libfoo.so, which sits also in /my/module/dir. In that case, '/my/
    module/dir', needs to be preset in the LD_LIBRARY_PATH *before* the
    python interpreter is set.

    In this case, the snippet of code above will not work as one would
    expect. It will normally give an ImportError saying it cannot find
    libfoo.so. That happens, apparently, because the dynamic linker cannot
    rescan LD_LIBRARY_PATH after the python interpreter has started.

    So is there any other way to circumvent this?

    import sys
    sys.path.append ('/my/module/dir')
    #black magic to get the linker to reload the LD_LIBRARY_PATH
    import MyNewModule

    So this succeeds?

  • Thinker

    #2
    Re: Importing binary modules

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    andre.dos.anjos @gmail.com wrote:
    cut ............... .......
    >
    The problem appears when libMyNewModule. so depends on another
    library, say libfoo.so, which sits also in /my/module/dir. In that
    case, '/my/ module/dir', needs to be preset in the LD_LIBRARY_PATH
    *before* the python interpreter is set.
    >
    In this case, the snippet of code above will not work as one would
    expect. It will normally give an ImportError saying it cannot find
    libfoo.so. That happens, apparently, because the dynamic linker
    cannot rescan LD_LIBRARY_PATH after the python interpreter has
    started.
    Since you create a new module, you should try to link your module with
    - -rpath-link option.
    You can specify a path searched by linker when finding shared object
    required.



    - --
    Thinker Li - thinker@branda. to thinker.li@gmai l.com

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.6 (FreeBSD)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iD8DBQFF5rcp1LD UVnWfY8gRAmLcAK Cobvo06x84L0pj6 6amTBspTJ9nUwCg 5sA+
    MP7tLF/i8zqoZHl5Fxw2Ys Q=
    =BNQy
    -----END PGP SIGNATURE-----

    Comment

    • =?iso-8859-1?B?QW5kcuk=?=

      #3
      Re: Importing binary modules

      On 1 mar, 12:21, Thinker <thin...@branda .towrote:
      -----BEGIN PGP SIGNED MESSAGE-----
      Hash: SHA1
      >
      andre.dos.an... @gmail.com wrote:
      >
      cut ............... .......
      >
      The problem appears when libMyNewModule. so depends on another
      library, say libfoo.so, which sits also in /my/module/dir. In that
      case, '/my/ module/dir', needs to be preset in the LD_LIBRARY_PATH
      *before* the python interpreter is set.
      >
      In this case, the snippet of code above will not work as one would
      expect. It will normally give an ImportError saying it cannot find
      libfoo.so. That happens, apparently, because the dynamic linker
      cannot rescan LD_LIBRARY_PATH after the python interpreter has
      started.
      >
      Since you create a new module, you should try to link your module with
      - -rpath-link option.
      You can specify a path searched by linker when finding shared object
      required.
      >
      - --
      Thinker Li - thin...@branda. to thinker...@gmai l.comhttp://heaven.branda.t o/~thinker/GinGin_CGI.py
      -----BEGIN PGP SIGNATURE-----
      Version: GnuPG v1.4.6 (FreeBSD)
      Comment: Using GnuPG with Mozilla -http://enigmail.mozdev .org
      >
      iD8DBQFF5rcp1LD UVnWfY8gRAmLcAK Cobvo06x84L0pj6 6amTBspTJ9nUwCg 5sA+
      MP7tLF/i8zqoZHl5Fxw2Ys Q=
      =BNQy
      -----END PGP SIGNATURE-----
      Well, unfortunately I don't have access to the binary like that. It is
      given me, I just wanted to use it the way it was compiled. What could
      be done then?

      Comment

      • Thinker

        #4
        Re: Importing binary modules

        -----BEGIN PGP SIGNED MESSAGE-----
        Hash: SHA1

        Well, unfortunately I don't have access to the binary like that. It
        is given me, I just wanted to use it the way it was compiled. What
        could be done then?
        You have better set LD_LIBRARY_PATH environment variable, or use
        ldconfig (plz, man ldconfig)
        to add the directory, where you shared object is in, to the search
        path of dynamic linker.

        - --
        Thinker Li - thinker@branda. to thinker.li@gmai l.com

        -----BEGIN PGP SIGNATURE-----
        Version: GnuPG v1.4.6 (FreeBSD)
        Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

        iD8DBQFF5uWt1LD UVnWfY8gRAs52AK DLkGkNaPo2Ngjdj FelNOx5KWG0KQCf T1RN
        l6WqAWcutwPtmJP leSdkrr4=
        =Ov/a
        -----END PGP SIGNATURE-----

        Comment

        Working...