From e1245f5c846d1f01ea8c338dae5156cd552b64c7 Mon Sep 17 00:00:00 2001 From: Huihuang Zheng Date: Fri, 21 Aug 2020 11:29:27 +0800 Subject: [PATCH] [Dy2stat] change deprecated imp dependency to importlib, test=develop (#26453) As the title --- .../paddle/fluid/dygraph/dygraph_to_static/utils.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/utils.py b/python/paddle/fluid/dygraph/dygraph_to_static/utils.py index 6636bf7c4b..21e05bc6fa 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/utils.py +++ b/python/paddle/fluid/dygraph/dygraph_to_static/utils.py @@ -19,7 +19,6 @@ import astor import atexit import copy import gast -import imp import inspect import os import six @@ -28,6 +27,12 @@ import textwrap from paddle.fluid import unique_name +# imp is deprecated in python3 +if six.PY2: + import imp +else: + from importlib.machinery import SourceFileLoader + dygraph_class_to_static_api = { "CosineDecay": "cosine_decay", "ExponentialDecay": "exponential_decay", @@ -391,7 +396,10 @@ def ast_to_func(ast_root, dyfunc, delete_on_exit=True): atexit.register(lambda: remove_if_exit(f.name)) atexit.register(lambda: remove_if_exit(f.name[:-3] + ".pyc")) - module = imp.load_source(module_name, f.name) + if six.PY2: + module = imp.load_source(module_name, f.name) + else: + module = SourceFileLoader(module_name, f.name).load_module() func_name = dyfunc.__name__ if not hasattr(module, func_name): raise ValueError( -- GitLab