问题
Given one root directory, I need to import all files from all folders (and folders inside other folders) under the root directory.
I figured this could maybe be done with the help of os.walk()
, but no idea how to import the files after "walking" through them.
Is there an easy way to do this?
回答1:
To import a module by name (in 3.4), use importlib.load_module
, which is a "simplifying wrapper around importlib.__import__
". You will see the latter mentioned (as a builtin) in other answers. To be useful, you must, of course, assign each module returned to something -- unless you are importing merely to check syntax or generate .pyc files, as in the compile_all module.
来源:https://stackoverflow.com/questions/27945764/recursively-import-all-py-files-from-all-folders