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

TooManyLevelsError: Relative import with too many levels (n) for module #1113

Closed
NikolayVasilyev opened this issue Jul 29, 2021 · 2 comments
Closed
Milestone

Comments

@NikolayVasilyev
Copy link

Reproduce

  1. Use Sublime Text editor, install Anaconda package, install pylint and astroid with pip3
  2. Write code with relative imports, like ("from .my_submodule import stuff")
  3. Open the file with ST3

Current behavior

When I open sublime text editor with a python code with relative import, I see the following traceback from linter:

Traceback (most recent call last):
  File "/usr/local/bin/pylint", line 8, in <module>
    sys.exit(run_pylint())
  File "/usr/local/lib/python3.9/dist-packages/pylint/__init__.py", line 24, in run_pylint
    PylintRun(sys.argv[1:])
  File "/usr/local/lib/python3.9/dist-packages/pylint/lint/run.py", line 384, in __init__
    linter.check(args)
  File "/usr/local/lib/python3.9/dist-packages/pylint/lint/pylinter.py", line 975, in check
    self._check_files(
  File "/usr/local/lib/python3.9/dist-packages/pylint/lint/pylinter.py", line 1009, in _check_files
    self._check_file(get_ast, check_astroid_module, name, filepath, modname)
  File "/usr/local/lib/python3.9/dist-packages/pylint/lint/pylinter.py", line 1035, in _check_file
    check_astroid_module(ast_node)
  File "/usr/local/lib/python3.9/dist-packages/pylint/lint/pylinter.py", line 1172, in check_astroid_module
    retval = self._check_astroid_module(
  File "/usr/local/lib/python3.9/dist-packages/pylint/lint/pylinter.py", line 1217, in _check_astroid_module
    walker.walk(ast_node)
  File "/usr/local/lib/python3.9/dist-packages/pylint/utils/ast_walker.py", line 77, in walk
    self.walk(child)
  File "/usr/local/lib/python3.9/dist-packages/pylint/utils/ast_walker.py", line 74, in walk
    callback(astroid)
  File "/usr/local/lib/python3.9/dist-packages/pylint/checkers/deprecated.py", line 112, in visit_importfrom
    basename = get_import_name(node, basename)
  File "/usr/local/lib/python3.9/dist-packages/pylint/checkers/utils.py", line 1552, in get_import_name
    modname = root.relative_to_absolute_name(
  File "/usr/local/lib/python3.9/dist-packages/astroid/scoped_nodes.py", line 725, in relative_to_absolute_name
    raise TooManyLevelsError(level=level, name=self.name)
astroid.exceptions.TooManyLevelsError: Relative import with too many levels (1) for module 'rating'

Notes

If I manually change file: /usr/local/lib/python3.9/dist-packages/astroid/scoped_nodes.py from this:

724             if level and self.name.count(".") < level:
725                raise TooManyLevelsError(level=level, name=self.name)

to this:

724            if level and self.name.count(".") < level:
725                pass

Than everything goes fine, but linter says:
Unable to import '<cur_file>.<module>' (import-error)
But should be: `<top_level_dir_name>.

python -c "from astroid import __pkginfo__; print(__pkginfo__.version)" output

~$ python3 -c "from astroid import __pkginfo__; print(__pkginfo__.version)"
2.6.5

Thanks.

@rublag
Copy link

rublag commented Jul 30, 2021

It seems like the bug is in pylint. I've created an issue here: pylint-dev/pylint#4775

@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.6.6 milestone Jul 30, 2021
@Pierre-Sassoulas Pierre-Sassoulas changed the title TooManyLevelsError TooManyLevelsError: Relative import with too many levels (n) for module Aug 1, 2021
@Pierre-Sassoulas Pierre-Sassoulas modified the milestones: 2.6.6, 2.6.7, 2.7.0 Aug 3, 2021
@Pierre-Sassoulas
Copy link
Member

This was fixed in pylint in pylint-dev/pylint#4848

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

3 participants