Search code examples
pythonpython-importpython-module

Require packages in my script


I have a python script that makes use of the requests library. I was wondering if there was a more elegant way to go about displaying packages required to run my script other than exception handling on the import statements. Any insight would be greatly appreciated. Thank you!


Solution

  • The packages required to run any python script mention in top of the script by import moduleName.

    you can get a list of installed packages in your python by this:

    import pip
    installed_packages = pip.get_installed_distributions()
    for pack in installed_packages:
        print pack
    

    Use this in top of your code this will install required modules if missing from python.

    try:
        import yourModule
    except ImportError:
        # Do installation process here
        # pip install module ...