Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python auto module installer

def install_missing_modules():
    """
    Purpose: This function will check all the installed modules
    If anything is missing, it will install them only
    """
    try:
        import platform
        import traceback
        import subprocess
        import sys
        try:
            from pip._internal.operations import freeze
        except ImportError:
            # pip < 10.0
            from pip.operations import freeze
        # NULL output device for disabling print output of pip installs
        try:
            from subprocess import DEVNULL  # py3k
        except ImportError:
            import os
            DEVNULL = open(os.devnull, 'wb')

        """ Mention the modules needed for your apps here in req_list.
        	you can also read the list from a requirements.txt"""
        req_list = ["requests", "colorama", "python-dateutil", "pytz"]
        freeze_list = freeze.freeze()
        alredy_installed_list = []
        for p in freeze_list:
            name = p.split("==")[0]
            if "@" not in name:
                # '@' symbol appears in some python modules in Windows
                alredy_installed_list.append(str(name).lower())

        # installing any missing modules
        installed = False
        error = False
        for module_name in req_list:
            if module_name.lower() not in alredy_installed_list:
                try:
                    print("module_installer: Installing module: %s" % module_name)
                    subprocess.check_call([sys.executable, "-m", "pip", "install", "--trusted-host=pypi.org", "--trusted-host=files.pythonhosted.org", module_name], stderr=DEVNULL, stdout=DEVNULL, )
                    print("module_installer: Installed missing module: %s" % module_name)
                    installed = True
                except:
                    print("module_installer: Failed to install module: %s" % module_name)
                    error = True

        if not installed and not error:
            print("All required modules are already installed. Continuing...
")

    except:
        traceback.print_exc()
        print("Failed to install missing modules...
")
 
PREVIOUS NEXT
Tagged: #python #auto #module #installer
ADD COMMENT
Topic
Name
4+2 =