can I import the module twice (under differnet names)

Thomas Nelson thn at mail.utexas.edu
Thu Nov 2 01:00:02 EST 2006


alf wrote:
> Hi,
>
> wonder if in the python I could treat modules imorts like classes
> instances. It means I could import it twice or more times under
> different names.
>
> --
> alfz1

You can always give any object as many names as you want:
>>> import sys
>>> s1 = sys
>>> s2 = sys
>>> s1.path
['', '/usr/local/bin', '/bin', '/sbin', '/usr/bin', '/usr/sbin',...
>>> s2.argv
['']

Or maybe you're looking for the builtin function reload?
>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
...
>>> import this  #note nothing happens
>>> reload(this)  #runs again
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
....


Neither one of these methods are too common in practice I think.  Could
you tell us why you want to import a module more than once?
-Tom




More information about the Python-list mailing list