can I import the module twice (under differnet names)

A

alf

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.
 
B

Ben Finney

alf said:
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.

No need to import more than once. The 'import' statement binds a
module object to the specified name, and you can bind as many names as
you like to the same object.
2147483647
 
T

Thomas Nelson

alf said:
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.

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?The Zen of Python, by Tim Peters

Beautiful is better than ugly.
....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
 
S

Steve Holden

alf said:
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.
If you want to repeat the full import, and have each imported version
get an independent namespace as well as independent code objects and so
on then you will have to somehow persuade the interpreter that they come
from different files, I believe.

If you just want to be able to use several names to refer to the same
module then you have already had a coupe of good answers.

regards
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top