Questions tagged «dynamic-import»

9
从字符串动态导入文件中的方法
我有一个字符串,说:abc.def.ghi.jkl.myfile.mymethod。如何动态导入mymethod? 这是我的处理方法: def get_method_from_file(full_path): if len(full_path) == 1: return map(__import__,[full_path[0]])[0] return getattr(get_method_from_file(full_path[:-1]),full_path[-1]) if __name__=='__main__': print get_method_from_file('abc.def.ghi.jkl.myfile.mymethod'.split('.')) 我想知道是否完全需要导入单个模块。 编辑:我正在使用Python版本2.6.5。
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.