Duplicating Modules

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

    #1

    Duplicating Modules

    Hi folks!

    Short:

    There is a way to dumplicate a module ?

    I tried
    copy.deepcopy(m odule) but hangs with an error (also with standard modules )..

    The only solution that I have by now is creating two files and importing them.
    I.E:[color=blue]
    > cp module.py module1.py[/color]
    [color=blue][color=green]
    >> import module
    >> import module1[/color][/color]


    Any Ideas?

    P.S: I know that there is some design Issue here, but my boss says no :)

    Misto
  • kimes

    #2
    Re: Duplicating Modules

    Why don't you do like this..

    import module
    import mudule as module2

    Comment

    • Peter Otten

      #3
      Re: Duplicating Modules

      kimes wrote:
      [color=blue]
      > Why don't you do like this..
      >
      > import module
      > import mudule as module2[/color]
      [color=blue][color=green][color=darkred]
      >>> import module as a
      >>> import module as b
      >>> b is a[/color][/color][/color]
      True

      You have to remove the module from the cache before the second import:
      [color=blue][color=green][color=darkred]
      >>> import sys
      >>> import module as a
      >>> del sys.modules["module"]
      >>> import module as b
      >>> b is a[/color][/color][/color]
      False

      Peter


      Comment

      • Dave Benjamin

        #4
        Re: Duplicating Modules

        Misto . wrote:[color=blue]
        > Hi folks!
        >
        > Short:
        >
        > There is a way to dumplicate a module ?[/color]

        Here's one way... it doesn't quite work with modules inside of packages,
        unfortunately, but it does avoid defeating module caching and tries to
        keep sys.modules in a predictable state. I don't know what the
        thread-safety implications are for this sort of trickery with sys.modules.

        def import_as(newna me, oldname):
        """Import a module under a different name.

        This procedure always returns a brand new module, even if
        the original module has always been imported.

        Example::

        try:
        # Reuse this module if it's already been imported
        # as "mymath".
        import mymath
        except ImportError:
        # "mymath" has not yet been imported.
        # Import and customize it.
        mymath = import_as('myma th', 'math')
        mymath.phi = (mymath.sqrt(5. 0) - 1.0) / 2.0

        The above code will not reinitialize "mymath" if it executes
        a second time (ie. if the module containing this code is
        reloaded). Whether or not "math" has already been imported,
        it will always be a different object than "mymath".
        """

        import sys
        if sys.modules.has _key(oldname):
        tmp = sys.modules[oldname]
        del sys.modules[oldname]
        result = __import__(oldn ame)
        sys.modules[newname] = sys.modules[oldname]
        sys.modules[oldname] = tmp
        else:
        result = __import__(oldn ame)
        sys.modules[newname] = sys.modules[oldname]
        del sys.modules[oldname]
        return result

        Dave

        Comment

        • Steven D'Aprano

          #5
          Re: Duplicating Modules

          On Fri, 30 Sep 2005 19:52:56 +0200, Misto . wrote:
          [color=blue]
          > There is a way to dumplicate a module ?[/color]

          [snip]
          [color=blue]
          > P.S: I know that there is some design Issue here, but my boss says no :)[/color]

          It depends on what you are expecting to do with the duplicated module. If
          all you need is to access the same module from two different names, you
          can do this:

          py> import sys
          py> my_boss_is_an_i diot = sys # *grins*
          py> my_boss_is_an_i diot.version
          '2.3.3 (#1, May 7 2004, 10:31:40) \n[GCC 3.3.3 20040412 (Red Hat Linux
          3.3.3-7)]'


          But keep in mind that using this method, sys and my_boss_is_an_i diot are
          merely different names for the same underlying module. Change one and you
          change the other.

          I'm curious... I don't expect you to comment on your boss' mental state,
          but how/why do you need to duplicate the module?


          --
          Steven.

          Comment

          Working...