can I import the module twice (under differnet names)

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

    #1

    can I import the module twice (under differnet names)

    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
  • Ben Finney

    #2
    Re: can I import the module twice (under differnet names)

    alf <ask@me.xs4all. nlwrites:
    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.
    >>import sys
    >>foo = sys
    >>bar = sys
    >>print sys.maxint
    2147483647
    >>print foo.maxint
    2147483647
    >>print bar.maxint
    2147483647

    --
    \ "Laugh and the world laughs with you; snore and you sleep |
    `\ alone." -- Anonymous |
    _o__) |
    Ben Finney

    Comment

    • Thomas Nelson

      #3
      Re: can I import the module twice (under differnet names)

      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

      Comment

      • Steve Holden

        #4
        Re: can I import the module twice (under differnet names)

        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.
        >
        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
        --
        Steve Holden +44 150 684 7255 +1 800 494 3119
        Holden Web LLC/Ltd http://www.holdenweb.com
        Skype: holdenweb http://holdenweb.blogspot.com
        Recent Ramblings http://del.icio.us/steve.holden

        Comment

        Working...