deployment - How to check if all modules imported by a Python script are installed without running the script? -


i check if modules imported script installed before run script, because script quite complex , running many hours. also, may import different modules depending on options passed it, running once may not check everything. so, wouldn't run script on new system few hours see failing before completion because of missing module.

apparently, pyflakes , pychecker not helpful here, correct me if i'm wrong. can this:

$ python -c "$(cat *.py|grep import|sed 's/^\s\+//g'|tr '\n' ';')" 

but it's not robust, break if word 'import' appears in string, example.

so, how can task properly?

you use modulefinder standard lib modulefinder using example docs

from modulefinder import modulefinder finder = modulefinder() finder.run_script('bacon.py')  print 'loaded modules:' name, mod in finder.modules.iteritems():     print '%s: ' % name,     print ','.join(mod.globalnames.keys()[:3])  print '-'*50 print 'modules not imported:' print '\n'.join(finder.badmodules.iterkeys()) 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -