Skip to content
Related Articles

Related Articles

Making automatic module installer in Python
  • Last Updated : 26 Dec, 2020


Many times the task of installing a module not already available in built-in Python can seem frustrating. This article focuses on removing the task of opening command-line interface and type the pip install module name to download some python modules. In this article, we will try to automate this process.


  • Importing subprocess module to simulate command line using python
  • Importing urllib.request for implementing internet checking facility.
  • Input module name using input() function and initialize module_name variable.
  • Send module name to the main function.
  • In the main function, we first update our pip version directly through python for the smooth functioning of our app.
  • In the next line p=‘pip3 install ‘+module_name) we write pip3 install module_name virtually into the command line.
  • Based on the combination of return code(p) of the above statement and return value of connect() function we can assume things mentioned below.
Return Code(P)Connect FunctionResult
1FalseInternet Off
1TrueInternet is On but some other problem occurred
0TrueModule Installed Successfully
  • Based on the above table we can give the desired output.

Function used:

The connect() function is used for the following purposes:

  1. To check whether internet is on or off.
  2. Reach a specific URL using urllib.request.urlopen(host) command.
    • If reached successfully, return True
    • Else if not reached i.e internet is off, return false.

Given below is the implementation to achieve our functionality using the above approach.



# importing subprocess module
import subprocess
# importing urllib.requests for internet cheking funtions
import urllib.request
# initializing host to gfg.
def connect(host=''):
    # trying to open gfg
        return True
    # trying to catch exception when internet is not ON.
        return False
def main(module_name):
    # updating pip to latest version'python -m pip install --upgrade pip')
    # commanding terminal to pip install
    p ='pip3 install '+module_name)
    # internet off
    if(p.returncode == 1 and connect() == False):
        print("error!! occured check\nInternet conection")
    # Every thing worked fine
    elif(p.returncode == 0):
        print("It worked", module_name, " is installed")
    # Name of module wrong
    elif(p.returncode == 1 and connect() == True):
        print("error!! occured check\nName of module")
module_name = input("Enter the module name: ")


 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :