Recursively import all .py files from all folders [duplicate]

旧街凉风 提交于 2019-12-10 14:08:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!