How to check if a module or a package is already installed in python3?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite
2












Need to verify whether it is installed or not before the program can run.










share|improve this question



















  • 2




    Just run the pip install. If it's already installed, it won't install it again.
    – jordanm
    Oct 10 '15 at 17:34










  • Thnk you jordanm, but i need to check and give a o/p then testing.
    – hubatrix
    Oct 10 '15 at 17:38















up vote
3
down vote

favorite
2












Need to verify whether it is installed or not before the program can run.










share|improve this question



















  • 2




    Just run the pip install. If it's already installed, it won't install it again.
    – jordanm
    Oct 10 '15 at 17:34










  • Thnk you jordanm, but i need to check and give a o/p then testing.
    – hubatrix
    Oct 10 '15 at 17:38













up vote
3
down vote

favorite
2









up vote
3
down vote

favorite
2






2





Need to verify whether it is installed or not before the program can run.










share|improve this question















Need to verify whether it is installed or not before the program can run.







package-management python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 21 at 17:13









Jeff Schaller

34.3k951114




34.3k951114










asked Oct 10 '15 at 17:17









hubatrix

721310




721310







  • 2




    Just run the pip install. If it's already installed, it won't install it again.
    – jordanm
    Oct 10 '15 at 17:34










  • Thnk you jordanm, but i need to check and give a o/p then testing.
    – hubatrix
    Oct 10 '15 at 17:38













  • 2




    Just run the pip install. If it's already installed, it won't install it again.
    – jordanm
    Oct 10 '15 at 17:34










  • Thnk you jordanm, but i need to check and give a o/p then testing.
    – hubatrix
    Oct 10 '15 at 17:38








2




2




Just run the pip install. If it's already installed, it won't install it again.
– jordanm
Oct 10 '15 at 17:34




Just run the pip install. If it's already installed, it won't install it again.
– jordanm
Oct 10 '15 at 17:34












Thnk you jordanm, but i need to check and give a o/p then testing.
– hubatrix
Oct 10 '15 at 17:38





Thnk you jordanm, but i need to check and give a o/p then testing.
– hubatrix
Oct 10 '15 at 17:38











6 Answers
6






active

oldest

votes

















up vote
3
down vote



accepted










Type in the shell: pydoc modules .

This will list modules and you can grep the module which you want.

Found on stackoverflow here






