import vs from module import : any performance issue?

Discussion in 'Python' started by Pierre Rouleau, Mar 6, 2004.

  1. Q1:
    I'v seen mentionned in some post that there could be performance issues
    of using:

    from moduleX import whatever

    as opposed to:

    import moduleX

    What would be the performance issues?

    Q2:
    What happens when names from moduleX are imported using the first former
    syntax in some modules of an application and imported using the latter
    syntax in other modules of the same application?

    Q3:
    It's my understanding that using the "from moduleX import whatever"
    syntax in moduleA, all of moduleX code will run and only the name
    "whatever" will be made available to moduleA. What are the benefit of
    using this syntax then, is it only the fact that you don't have to type
    the "moduleX." prefix in moduleA?


    Thanks

    Pierre
     
    Pierre Rouleau, Mar 6, 2004
    #1
    1. Advertisements

  2. Pierre Rouleau

    Peter Otten Guest

    Pierre Rouleau wrote:

    > Q1:
    > I'v seen mentionned in some post that there could be performance issues
    > of using:
    >
    > from moduleX import whatever
    >
    > as opposed to:
    >
    > import moduleX
    >
    > What would be the performance issues?


    You wouldn't put the import statement into an inner loop, so why bother.
    Every module is imported once (there is a pathological exception) and
    subsequent imports are essentially lookups in sys.modules.
    However, moduleX.whatever() is one dictionary lookup slower than the bare
    whatever(), and *that* might occur in an inner loop.

    >
    > Q2:
    > What happens when names from moduleX are imported using the first former
    > syntax in some modules of an application and imported using the latter
    > syntax in other modules of the same application?


    Nothing. You can even mix both forms in the same module:

    >>> import os
    >>> from os import walk
    >>> os.walk

    <function walk at 0x40279a74>
    >>> walk

    <function walk at 0x40279a74>
    >>>


    > Q3:
    > It's my understanding that using the "from moduleX import whatever"
    > syntax in moduleA, all of moduleX code will run and only the name
    > "whatever" will be made available to moduleA. What are the benefit of
    > using this syntax then, is it only the fact that you don't have to type
    > the "moduleX." prefix in moduleA?


    A tiny speedup. The disadvantage being pollution of the importing module's
    namespace. Not as bad as from module import *, though.

    Peter
     
    Peter Otten, Mar 6, 2004
    #2
    1. Advertisements

  3. Pierre Rouleau

    Ivo Guest

    Quote from Dive Into Pyhton
    """
    When a line of code asks for the value of a variable x, Python will search
    for
    that variable in all the available namespaces, in order:

    1. local namespace - specific to the current function or class method. If
    the
    function defines a local variable x, or has an argument x, Python will
    use
    this and stop searching.
    2. global namespace - specific to the current module. If the module has
    defined
    a variable, function, or class called x, Python will use that and stop
    searching.
    3. built-in namespace - global to all modules. As a last resort, Python
    will
    assume that x is the name of built-in function or variable.
    """
    when you use: from x import y you import into the local namespace. so in
    theory it could by a speedup to use this form. if you use import x you will
    have to address the functions by there namespace (which equals the name of
    x), so no speedup will be gotten there.



    "Peter Otten" <> wrote in message
    news:c2c2r9$p5t$03$-online.com...
    > Pierre Rouleau wrote:
    >
    > > Q1:
    > > I'v seen mentionned in some post that there could be performance issues
    > > of using:
    > >
    > > from moduleX import whatever
    > >
    > > as opposed to:
    > >
    > > import moduleX
    > >
    > > What would be the performance issues?

    >
    > You wouldn't put the import statement into an inner loop, so why bother.
    > Every module is imported once (there is a pathological exception) and
    > subsequent imports are essentially lookups in sys.modules.
    > However, moduleX.whatever() is one dictionary lookup slower than the bare
    > whatever(), and *that* might occur in an inner loop.
    >
    > >
    > > Q2:
    > > What happens when names from moduleX are imported using the first former
    > > syntax in some modules of an application and imported using the latter
    > > syntax in other modules of the same application?

    >
    > Nothing. You can even mix both forms in the same module:
    >
    > >>> import os
    > >>> from os import walk
    > >>> os.walk

    > <function walk at 0x40279a74>
    > >>> walk

    > <function walk at 0x40279a74>
    > >>>

    >
    > > Q3:
    > > It's my understanding that using the "from moduleX import whatever"
    > > syntax in moduleA, all of moduleX code will run and only the name
    > > "whatever" will be made available to moduleA. What are the benefit of
    > > using this syntax then, is it only the fact that you don't have to type
    > > the "moduleX." prefix in moduleA?

    >
    > A tiny speedup. The disadvantage being pollution of the importing module's
    > namespace. Not as bad as from module import *, though.
    >
    > Peter
     
    Ivo, Mar 6, 2004
    #3
  4. Pierre Rouleau

    Aahz Guest

    In article <3Rc2c.20653$>,
    Pierre Rouleau <> wrote:
    >
    >Q3:
    >It's my understanding that using the "from moduleX import whatever"
    >syntax in moduleA, all of moduleX code will run and only the name
    >"whatever" will be made available to moduleA. What are the benefit of
    >using this syntax then, is it only the fact that you don't have to type
    >the "moduleX." prefix in moduleA?


    Essentially, yes.
    --
    Aahz () <*> http://www.pythoncraft.com/

    "Do not taunt happy fun for loops. Do not change lists you are looping over."
    --Remco Gerlich, comp.lang.python
     
    Aahz, Mar 6, 2004
    #4
  5. Pierre Rouleau, Mar 7, 2004
    #5
    1. Advertisements

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.
Similar Threads
  1. Jeff Sykes

    module import performance question

    Jeff Sykes, Nov 11, 2003, in forum: Python
    Replies:
    4
    Views:
    514
    Scott D. Daniels
    Nov 11, 2003
  2. plb
    Replies:
    0
    Views:
    586
  3. plb
    Replies:
    0
    Views:
    461
  4. Xiao Jianfeng
    Replies:
    2
    Views:
    999
    Ben Finney
    Dec 16, 2005
  5. Mudcat
    Replies:
    3
    Views:
    1,110
    Steven D'Aprano
    Jan 14, 2006
  6. Kottiyath

    Python module import loop issue

    Kottiyath, Dec 29, 2008, in forum: Python
    Replies:
    5
    Views:
    982
    Gabriel Genellina
    Dec 30, 2008
  7. Peng Yu
    Replies:
    0
    Views:
    497
    Peng Yu
    Oct 31, 2009
  8. Stéphane Klein
    Replies:
    0
    Views:
    458
    Stéphane Klein
    Jul 5, 2011
Loading...