Python 3+ import package in a function call?











up vote
2
down vote

favorite












Overtime I have built up a collection of utility functions for various things.



I would like to put them all in package, with a bit more structure than just a single file containing all the functions.



Some of these functions are written assuming certain packages have been imported e.g. I have several numpy and pandas utility functions that assume something like import numpy as np



Obviously I will not use this hypothetical package like from <pkg> import * but I do not want to hinder performance either.



So if I have a numpy utility function, should I add this to every function



# mypkg.np.utils
import sys

def np_util_fn(...):
if 'np' not in sys.modules: import numpy as np
# rest of func


or



# mypkg.np.utils
import sys
if 'np' not in sys.modules: import numpy as np

def np_util_fn(...):
# rest of func


which is more performant if I use a different part of this package? e.g. from pkg.other.utils import fn










share|improve this question


























    up vote
    2
    down vote

    favorite












    Overtime I have built up a collection of utility functions for various things.



    I would like to put them all in package, with a bit more structure than just a single file containing all the functions.



    Some of these functions are written assuming certain packages have been imported e.g. I have several numpy and pandas utility functions that assume something like import numpy as np



    Obviously I will not use this hypothetical package like from <pkg> import * but I do not want to hinder performance either.



    So if I have a numpy utility function, should I add this to every function



    # mypkg.np.utils
    import sys

    def np_util_fn(...):
    if 'np' not in sys.modules: import numpy as np
    # rest of func


    or



    # mypkg.np.utils
    import sys
    if 'np' not in sys.modules: import numpy as np

    def np_util_fn(...):
    # rest of func


    which is more performant if I use a different part of this package? e.g. from pkg.other.utils import fn










    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      Overtime I have built up a collection of utility functions for various things.



      I would like to put them all in package, with a bit more structure than just a single file containing all the functions.



      Some of these functions are written assuming certain packages have been imported e.g. I have several numpy and pandas utility functions that assume something like import numpy as np



      Obviously I will not use this hypothetical package like from <pkg> import * but I do not want to hinder performance either.



      So if I have a numpy utility function, should I add this to every function



      # mypkg.np.utils
      import sys

      def np_util_fn(...):
      if 'np' not in sys.modules: import numpy as np
      # rest of func


      or



      # mypkg.np.utils
      import sys
      if 'np' not in sys.modules: import numpy as np

      def np_util_fn(...):
      # rest of func


      which is more performant if I use a different part of this package? e.g. from pkg.other.utils import fn










      share|improve this question













      Overtime I have built up a collection of utility functions for various things.



      I would like to put them all in package, with a bit more structure than just a single file containing all the functions.



      Some of these functions are written assuming certain packages have been imported e.g. I have several numpy and pandas utility functions that assume something like import numpy as np



      Obviously I will not use this hypothetical package like from <pkg> import * but I do not want to hinder performance either.



      So if I have a numpy utility function, should I add this to every function



      # mypkg.np.utils
      import sys

      def np_util_fn(...):
      if 'np' not in sys.modules: import numpy as np
      # rest of func


      or



      # mypkg.np.utils
      import sys
      if 'np' not in sys.modules: import numpy as np

      def np_util_fn(...):
      # rest of func


      which is more performant if I use a different part of this package? e.g. from pkg.other.utils import fn







      python






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 18 at 8:21









      SumNeuron

      1,075723




      1,075723
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          Ok, let's analyze your issue. Assume you have a file module.py:



          print("Module got imported")


          and a file test.py with:



          import module
          import module


          . If you now execute test.py you will get



          Module got imported


          . Please note that this line is not outputted two times. This means that python already checks whether a module was already imported (before reimporting it). So your check if 'np' not in sys.modules: import numpy as np is not needed. This check only delays things as it may result in a double check.



          In case you want to reimport a module you need reload(module). So if you have



          import module
          import module
          reload(module)


          in code.py you will see the line Module got imported two times.



          This means that



          import numpy as np


          is sufficient. There is no need to check whether it already got imported via:



          if 'np' not in sys.modules: import numpy as np


          It depends whether it is advantageous to do import numpy as np at the very beginning of your script or in a function. If the function is executed multiple times, it is advantageous to do so only at the very beginning. Otherwise you are rechecking whether 'np' is not in sys.modules all the time. In contrast if you can argue that your function is not called to often / is not necessarily executed in your program (e.g. because it depends on user input) then it may be advantageous (seen from the "point vu" of speed) to import this module in a function only.



          I normally don't use any import statements in functions as I always have the feeling that they blow up the function body and thus reduce readability.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53359054%2fpython-3-import-package-in-a-function-call%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote



            accepted










            Ok, let's analyze your issue. Assume you have a file module.py:



            print("Module got imported")


            and a file test.py with:



            import module
            import module


            . If you now execute test.py you will get



            Module got imported


            . Please note that this line is not outputted two times. This means that python already checks whether a module was already imported (before reimporting it). So your check if 'np' not in sys.modules: import numpy as np is not needed. This check only delays things as it may result in a double check.



            In case you want to reimport a module you need reload(module). So if you have



            import module
            import module
            reload(module)


            in code.py you will see the line Module got imported two times.



            This means that



            import numpy as np


            is sufficient. There is no need to check whether it already got imported via:



            if 'np' not in sys.modules: import numpy as np


            It depends whether it is advantageous to do import numpy as np at the very beginning of your script or in a function. If the function is executed multiple times, it is advantageous to do so only at the very beginning. Otherwise you are rechecking whether 'np' is not in sys.modules all the time. In contrast if you can argue that your function is not called to often / is not necessarily executed in your program (e.g. because it depends on user input) then it may be advantageous (seen from the "point vu" of speed) to import this module in a function only.



            I normally don't use any import statements in functions as I always have the feeling that they blow up the function body and thus reduce readability.






            share|improve this answer



























              up vote
              3
              down vote



              accepted










              Ok, let's analyze your issue. Assume you have a file module.py:



              print("Module got imported")


              and a file test.py with:



              import module
              import module


              . If you now execute test.py you will get



              Module got imported


              . Please note that this line is not outputted two times. This means that python already checks whether a module was already imported (before reimporting it). So your check if 'np' not in sys.modules: import numpy as np is not needed. This check only delays things as it may result in a double check.



              In case you want to reimport a module you need reload(module). So if you have



              import module
              import module
              reload(module)


              in code.py you will see the line Module got imported two times.



              This means that



              import numpy as np


              is sufficient. There is no need to check whether it already got imported via:



              if 'np' not in sys.modules: import numpy as np


              It depends whether it is advantageous to do import numpy as np at the very beginning of your script or in a function. If the function is executed multiple times, it is advantageous to do so only at the very beginning. Otherwise you are rechecking whether 'np' is not in sys.modules all the time. In contrast if you can argue that your function is not called to often / is not necessarily executed in your program (e.g. because it depends on user input) then it may be advantageous (seen from the "point vu" of speed) to import this module in a function only.



              I normally don't use any import statements in functions as I always have the feeling that they blow up the function body and thus reduce readability.






              share|improve this answer

























                up vote
                3
                down vote



                accepted







                up vote
                3
                down vote



                accepted






                Ok, let's analyze your issue. Assume you have a file module.py:



                print("Module got imported")


                and a file test.py with:



                import module
                import module


                . If you now execute test.py you will get



                Module got imported


                . Please note that this line is not outputted two times. This means that python already checks whether a module was already imported (before reimporting it). So your check if 'np' not in sys.modules: import numpy as np is not needed. This check only delays things as it may result in a double check.



                In case you want to reimport a module you need reload(module). So if you have



                import module
                import module
                reload(module)


                in code.py you will see the line Module got imported two times.



                This means that



                import numpy as np


                is sufficient. There is no need to check whether it already got imported via:



                if 'np' not in sys.modules: import numpy as np


                It depends whether it is advantageous to do import numpy as np at the very beginning of your script or in a function. If the function is executed multiple times, it is advantageous to do so only at the very beginning. Otherwise you are rechecking whether 'np' is not in sys.modules all the time. In contrast if you can argue that your function is not called to often / is not necessarily executed in your program (e.g. because it depends on user input) then it may be advantageous (seen from the "point vu" of speed) to import this module in a function only.



                I normally don't use any import statements in functions as I always have the feeling that they blow up the function body and thus reduce readability.






                share|improve this answer














                Ok, let's analyze your issue. Assume you have a file module.py:



                print("Module got imported")


                and a file test.py with:



                import module
                import module


                . If you now execute test.py you will get



                Module got imported


                . Please note that this line is not outputted two times. This means that python already checks whether a module was already imported (before reimporting it). So your check if 'np' not in sys.modules: import numpy as np is not needed. This check only delays things as it may result in a double check.



                In case you want to reimport a module you need reload(module). So if you have



                import module
                import module
                reload(module)


                in code.py you will see the line Module got imported two times.



                This means that



                import numpy as np


                is sufficient. There is no need to check whether it already got imported via:



                if 'np' not in sys.modules: import numpy as np


                It depends whether it is advantageous to do import numpy as np at the very beginning of your script or in a function. If the function is executed multiple times, it is advantageous to do so only at the very beginning. Otherwise you are rechecking whether 'np' is not in sys.modules all the time. In contrast if you can argue that your function is not called to often / is not necessarily executed in your program (e.g. because it depends on user input) then it may be advantageous (seen from the "point vu" of speed) to import this module in a function only.



                I normally don't use any import statements in functions as I always have the feeling that they blow up the function body and thus reduce readability.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 18 at 8:44

























                answered Nov 18 at 8:37









                quant

                1,54911526




                1,54911526






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53359054%2fpython-3-import-package-in-a-function-call%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Costa Masnaga

                    Fotorealismo

                    Sidney Franklin