share|improve this answer





























    up vote
    8
    down vote













    You should use pip's list command with grep, that only lists installed packages (not all modules and their neighbours as well):



    pip list | grep -F package_name





    share|improve this answer



























      up vote
      1
      down vote













      If the package doesn't do something crazy or time consuming on import you can try actually importing it:





      if python -c "import package_name" &> /dev/null; then
      echo 'all good'
      else
      echo 'uh oh'
      fi





      share|improve this answer



























        up vote
        1
        down vote













        I have found existing answers incomplete and lacking good enough examples. Here is the solution I have settled on:



        # an example checking if the pandas package is installed
        if python -c 'import pkgutil; exit(not pkgutil.find_loader("pandas"))'; then
        echo 'pandas found'
        else
        echo 'pandas not found'
        fi


        A Github gist of this example can be found here: https://gist.github.com/shaypal5/d505af9953cd86f59c750fa600ee4ba6






        share|improve this answer




















        • this looks really similar to an existing answer
          – Jeff Schaller
          Jul 21 at 17:13






        • 1




          The bash code is similar, the Python code is different, which makes quite a difference. I have read all existing answers before choosing to add my own, and the specific answer you linked to did not work for me. Thus, I find it relevant and valuable to add my answer here. :)
          – ShayPal5
          Jul 22 at 8:18

















        up vote
        0
        down vote













        I used a slightly stricter version of Anthon's answer, for use in a script:



        pip3 list |
        grep -v "^Package *Version$" | grep -v "^-*$" |
        cut -d ' ' -f 1 |
        grep -xF "$package_name"


        The first two greps are intended to strip the header lines from pip3's output.



        The cut then selects only the package names (discarding the version numbers).



        Then the final grep can perform an exact search for the relevant $package_name



        If the package is found, it will display the package name, and return with exit code 0. If you don't want to see the package name, end the command with >/dev/null






        share|improve this answer



























          up vote
          0
          down vote













          You can also use something like this in your scripts.



          python -c "import sys, pkgutil; sys.exit(0 if pkgutil.find_loader('$Package') else 1)"


          What you'd get when a package is not installed.



          Package=psutil
          python3 -c "import sys, pkgutil; sys.exit(0 if pkgutil.find_loader('$Package') else 1)"
          echo $?
          1


          What you'd get when a package is installed.



          Package=requests
          python3 -c "import sys, pkgutil; sys.exit(0 if pkgutil.find_loader('$Package') else 1)"
          echo $?
          0


          Works in python2 and python3, and then you install based on the exit code.





          share




















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            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: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f235230%2fhow-to-check-if-a-module-or-a-package-is-already-installed-in-python3%23new-answer', 'question_page');

            );

            Post as a guest






























            6 Answers
            6






            active

            oldest

            votes








            6 Answers
            6






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote



            accepted










            Type in the shell: pydoc modules .

            This will list modules and you can grep the module which you want.

            Found on stackoverflow here






            share|improve this answer


























              up vote
              3
              down vote



              accepted










              Type in the shell: pydoc modules .

              This will list modules and you can grep the module which you want.

              Found on stackoverflow here






              share|improve this answer
























                up vote
                3
                down vote



                accepted







                up vote
                3
                down vote



                accepted






                Type in the shell: pydoc modules .

                This will list modules and you can grep the module which you want.

                Found on stackoverflow here






                share|improve this answer














                Type in the shell: pydoc modules .

                This will list modules and you can grep the module which you want.

                Found on stackoverflow here







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited May 23 '17 at 12:39









                Community♦

                1




                1










                answered Oct 10 '15 at 17:43









                mazs

                2,5551622




                2,5551622






















                    up vote
                    8
                    down vote













                    You should use pip's list command with grep, that only lists installed packages (not all modules and their neighbours as well):



                    pip list | grep -F package_name





                    share|improve this answer
























                      up vote
                      8
                      down vote













                      You should use pip's list command with grep, that only lists installed packages (not all modules and their neighbours as well):



                      pip list | grep -F package_name





                      share|improve this answer






















                        up vote
                        8
                        down vote










                        up vote
                        8
                        down vote









                        You should use pip's list command with grep, that only lists installed packages (not all modules and their neighbours as well):



                        pip list | grep -F package_name





                        share|improve this answer












                        You should use pip's list command with grep, that only lists installed packages (not all modules and their neighbours as well):



                        pip list | grep -F package_name






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Oct 10 '15 at 18:00









                        Anthon

                        59.3k1798161




                        59.3k1798161




















                            up vote
                            1
                            down vote













                            If the package doesn't do something crazy or time consuming on import you can try actually importing it:





                            if python -c "import package_name" &> /dev/null; then
                            echo 'all good'
                            else
                            echo 'uh oh'
                            fi





                            share|improve this answer
























                              up vote
                              1
                              down vote













                              If the package doesn't do something crazy or time consuming on import you can try actually importing it:





                              if python -c "import package_name" &> /dev/null; then
                              echo 'all good'
                              else
                              echo 'uh oh'
                              fi





                              share|improve this answer






















                                up vote
                                1
                                down vote










                                up vote
                                1
                                down vote









                                If the package doesn't do something crazy or time consuming on import you can try actually importing it:





                                if python -c "import package_name" &> /dev/null; then
                                echo 'all good'
                                else
                                echo 'uh oh'
                                fi





                                share|improve this answer












                                If the package doesn't do something crazy or time consuming on import you can try actually importing it:





                                if python -c "import package_name" &> /dev/null; then
                                echo 'all good'
                                else
                                echo 'uh oh'
                                fi






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Jul 18 '17 at 17:11









                                Dmiters

                                1456




                                1456




















                                    up vote
                                    1
                                    down vote













                                    I have found existing answers incomplete and lacking good enough examples. Here is the solution I have settled on:



                                    # an example checking if the pandas package is installed
                                    if python -c 'import pkgutil; exit(not pkgutil.find_loader("pandas"))'; then
                                    echo 'pandas found'
                                    else
                                    echo 'pandas not found'
                                    fi


                                    A Github gist of this example can be found here: https://gist.github.com/shaypal5/d505af9953cd86f59c750fa600ee4ba6






                                    share|improve this answer




















                                    • this looks really similar to an existing answer
                                      – Jeff Schaller
                                      Jul 21 at 17:13






                                    • 1




                                      The bash code is similar, the Python code is different, which makes quite a difference. I have read all existing answers before choosing to add my own, and the specific answer you linked to did not work for me. Thus, I find it relevant and valuable to add my answer here. :)
                                      – ShayPal5
                                      Jul 22 at 8:18














                                    up vote
                                    1
                                    down vote













                                    I have found existing answers incomplete and lacking good enough examples. Here is the solution I have settled on:



                                    # an example checking if the pandas package is installed
                                    if python -c 'import pkgutil; exit(not pkgutil.find_loader("pandas"))'; then
                                    echo 'pandas found'
                                    else
                                    echo 'pandas not found'
                                    fi


                                    A Github gist of this example can be found here: https://gist.github.com/shaypal5/d505af9953cd86f59c750fa600ee4ba6






                                    share|improve this answer




















                                    • this looks really similar to an existing answer
                                      – Jeff Schaller
                                      Jul 21 at 17:13






                                    • 1




                                      The bash code is similar, the Python code is different, which makes quite a difference. I have read all existing answers before choosing to add my own, and the specific answer you linked to did not work for me. Thus, I find it relevant and valuable to add my answer here. :)
                                      – ShayPal5
                                      Jul 22 at 8:18












                                    up vote
                                    1
                                    down vote










                                    up vote
                                    1
                                    down vote









                                    I have found existing answers incomplete and lacking good enough examples. Here is the solution I have settled on:



                                    # an example checking if the pandas package is installed
                                    if python -c 'import pkgutil; exit(not pkgutil.find_loader("pandas"))'; then
                                    echo 'pandas found'
                                    else
                                    echo 'pandas not found'
                                    fi


                                    A Github gist of this example can be found here: https://gist.github.com/shaypal5/d505af9953cd86f59c750fa600ee4ba6






                                    share|improve this answer












                                    I have found existing answers incomplete and lacking good enough examples. Here is the solution I have settled on:



                                    # an example checking if the pandas package is installed
                                    if python -c 'import pkgutil; exit(not pkgutil.find_loader("pandas"))'; then
                                    echo 'pandas found'
                                    else
                                    echo 'pandas not found'
                                    fi


                                    A Github gist of this example can be found here: https://gist.github.com/shaypal5/d505af9953cd86f59c750fa600ee4ba6







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Jul 21 at 17:05









                                    ShayPal5

                                    111




                                    111











                                    • this looks really similar to an existing answer
                                      – Jeff Schaller
                                      Jul 21 at 17:13






                                    • 1




                                      The bash code is similar, the Python code is different, which makes quite a difference. I have read all existing answers before choosing to add my own, and the specific answer you linked to did not work for me. Thus, I find it relevant and valuable to add my answer here. :)
                                      – ShayPal5
                                      Jul 22 at 8:18
















                                    • this looks really similar to an existing answer
                                      – Jeff Schaller
                                      Jul 21 at 17:13






                                    • 1




                                      The bash code is similar, the Python code is different, which makes quite a difference. I have read all existing answers before choosing to add my own, and the specific answer you linked to did not work for me. Thus, I find it relevant and valuable to add my answer here. :)
                                      – ShayPal5
                                      Jul 22 at 8:18















                                    this looks really similar to an existing answer
                                    – Jeff Schaller
                                    Jul 21 at 17:13




                                    this looks really similar to an existing answer
                                    – Jeff Schaller
                                    Jul 21 at 17:13




                                    1




                                    1




                                    The bash code is similar, the Python code is different, which makes quite a difference. I have read all existing answers before choosing to add my own, and the specific answer you linked to did not work for me. Thus, I find it relevant and valuable to add my answer here. :)
                                    – ShayPal5
                                    Jul 22 at 8:18




                                    The bash code is similar, the Python code is different, which makes quite a difference. I have read all existing answers before choosing to add my own, and the specific answer you linked to did not work for me. Thus, I find it relevant and valuable to add my answer here. :)
                                    – ShayPal5
                                    Jul 22 at 8:18










                                    up vote
                                    0
                                    down vote













                                    I used a slightly stricter version of Anthon's answer, for use in a script:



                                    pip3 list |
                                    grep -v "^Package *Version$" | grep -v "^-*$" |
                                    cut -d ' ' -f 1 |
                                    grep -xF "$package_name"


                                    The first two greps are intended to strip the header lines from pip3's output.



                                    The cut then selects only the package names (discarding the version numbers).



                                    Then the final grep can perform an exact search for the relevant $package_name



                                    If the package is found, it will display the package name, and return with exit code 0. If you don't want to see the package name, end the command with >/dev/null






                                    share|improve this answer
























                                      up vote
                                      0
                                      down vote













                                      I used a slightly stricter version of Anthon's answer, for use in a script:



                                      pip3 list |
                                      grep -v "^Package *Version$" | grep -v "^-*$" |
                                      cut -d ' ' -f 1 |
                                      grep -xF "$package_name"


                                      The first two greps are intended to strip the header lines from pip3's output.



                                      The cut then selects only the package names (discarding the version numbers).



                                      Then the final grep can perform an exact search for the relevant $package_name



                                      If the package is found, it will display the package name, and return with exit code 0. If you don't want to see the package name, end the command with >/dev/null






                                      share|improve this answer






















                                        up vote
                                        0
                                        down vote










                                        up vote
                                        0
                                        down vote









                                        I used a slightly stricter version of Anthon's answer, for use in a script:



                                        pip3 list |
                                        grep -v "^Package *Version$" | grep -v "^-*$" |
                                        cut -d ' ' -f 1 |
                                        grep -xF "$package_name"


                                        The first two greps are intended to strip the header lines from pip3's output.



                                        The cut then selects only the package names (discarding the version numbers).



                                        Then the final grep can perform an exact search for the relevant $package_name



                                        If the package is found, it will display the package name, and return with exit code 0. If you don't want to see the package name, end the command with >/dev/null






                                        share|improve this answer












                                        I used a slightly stricter version of Anthon's answer, for use in a script:



                                        pip3 list |
                                        grep -v "^Package *Version$" | grep -v "^-*$" |
                                        cut -d ' ' -f 1 |
                                        grep -xF "$package_name"


                                        The first two greps are intended to strip the header lines from pip3's output.



                                        The cut then selects only the package names (discarding the version numbers).



                                        Then the final grep can perform an exact search for the relevant $package_name



                                        If the package is found, it will display the package name, and return with exit code 0. If you don't want to see the package name, end the command with >/dev/null







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Jul 7 at 13:48









                                        joeytwiddle

                                        54939




                                        54939




















                                            up vote
                                            0
                                            down vote













                                            You can also use something like this in your scripts.



                                            python -c "import sys, pkgutil; sys.exit(0 if pkgutil.find_loader('$Package') else 1)"


                                            What you'd get when a package is not installed.



                                            Package=psutil
                                            python3 -c "import sys, pkgutil; sys.exit(0 if pkgutil.find_loader('$Package') else 1)"
                                            echo $?
                                            1


                                            What you'd get when a package is installed.



                                            Package=requests
                                            python3 -c "import sys, pkgutil; sys.exit(0 if pkgutil.find_loader('$Package') else 1)"
                                            echo $?
                                            0


                                            Works in python2 and python3, and then you install based on the exit code.





                                            share
























                                              up vote
                                              0
                                              down vote













                                              You can also use something like this in your scripts.



                                              python -c "import sys, pkgutil; sys.exit(0 if pkgutil.find_loader('$Package') else 1)"


                                              What you'd get when a package is not installed.



                                              Package=psutil
                                              python3 -c "import sys, pkgutil; sys.exit(0 if pkgutil.find_loader('$Package') else 1)"
                                              echo $?
                                              1


                                              What you'd get when a package is installed.



                                              Package=requests
                                              python3 -c "import sys, pkgutil; sys.exit(0 if pkgutil.find_loader('$Package') else 1)"
                                              echo $?
                                              0


                                              Works in python2 and python3, and then you install based on the exit code.





                                              share






















                                                up vote
                                                0
                                                down vote










                                                up vote
                                                0
                                                down vote









                                                You can also use something like this in your scripts.



                                                python -c "import sys, pkgutil; sys.exit(0 if pkgutil.find_loader('$Package') else 1)"


                                                What you'd get when a package is not installed.



                                                Package=psutil
                                                python3 -c "import sys, pkgutil; sys.exit(0 if pkgutil.find_loader('$Package') else 1)"
                                                echo $?
                                                1


                                                What you'd get when a package is installed.



                                                Package=requests
                                                python3 -c "import sys, pkgutil; sys.exit(0 if pkgutil.find_loader('$Package') else 1)"
                                                echo $?
                                                0


                                                Works in python2 and python3, and then you install based on the exit code.





                                                share












                                                You can also use something like this in your scripts.



                                                python -c "import sys, pkgutil; sys.exit(0 if pkgutil.find_loader('$Package') else 1)"


                                                What you'd get when a package is not installed.



                                                Package=psutil
                                                python3 -c "import sys, pkgutil; sys.exit(0 if pkgutil.find_loader('$Package') else 1)"
                                                echo $?
                                                1


                                                What you'd get when a package is installed.



                                                Package=requests
                                                python3 -c "import sys, pkgutil; sys.exit(0 if pkgutil.find_loader('$Package') else 1)"
                                                echo $?
                                                0


                                                Works in python2 and python3, and then you install based on the exit code.






                                                share











                                                share


                                                share










                                                answered 1 min ago









                                                R J

                                                49427




                                                49427



























                                                     

                                                    draft saved


                                                    draft discarded















































                                                     


                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function ()
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f235230%2fhow-to-check-if-a-module-or-a-package-is-already-installed-in-python3%23new-answer', 'question_page');

                                                    );

                                                    Post as a guest













































































                                                    Popular posts from this blog

                                                    How to check contact read email or not when send email to Individual?

                                                    Displaying single band from multi-band raster using QGIS

                                                    How many registers does an x86_64 CPU actually have?