提交 16f7e36f 编写于 作者: W wuzewu

Fix Module loading bug

上级 5c967ef7
...@@ -22,6 +22,9 @@ import shutil ...@@ -22,6 +22,9 @@ import shutil
from functools import cmp_to_key from functools import cmp_to_key
import tarfile import tarfile
import sys
import importlib
import inspect
import paddlehub as hub import paddlehub as hub
from paddlehub.common import utils from paddlehub.common import utils
...@@ -54,6 +57,22 @@ class LocalModuleManager(object): ...@@ -54,6 +57,22 @@ class LocalModuleManager(object):
"version"].s "version"].s
return True, info return True, info
else: else:
module_file = os.path.join(module_path, 'module.py')
if os.path.exists(module_file):
basename = os.path.split(module_path)[-1]
dirname = os.path.join(
*list(os.path.split(module_path)[:-1]))
sys.path.insert(0, dirname)
_module = importlib.import_module(
"{}.module".format(basename))
for _item, _cls in inspect.getmembers(
_module, inspect.isclass):
_item = _module.__dict__[_item]
if issubclass(_item, hub.Module):
version = _item._version
break
sys.path.pop(0)
return True, {'version': version}
logger.warning( logger.warning(
"%s does not exist, the module will be reinstalled" % "%s does not exist, the module will be reinstalled" %
desc_pb_path) desc_pb_path)
......
...@@ -169,9 +169,6 @@ class Module(object): ...@@ -169,9 +169,6 @@ class Module(object):
basename = os.path.split(directory)[-1] basename = os.path.split(directory)[-1]
dirname = os.path.join(*list(os.path.split(directory)[:-1])) dirname = os.path.join(*list(os.path.split(directory)[:-1]))
sys.path.insert(0, dirname) sys.path.insert(0, dirname)
# clear module cache
if 'module' in sys.modules:
sys.modules.pop('module')
_module = importlib.import_module("{}.module".format(basename)) _module = importlib.import_module("{}.module".format(basename))
for _item, _cls in inspect.getmembers(_module, inspect.isclass): for _item, _cls in inspect.getmembers(_module, inspect.isclass):
_item = _module.__dict__[_item] _item = _module.__dict__[_item]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册