Duplicating Modules

Discussion in 'Python' started by Misto ., Sep 30, 2005.

  1. Misto .

    Misto . Guest

    Hi folks!

    Short:

    There is a way to dumplicate a module ?

    I tried
    copy.deepcopy(module) 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:
    > cp module.py module1.py


    >> import module
    >> import module1



    Any Ideas?

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

    Misto
     
    Misto ., Sep 30, 2005
    #1
    1. Advertising

  2. Misto .

    kimes Guest

    Why don't you do like this..

    import module
    import mudule as module2
     
    kimes, Sep 30, 2005
    #2
    1. Advertising

  3. Misto .

    Peter Otten Guest

    kimes wrote:

    > Why don't you do like this..
    >
    > import module
    > import mudule as module2


    >>> import module as a
    >>> import module as b
    >>> b is a

    True

    You have to remove the module from the cache before the second import:

    >>> import sys
    >>> import module as a
    >>> del sys.modules["module"]
    >>> import module as b
    >>> b is a

    False

    Peter
     
    Peter Otten, Sep 30, 2005
    #3
  4. Misto . wrote:
    > Hi folks!
    >
    > Short:
    >
    > There is a way to dumplicate a module ?


    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(newname, 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('mymath', '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__(oldname)
    sys.modules[newname] = sys.modules[oldname]
    sys.modules[oldname] = tmp
    else:
    result = __import__(oldname)
    sys.modules[newname] = sys.modules[oldname]
    del sys.modules[oldname]
    return result

    Dave
     
    Dave Benjamin, Sep 30, 2005
    #4
  5. On Fri, 30 Sep 2005 19:52:56 +0200, Misto . wrote:

    > There is a way to dumplicate a module ?


    [snip]

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


    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_idiot = sys # *grins*
    py> my_boss_is_an_idiot.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_idiot 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.
     
    Steven D'Aprano, Oct 1, 2005
    #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. AC
    Replies:
    0
    Views:
    523
  2. Don

    Duplicating datasheet screens

    Don, Feb 23, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    359
  3. Todd Plambeck

    ASP.Net Duplicating DB Entries

    Todd Plambeck, Aug 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    503
  4. George Jempty

    log4j duplicating appender entries

    George Jempty, Jan 7, 2005, in forum: Java
    Replies:
    2
    Views:
    2,485
  5. johkar
    Replies:
    0
    Views:
    341
    johkar
    Aug 8, 2003
Loading...

Share This Page