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

ModuleNotFound error with pylint-django plugin #7938

Closed
living180 opened this issue Dec 14, 2022 · 0 comments · Fixed by #7940
Closed

ModuleNotFound error with pylint-django plugin #7938

living180 opened this issue Dec 14, 2022 · 0 comments · Fixed by #7940
Labels
Milestone

Comments

@living180
Copy link
Contributor

Bug description

Starting with pylint 2.15, running pylint on a Django project with the pylint_django plugin enabled results in a ModuleNotFound exception. Bisecting points to commit a2c57ec as the one that exposes the issue. The reason the exception occurs is that when _astroid_module_checker() opens the the pylint_django plugin, pylint_django attempts to import the Django settings module. However, the import path has not been fixed up at this point, so the module cannot be found, thus the exception. I already have a fix working on my machine and thus hope to have a PR ready shortly.

Configuration

[MAIN]
load-plugins = pylint_django

Command used

DJANGO_SETTINGS_MODULE=demosite.settings pylint polls/models.py

Pylint output

Using env: /home/dharding/.virtualenvs/demosite
Traceback (most recent call last):
  File "/home/dharding/.local/bin/pylint", line 8, in <module>
    sys.exit(run_pylint())
  File "/home/dharding/.local/venvs/pylint/lib/python3.8/site-packages/pylint/__init__.py", line 35, in run_pylint
    PylintRun(argv or sys.argv[1:])
  File "/home/dharding/.local/venvs/pylint/lib/python3.8/site-packages/pylint/lint/run.py", line 207, in __init__
    linter.check(args)
  File "/home/dharding/.local/venvs/pylint/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 693, in check
    with self._astroid_module_checker() as check_astroid_module:
  File "/usr/lib/python3.8/contextlib.py", line 113, in __enter__
    return next(self.gen)
  File "/home/dharding/.local/venvs/pylint/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 990, in _astroid_module_checker
    checker.open()
  File "/home/dharding/.local/venvs/pylint/lib/python3.8/site-packages/pylint_django/checkers/foreign_key_strings.py", line 92, in open
    django.setup()
  File "/home/dharding/.virtualenvs/demosite/lib/python3.8/site-packages/django/__init__.py", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/home/dharding/.virtualenvs/demosite/lib/python3.8/site-packages/django/conf/__init__.py", line 92, in __getattr__
    self._setup(name)
  File "/home/dharding/.virtualenvs/demosite/lib/python3.8/site-packages/django/conf/__init__.py", line 79, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/dharding/.virtualenvs/demosite/lib/python3.8/site-packages/django/conf/__init__.py", line 190, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'demosite'

Expected behavior

Normal output without crashing, e.g.

Using env: /home/dharding/.virtualenvs/demosite
************* Module polls.models
polls/models.py:24: [E1101(no-member), Choice.was_published_recently] Instance of 'Choice' has no 'pub_date' member

Pylint version

pylint 2.15.8
astroid 2.12.13
Python 3.8.10 (default, Nov 14 2022, 12:59:47)
[GCC 9.4.0]

OS / Environment

Ubuntu 20.04.05

Additional dependencies

pylint-django==2.5.3

@living180 living180 added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Dec 14, 2022
@Pierre-Sassoulas Pierre-Sassoulas added Downstream Bug 🪲 The problem happens in a lib depending on pylint, not pylint and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Dec 14, 2022
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.15.9 milestone Dec 14, 2022
@Pierre-Sassoulas Pierre-Sassoulas added Bug 🪲 and removed Downstream Bug 🪲 The problem happens in a lib depending on pylint, not pylint labels Dec 14, 2022
Pierre-Sassoulas pushed a commit that referenced this issue Dec 14, 2022
Ensure that the import path is fixed up before calling
._astroid_module_checker() so that the pylint_django plugin can
successfully import the Django settings module when its checkers are
initialized.

Closes #7938
github-actions bot pushed a commit that referenced this issue Dec 14, 2022
Ensure that the import path is fixed up before calling
._astroid_module_checker() so that the pylint_django plugin can
successfully import the Django settings module when its checkers are
initialized.

Closes #7938

(cherry picked from commit 491eef5)
Pierre-Sassoulas pushed a commit that referenced this issue Dec 14, 2022
Ensure that the import path is fixed up before calling
._astroid_module_checker() so that the pylint_django plugin can
successfully import the Django settings module when its checkers are
initialized.

Closes #7938

(cherry picked from commit 491eef5)

Co-authored-by: Daniel Harding <[email protected]>
living180 added a commit to living180/pylint that referenced this issue Dec 15, 2022
living180 added a commit to living180/pylint that referenced this issue Dec 15, 2022
living180 added a commit to living180/pylint that referenced this issue Dec 15, 2022
living180 added a commit to living180/pylint that referenced this issue Dec 15, 2022
DanielNoord pushed a commit that referenced this issue Dec 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants