runtime
214 / 500
question 214 Modules & Packages · medium

Which imports would fail?

Before a long batch job starts, it checks a list of the modules it will import, one per line, so it fails in a second instead of after an hour. Lines may carry a version requirement and a comment, as in a requirements file. Report the module names that can't be imported here.

Functionmissing_imports(lines)
Line formata module name (letters, digits, _ and .), optionally followed by a version rule like >=1.2 or ==2.0, extras like [fast], spaces, or a # comment; blank and comment-only lines are skipped
Checkimportlib.util.find_spec(name) is None, or raises ModuleNotFoundError (a dotted name whose package is missing)
Returnsthe missing names, in order, each once; [] when everything can be imported
Herethe folder has a pricing module and a market package (with market.money)
Examples
callmissing_imports(['json', 'csv # reading exports', '', 'shiplabel>=1.2', 'pricing'])returns['shiplabel']
callmissing_imports(['# nothing yet', ' '])returns[]
callmissing_imports(['decimal==1.70', "rateclient ; python_version >= '3.12'", 'zoneinfo'])returns['rateclient']

Loading your progress…

Which imports would fail? — Python practice · Runtime