Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No module named 'custommd' error #2

Closed
Iranb opened this issue Mar 16, 2022 · 2 comments
Closed

No module named 'custommd' error #2

Iranb opened this issue Mar 16, 2022 · 2 comments

Comments

@Iranb
Copy link

Iranb commented Mar 16, 2022

Hi, i'm using the 2.19.0 version of mmdet. when running python ./tools/train.py configs/det/solo/solov2_r50_fpn_coco.py, the model can not import the custommd in the dir .

Traceback (most recent call last):
  File "./tools/train.py", line 190, in <module>
    main()
  File "./tools/train.py", line 92, in main
    cfg = Config.fromfile(args.config)
  File "/home/ubuntu/anaconda3/envs/hyq/lib/python3.7/site-packages/mmcv/utils/config.py", line 337, in fromfile
    import_modules_from_strings(**cfg_dict['custom_imports'])
  File "/home/ubuntu/anaconda3/envs/hyq/lib/python3.7/site-packages/mmcv/utils/misc.py", line 75, in import_modules_from_strings
    import_module(imp)
  File "/home/ubuntu/anaconda3/envs/hyq/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'custommd'

But if I create a test.py

from importlib import import_module
import os


custom_imports = dict(
    imports=[
        "custommd.models.detectors.single_stage_ins",
        "custommd.models.detectors.solov2",
        "custommd.models.solov2.mask_feat_head",
        "custommd.models.solov2.solov2_head",
    ],
    allow_failed_imports=False)



def import_modules_from_strings(imports, allow_failed_imports=False):
    """Import modules from the given list of strings.

    Args:
        imports (list | str | None): The given module names to be imported.
        allow_failed_imports (bool): If True, the failed imports will return
            None. Otherwise, an ImportError is raise. Default: False.

    Returns:
        list[module] | module | None: The imported modules.

    Examples:
        >>> osp, sys = import_modules_from_strings(
        ...     ['os.path', 'sys'])
        >>> import os.path as osp_
        >>> import sys as sys_
        >>> assert osp == osp_
        >>> assert sys == sys_
    """
    if not imports:
        return
    single_import = False
    if isinstance(imports, str):
        single_import = True
        imports = [imports]
    if not isinstance(imports, list):
        raise TypeError(
            f'custom_imports must be a list but got type {type(imports)}')
    imported = []
    for imp in imports:
        if not isinstance(imp, str):
            raise TypeError(
                f'{imp} is of type {type(imp)} and cannot be imported.')
        try:
            imported_tmp = import_module(imp)
            print(os.getcwd())
            print(imp)
        except ImportError:
            if allow_failed_imports:
                warnings.warn(f'{imp} failed to import and is ignored.',
                              UserWarning)
                imported_tmp = None
            else:
                raise ImportError
        imported.append(imported_tmp)
    if single_import:
        imported = imported[0]
    return imported


import_modules_from_strings(**custom_imports)

every thing goes well.
Wondering what cause the error?

@Iranb
Copy link
Author

Iranb commented Mar 16, 2022

Solved! when runing with single gpu , add export PYTHONPATH=$PWD:$PYTHONPATH before

@Iranb Iranb closed this as completed Mar 16, 2022
@lifuguan
Copy link
Owner

Exactly. It's necessary to import our current directory to the PYTHONPATH in any circumstance, which is mentioned in the README(Training and testing part).

Also, Any STAR will be appreciated if this repo helps you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants