By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,749 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,749 IT Pros & Developers. It's quick & easy.

Duplicating Modules

P: n/a
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
Sep 30 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Why don't you do like this..

import module
import mudule as module2

Sep 30 '05 #2

P: n/a
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
Sep 30 '05 #3

P: n/a
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
Sep 30 '05 #4

P: n/a
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.

Oct 1 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.