How to conditionally import and run a method from another Python package?









up vote
0
down vote

favorite












I have a project that contains several test suites. I want to be able to specify which suite I'd like to run in the command line:



suite=multiplication python3 .


Here's my current file structure:



__main__.py
suites/
__init__.py
addition.py
subtraction.py
multiplication.py
division.py


suites/__init__.py



__all__ = ['addition', 'subtraction', 'multiplication', 'division']


subtraction.py



def testSuite():
# Bunch of tests


__main__.py



import os
import suites

# Get suite name from 'suite=xxx' in command line
suiteName = os.getenv('suite')
# Based on suiteName, load the correct file
suite = suites[suiteName]
# Call the suite loaded from the file
suite()


This errors out:



suite = suites[suiteName]
TypeError: 'module' object is not subscriptable


What's the best way to conditionally import and run a script from another package?










share|improve this question

























    up vote
    0
    down vote

    favorite












    I have a project that contains several test suites. I want to be able to specify which suite I'd like to run in the command line:



    suite=multiplication python3 .


    Here's my current file structure:



    __main__.py
    suites/
    __init__.py
    addition.py
    subtraction.py
    multiplication.py
    division.py


    suites/__init__.py



    __all__ = ['addition', 'subtraction', 'multiplication', 'division']


    subtraction.py



    def testSuite():
    # Bunch of tests


    __main__.py



    import os
    import suites

    # Get suite name from 'suite=xxx' in command line
    suiteName = os.getenv('suite')
    # Based on suiteName, load the correct file
    suite = suites[suiteName]
    # Call the suite loaded from the file
    suite()


    This errors out:



    suite = suites[suiteName]
    TypeError: 'module' object is not subscriptable


    What's the best way to conditionally import and run a script from another package?










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a project that contains several test suites. I want to be able to specify which suite I'd like to run in the command line:



      suite=multiplication python3 .


      Here's my current file structure:



      __main__.py
      suites/
      __init__.py
      addition.py
      subtraction.py
      multiplication.py
      division.py


      suites/__init__.py



      __all__ = ['addition', 'subtraction', 'multiplication', 'division']


      subtraction.py



      def testSuite():
      # Bunch of tests


      __main__.py



      import os
      import suites

      # Get suite name from 'suite=xxx' in command line
      suiteName = os.getenv('suite')
      # Based on suiteName, load the correct file
      suite = suites[suiteName]
      # Call the suite loaded from the file
      suite()


      This errors out:



      suite = suites[suiteName]
      TypeError: 'module' object is not subscriptable


      What's the best way to conditionally import and run a script from another package?










      share|improve this question













      I have a project that contains several test suites. I want to be able to specify which suite I'd like to run in the command line:



      suite=multiplication python3 .


      Here's my current file structure:



      __main__.py
      suites/
      __init__.py
      addition.py
      subtraction.py
      multiplication.py
      division.py


      suites/__init__.py



      __all__ = ['addition', 'subtraction', 'multiplication', 'division']


      subtraction.py



      def testSuite():
      # Bunch of tests


      __main__.py



      import os
      import suites

      # Get suite name from 'suite=xxx' in command line
      suiteName = os.getenv('suite')
      # Based on suiteName, load the correct file
      suite = suites[suiteName]
      # Call the suite loaded from the file
      suite()


      This errors out:



      suite = suites[suiteName]
      TypeError: 'module' object is not subscriptable


      What's the best way to conditionally import and run a script from another package?







      python






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 17:47









      RobertAKARobin

      1,6731223




      1,6731223






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote













          Use importlib.import_module:



          from importlib import import_module

          suite = import_module('suites.' + suiteName)
          suite.testSuite()





          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%2f53230898%2fhow-to-conditionally-import-and-run-a-method-from-another-python-package%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
            4
            down vote













            Use importlib.import_module:



            from importlib import import_module

            suite = import_module('suites.' + suiteName)
            suite.testSuite()





            share|improve this answer
























              up vote
              4
              down vote













              Use importlib.import_module:



              from importlib import import_module

              suite = import_module('suites.' + suiteName)
              suite.testSuite()





              share|improve this answer






















                up vote
                4
                down vote










                up vote
                4
                down vote









                Use importlib.import_module:



                from importlib import import_module

                suite = import_module('suites.' + suiteName)
                suite.testSuite()





                share|improve this answer












                Use importlib.import_module:



                from importlib import import_module

                suite = import_module('suites.' + suiteName)
                suite.testSuite()






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 9 at 17:52









                jwodder

                31.9k34877




                31.9k34877



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230898%2fhow-to-conditionally-import-and-run-a-method-from-another-python-package%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

                    How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                    Syphilis

                    Darth Vader #20