Skip to content

Conversation

ProGamerGov
Copy link
Contributor

@ProGamerGov ProGamerGov commented Aug 13, 2022

If the module import path used by users does not match the directory / file structure, import_lib's import_module function cannot find it.

Below is a basic example of where this failure occurs:

package/
    `__init__.py` # import package.subpackage1 as subpackage1
    subpackage1/
        `__init__.py` # from package.subpackage1.subpackage2 import module_1
        subpackge2/
            `__init__.py`
            module_1.py

Using the above directory / file structure, users import module1 via:

import package.subpackage1.module1

Sphinx is generally able to handle this sort of structure, except for the viewcode extension that uses the get_full_modname function from here: https://github.com/sphinx-doc/sphinx/blob/5.x/sphinx/util/__init__.py

That function specifically causes an error with the above example when importlib's import_module is run:

from importlib import import_module
module = import_module("package.subpackage1.module1") # Fails
# ModuleNotFoundError: No module named 'package.subpackage1.module1'

The exact path to the module however has no error:

from importlib import import_module
module = import_module("package.subpackage1.subpackage2.module1") # Works

Feature / Bugfix

This PR is my attempt at fixing the above import_module error by locating the exact path to the module, which is then used with import_module.

Currently this issue can be seen on projects where the viewcode extension is enabled, yet no source code link is created. Example: https://captum.ai/api/utilities.html#visualization

My solution utilizes importlib's __import__ function to find the directory module path to the object.

@ProGamerGov ProGamerGov changed the title Improve the Python module path finder inside get_full_modname Improve the ViewCode extension's Python module path finder inside get_full_modname Aug 13, 2022
@ProGamerGov ProGamerGov changed the title Improve the ViewCode extension's Python module path finder inside get_full_modname Viewcode: Support import paths that differ from the directory structure Aug 13, 2022
@ProGamerGov
Copy link
Contributor Author

@AA-Turner, @tk0miya let me know if I need to change anything for this PR to be merged

@ProGamerGov
Copy link
Contributor Author

@danieleades The conflicts have been resolved, so this PR is ready for merging

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants