Skip to content
Snippets Groups Projects
Unverified Commit 9868131c authored by Mashiro's avatar Mashiro Committed by GitHub
Browse files

[Enhance] Enhance error message during custom import (#1102)

parent 1c01594c
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,7 @@ import os ...@@ -5,6 +5,7 @@ import os
import os.path as osp import os.path as osp
import platform import platform
import shutil import shutil
import sys
import tempfile import tempfile
import types import types
import uuid import uuid
...@@ -180,7 +181,15 @@ class Config: ...@@ -180,7 +181,15 @@ class Config:
try: try:
import_modules_from_strings(**cfg_dict['custom_imports']) import_modules_from_strings(**cfg_dict['custom_imports'])
except ImportError as e: except ImportError as e:
raise ImportError('Failed to custom import!') from e err_msg = (
'Failed to import custom modules from '
f"{cfg_dict['custom_imports']}, the current sys.path is: ")
for p in sys.path:
err_msg += f'\n {p}'
err_msg += (
'\nYou should set `PYTHONPATH` to make `sys.path` include '
'the directory which contains your custom module')
raise ImportError(err_msg) from e
return Config( return Config(
cfg_dict, cfg_dict,
cfg_text=cfg_text, cfg_text=cfg_text,
......
...@@ -65,8 +65,14 @@ class TestConfig: ...@@ -65,8 +65,14 @@ class TestConfig:
# If import successfully, os.environ[''TEST_VALUE''] will be # If import successfully, os.environ[''TEST_VALUE''] will be
# set to 'test' # set to 'test'
assert os.environ.pop('TEST_VALUE') == 'test' assert os.environ.pop('TEST_VALUE') == 'test'
sys.path.pop()
Config.fromfile(cfg_file, import_custom_modules=False) Config.fromfile(cfg_file, import_custom_modules=False)
assert 'TEST_VALUE' not in os.environ assert 'TEST_VALUE' not in os.environ
sys.modules.pop('test_custom_import_module')
with pytest.raises(
ImportError, match='Failed to import custom modules from'):
Config.fromfile(cfg_file, import_custom_modules=True)
@pytest.mark.parametrize('file_format', ['py', 'json', 'yaml']) @pytest.mark.parametrize('file_format', ['py', 'json', 'yaml'])
def test_fromstring(self, file_format): def test_fromstring(self, file_format):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment