Skip to content

Fix is_installed/get_installed_path crash on dotted package names - #1699

Open
yoarajota wants to merge 1 commit into
open-mmlab:mainfrom
yoarajota:fix/is-installed-dotted-name
Open

yoarajota wants to merge 1 commit into
open-mmlab:mainfrom
yoarajota:fix/is-installed-dotted-name

Conversation

@yoarajota

Copy link
Copy Markdown

importlib.util.find_spec imports the leading component of a dotted module name to resolve it, and raises ModuleNotFoundError instead of returning None when that component isn't importable.

is_installed, get_installed_path, and package2module in mmengine/utils/package_utils.py call find_spec directly on the package name. Fine for a single-level name — find_spec('nonexistentpackage12345') returns None. Not fine for a dotted name whose leading segment isn't installed, e.g. a namespace package like zope.interface:

$ python -c "from mmengine.utils import is_installed; is_installed('zope.interface')"
Traceback (most recent call last):
  ...
ModuleNotFoundError: No module named 'zope'

is_installed should return False here — that's what the distribution() fallback exists for. get_installed_path should raise the PackageNotFoundError it documents, not a ModuleNotFoundError naming a truncated module that was never the argument.

Reachable through Config._get_cfg_path (mmengine/config/config.py:1263), which calls is_installed(package) to give a friendly error when an external config's scope package isn't installed.

Reproduced on current main (f8a8b1b) with Python 3.12.3, command above.

Fix: wrap the find_spec calls in is_installed and package2module with try/except ModuleNotFoundError, same handling as a None return. Added regression tests to tests/test_utils/test_package_utils.py covering both functions with a dotted name whose leading component doesn't exist.

pytest tests/test_utils/test_package_utils.py passes, 2/2. flake8 and isort are clean on the diff.

@CLAassistant

CLAassistant commented Sep 17, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@yoarajota

Copy link
Copy Markdown
Author

@HAOCHENYE could you take a look when you have some time? The two failing checks (ci/circleci: lint and pr_stage_test) fail the same way on the other open PRs, like #1696 and #1698, so I believe they're unrelated to this change. Locally, pytest tests/test_utils/test_package_utils.py, flake8 and isort are clean.

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

Successfully merging this pull request may close these issues.

2 participants