Skip to main content
1 of 2
ferada
  • 11.1k
  • 25
  • 63

I mean I'm not a fan of it since it's basically just a list of statements to execute, but then again, the solution via globals and importlib doesn't look that much better:

import importlib
import nltk

req_modules = {'nltk.punkt': 'punkt',
               'nltk.corpus.stopwords': 'stopwords',
               'nltk.pos_tag': 'averaged_perceptron_tagger',
               'nltk.ne_chunk': 'maxent_ne_chunker'}

def try_load(module, name):
    print("Trying to load: '%s'" % module)
    globals()[name] = importlib.import_module(module)
    print("Success.")

for module, name in req_modules.items():
    try:
        try_load(module, name)
    except (LookupError, ImportError):
        # if data not found (not already installed), download it
        print("Tried to load: '%s'. Resource '%s' was not available \
               and is being downloaded.\n" % (module, name))
        nltk.download(name)
        try_load(module, name)

N.b. I'd say use a list of module/name pairs instead to enforce the order to make diagnostics a bit easier (or load dependencies for one of the later modules first).

ferada
  • 11.1k
  • 25
  • 63