0

As part of the testing regime for building a python application [a Jupyter notebook, in this instance], I want to test that all the modules installed in the system will actually load.

import module from string variable pointed me to build the following code:

    import re
    import subprocess

    s_counter = 0
    f_counter = 0
    errors = []
    libraries = []

    output =  subprocess.run(['conda', 'list'], stdout=subprocess.PIPE).stdout.decode('utf-8').splitlines()
    for line in output:
        if not re.match('\s*#', line):
            words = re.split('\s+', line)
            if words[0] != 'python':
                libraries.append(words[0])
    for lib in libraries:
        try:
            lib_obj = __import__(lib)
            globals()[lib] = lib_obj
            s_counter += 1
        except ImportError:
            errors.append("ERROR: missing python library: " + lib)
            f_counter += 1
    print(f"Successfuly loaded {s_counter} libraries, failed to load {f_counter}")
    print(f"{errors}")

The problem with this simplistic solution is that it assumes the module name is the same as the import name.... and often it isn't.

For example: .... install beautifulsoup4 and from bs4 import BeautifulSoup

So the question is, how do I find out the import string for a module... programatically?

hem
  • 1,012
  • 6
  • 11
CodeGorilla
  • 811
  • 1
  • 6
  • 21
  • 2
    How about you just surround the import statement for the module or package with `try` / `except` and forget about the `conda list` call? – Klaus D. Jul 02 '19 at 11:05
  • this: https://stackoverflow.com/questions/1707709/list-all-the-modules-that-are-part-of-a-python-package/1707786 and this: https://stackoverflow.com/questions/487971/is-there-a-standard-way-to-list-names-of-python-modules-in-a-package might help you; you can have a list of the modules in a package and then problematically import each – Petronella Jul 02 '19 at 11:46
  • @KlausD.- the import is already wrapped in a try-block.... however that's not my problem: I want to work out how to import a library when the import statement is not the same as the library name (see the `beautifulsoup4` example) – CodeGorilla Jul 04 '19 at 10:49

0 Answers0