Skip to content

ImportedModule always assumes paths to be relative to current resource #3

@mcepl

Description

@mcepl

I'm using rope inside SublimeRope and have a project that consists of several namespaces, all available on python path. While rope properly indexes those, it cannot resolve ImportedModule nodes pointing from one package to another as with a non-zero level only a relative import will be attempted.

I have changed the local implementation to instead try relative first and fall back to absolute if needed:

class ImportedModule(PyName):

    # ...

    def _get_pymodule(self):
        if self.pymodule.get() is None:
            pycore = self.importing_module.pycore
            if self.resource is not None:
                self.pymodule.set(pycore.resource_to_pyobject(self.resource))
            elif self.module_name is not None:
                try:
                    pymodule = None
                    if self.level > 0:
                        try:
                            pymodule = pycore.get_relative_module(
                                self.module_name, self._current_folder(),
                                self.level)
                        except exceptions.ModuleNotFoundError:
                            pass
                    if pymodule is None:
                        pymodule = pycore.get_module(self.module_name,
                                                     self._current_folder())
                    self.pymodule.set(pymodule)
                except exceptions.ModuleNotFoundError:
                    pass
        return self.pymodule.get()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions