can I import the module twice (under differnet names)

Discussion in 'Python' started by alf, Nov 2, 2006.

  1. alf

    alf Guest

    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
    alf, Nov 2, 2006
    #1
    1. Advertising

  2. alf

    Ben Finney Guest

    alf <4all.nl> writes:

    > 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
    Ben Finney, Nov 2, 2006
    #2
    1. Advertising

  3. 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
    Thomas Nelson, Nov 2, 2006
    #3
  4. alf

    Steve Holden Guest

    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
    Steve Holden, Nov 2, 2006
    #4
    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.

Share This Page