Search
 
SCRIPT & CODE EXAMPLE
 

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...
")
Comment

PREVIOUS NEXT
Code Example
Python :: python change working directory to file directory 
Python :: Generate random image np array 
Python :: import scipy python 
Python :: how to add icon to tkinter window 
Python :: pandas replace nonetype with empty string 
Python :: python discord bot join voice channel 
Python :: tkinter entry default value 
Python :: python file size 
Python :: matplotlib x label rotation 
Python :: How to perform run-length encoding in Python? 
Python :: pretty print pandas dataframe 
Python :: python loop through files in directory recursively 
Python :: check gpu in tensorflow 
Python :: month from datetime pandas 
Python :: flask boiler plate 
Python :: comment dériver une classe python 
Python :: syntax to update sklearn 
Python :: python time using timeit module 
Python :: pandas concat and reset index 
Python :: open url python 
Python :: django created at field 
Python :: sklearn roc curve 
Python :: mean squared error python 
Python :: python copy file and rename 
Python :: show jpg in jupyter notebook 
Python :: python count the frequency of words in a list 
Python :: how to add list item to text file python 
Python :: rename colmnname in dataframe scala 
Python :: add all string elements in list python 
Python :: learn python the hard way pdf 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =