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).
Now that I read it again, this seems alright if it's in a module and reusable, otherwise doesn't look like it's less safe than other options that involve nltk.download
and it provides informative output while it gets things, so that's nice.