Importing again and again

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

    #1

    Importing again and again

    If I have code which imports a module over and over again...say each
    time a function is called, does that cause Python to actually re-import
    it...or will it skip it once the module has been imported??

    for example:

    def foo():
    import bar
    bar.printStuff( )

    foo()
    foo()
    foo()
    foo()

    ....will that re-import bar 4 times...or just import it once? is this a
    big performance hit?

    thanks

  • Dustan

    #2
    Re: Importing again and again


    abcd wrote:[color=blue]
    > If I have code which imports a module over and over again...say each
    > time a function is called, does that cause Python to actually re-import
    > it...or will it skip it once the module has been imported??
    >
    > for example:
    >
    > def foo():
    > import bar
    > bar.printStuff( )
    >
    > foo()
    > foo()
    > foo()
    > foo()
    >
    > ...will that re-import bar 4 times...or just import it once? is this a
    > big performance hit?
    >
    > thanks[/color]

    I don't really know, but I do know that the right way to re-import a
    module is:

    reload(bar)

    Comment

    • John Salerno

      #3
      Re: Importing again and again

      abcd wrote:
      [color=blue]
      > def foo():
      > import bar
      > bar.printStuff( )
      >
      > foo()
      > foo()
      > foo()
      > foo()
      >
      > ...will that re-import bar 4 times...or just import it once? is this a
      > big performance hit?
      >
      > thanks
      >[/color]

      Given a file called bar.py with the following contents:

      print "I'm being imported!"

      def printStuff():
      print 'stuff'

      I get this output when I import foo.py:
      [color=blue][color=green][color=darkred]
      >>> import foo[/color][/color][/color]
      I'm being imported!
      stuff
      stuff
      stuff
      stuff[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]


      Comment

      • Laszlo Nagy

        #4
        Re: Importing again and again

        abcd írta:[color=blue]
        > If I have code which imports a module over and over again...say each
        > time a function is called, does that cause Python to actually re-import
        > it...or will it skip it once the module has been imported??
        >
        > for example:
        >
        > def foo():
        > import bar
        > bar.printStuff( )
        >
        > foo()
        > foo()
        > foo()
        > foo()
        >
        > ...will that re-import bar 4 times...or just import it once?[/color]
        Just once. Try this:

        bar.py:

        print "I have been imported"

        def printStuff():
        print "printStuff was called"

        foo.py:

        def foo():
        import bar
        bar.printStuff( )

        foo()
        foo()
        foo()


        The result is:

        I have been imported
        printStuff was called
        printStuff was called
        printStuff was called


        If you really need to reimport the module, you can do this:

        foo.py:

        import bar

        def foo():
        global bar
        bar = reload(bar)
        bar.printStuff( )

        foo()
        foo()
        foo()


        The result is:

        I have been imported
        I have been imported
        printStuff was called
        I have been imported
        printStuff was called
        I have been imported
        printStuff was called

        [color=blue]
        > Is this a big performance hit?
        >[/color]
        It depends on the size of your 'bar.py' module, and also it depends on
        how often you need to change/reload while your program is running.

        Best,

        Laszlo

        Comment

        • John Bokma

          #5
          Re: Importing again and again

          "abcd" <codecraig@gmai l.com> wrote:
          [color=blue]
          > If I have code which imports a module over and over again...say each
          > time a function is called, does that cause Python to actually re-import
          > it...or will it skip it once the module has been imported??
          >
          > for example:
          >
          > def foo():
          > import bar
          > bar.printStuff( )
          >
          > foo()
          > foo()
          > foo()
          > foo()
          >
          > ...will that re-import bar 4 times...or just import it once? is this a
          > big performance hit?[/color]

          I am new to Python so this might be a weird question, but it there a
          reason why you import bar inside foo?

          --
          John MexIT: http://johnbokma.com/mexit/
          personal page: http://johnbokma.com/
          Experienced programmer available: http://castleamber.com/
          Happy Customers: http://castleamber.com/testimonials.html

          Comment

          • Terry Reedy

            #6
            Re: Importing again and again


            "John Bokma" <john@castleamb er.com> wrote in message
            news:Xns97DCA4E EBEBEEcastleamb er@130.133.1.4. ..[color=blue][color=green]
            >> def foo():
            >> import bar
            >> bar.printStuff( )[/color]
            > I am new to Python so this might be a weird question, but it there a
            > reason why you import bar inside foo?[/color]

            Two possible reasons:
            1) delay import until actually needed, if ever.
            2) put 'bar' into the function local namespace instead of the module global
            namespace

            Terry Jan Reedy



            Comment

            • John Bokma

              #7
              Re: Importing again and again

              "Terry Reedy" <tjreedy@udel.e du> wrote:
              [color=blue]
              >
              > "John Bokma" <john@castleamb er.com> wrote in message
              > news:Xns97DCA4E EBEBEEcastleamb er@130.133.1.4. ..[color=green][color=darkred]
              >>> def foo():
              >>> import bar
              >>> bar.printStuff( )[/color]
              >> I am new to Python so this might be a weird question, but it there a
              >> reason why you import bar inside foo?[/color]
              >
              > Two possible reasons:
              > 1) delay import until actually needed, if ever.
              > 2) put 'bar' into the function local namespace instead of the module
              > global namespace[/color]

              OK clear and thanks. I was guessing the later, and the first one I
              overlooked (Perl works different, you have to do the delay yourself).

              --
              John MexIT: http://johnbokma.com/mexit/
              personal page: http://johnbokma.com/
              Experienced programmer available: http://castleamber.com/
              Happy Customers: http://castleamber.com/testimonials.html

              Comment

              • Schüle Daniel

                #8
                Re: Importing again and again

                it's import-ed only once

                # magic.py file

                #!/usr/bin/python
                print "here"
                import magic # try to import itself

                then try

                # bad_magic.py

                #!/usr/bin/python
                print "here"
                import bad_magic
                reload(bad_magi c)


                hth, Daniel

                Comment

                • Steve Holden

                  #9
                  Re: Importing again and again

                  abcd wrote:[color=blue]
                  > If I have code which imports a module over and over again...say each
                  > time a function is called, does that cause Python to actually re-import
                  > it...or will it skip it once the module has been imported??
                  >
                  > for example:
                  >
                  > def foo():
                  > import bar
                  > bar.printStuff( )
                  >
                  > foo()
                  > foo()
                  > foo()
                  > foo()
                  >
                  > ...will that re-import bar 4 times...or just import it once? is this a
                  > big performance hit?
                  >
                  > thanks
                  >[/color]
                  To address the performance question, by the way, the first thing that
                  the import process does is look in sys.modules to see whether the module
                  has already been imported. If so then it uses the module referenced by
                  sys.modules, so the overhead is pretty small (dicts being quite fast
                  generally speaking).

                  So your code is a reasonable way to defer the import of bar until the
                  function is called, which is why I presumed you coded it that way.

                  regards
                  Steve
                  --
                  Steve Holden +44 150 684 7255 +1 800 494 3119
                  Holden Web LLC/Ltd http://www.holdenweb.com
                  Love me, love my blog http://holdenweb.blogspot.com
                  Recent Ramblings http://del.icio.us/steve.holden

                  Comment

                  • Maric Michaud

                    #10
                    Re: Importing again and again

                    Le Jeudi 08 Juin 2006 22:02, abcd a écrit :[color=blue]
                    >
                    > def foo():
                    > import bar
                    > bar.printStuff( )
                    >
                    > foo()
                    > foo()
                    > foo()
                    > foo()
                    >
                    > ...will that re-import bar 4 times...or just import it once? is this a
                    > big performance hit?[/color]

                    Import a module more than once doesn't execute the code of this module more
                    than once.

                    I don't know what's your need but as some have spoke of reload I just want to
                    warn you, reload a module means that you want invalidate the code of this and
                    replace it by a new one, this is not like a normal but deeper import. Also,
                    you'll have to deal yourself wiith references to your old code.
                    Hmmm, the following example should be clear than my explanations :)

                    n [1]: import temp

                    In [2]: class a(temp.Base
                    temp.Base

                    In [2]: class a(temp.Base
                    temp.Base

                    In [2]: class a(temp.Base
                    temp.Base

                    In [2]: class a(temp.Base) : pass
                    ...:

                    In [3]: reload(temp)
                    Out[3]: <module 'temp' from 'temp.pyc'>

                    In [4]: class b(temp.Base) : pass
                    ...:

                    In [7]: b.__base__, a.__base__, b.__base__ is a.__base__
                    Out[7]: (<class 'temp.Base'>, <class 'temp.Base'>, False)

                    In [8]: isinstance(a(), temp.Base), isinstance(b(), temp.Base)
                    Out[8]: (False, True)

                    --
                    _____________

                    Maric Michaud
                    _____________

                    Aristote - www.aristote.info
                    3 place des tapis
                    69004 Lyon
                    Tel: +33 426 880 097

                    Comment

                    Working...