pylint-django icon indicating copy to clipboard operation
pylint-django copied to clipboard

KeyError: 'Command line or configuration file'

Open Tobi-De opened this issue 2 years ago • 2 comments

Hello, running the latest version of pylint and pylint-django fails with ModuleNotFoundError: No module named 'config' and KeyError: 'Command line or configuration file'.

This is very similar to https://github.com/PyCQA/pylint-django/issues/370

To reproduce the issue you can clone this repo https://github.com/Tobi-De/maskey Create a virtualenv, install the requirements using the requirements.txt file and run pre-commit or just this command pylint -rn -sn --rcfile=pyproject.toml --django-settings-module=config.settings .

Output of pip freeze

pylint==2.15.10
pylint-django==2.5.3

pyproject.toml

[tool.pylint.MASTER]
load-plugins = "pylint_django"
django-settings-module = "config.settings"pylint -rn -sn --rcfile=pyproject.toml --django-settings-module=config.settings  . 
ignore-paths = ".*/migrations"

This is not particularly relevant since the same thing happens when I run the command from the shell, but I'm adding it just in case.

pre-commit config

  - repo: local
    hooks:
      - id: pylint
        name: pylint
        entry: pylint
        language: system
        types: [ python ]
        args:
          [
            "-rn", # Only display messages
            "-sn", # Don't display the score
            "--rcfile=pyproject.toml"
          ]

Full traceback

Traceback (most recent call last):
  File "/home/tobi/Builds/bft_tasks/venv/lib/python3.11/site-packages/pylint_django/checkers/foreign_key_strings.py", line 92, in open
    django.setup()
  File "/home/tobi/Builds/bft_tasks/venv/lib/python3.11/site-packages/django/__init__.py", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
                      ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tobi/Builds/bft_tasks/venv/lib/python3.11/site-packages/django/conf/__init__.py", line 92, in __getattr__
    self._setup(name)
  File "/home/tobi/Builds/bft_tasks/venv/lib/python3.11/site-packages/django/conf/__init__.py", line 72, in _setup
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/tobi/Builds/bft_tasks/venv/lib/python3.11/site-packages/pylint_django/checkers/foreign_key_strings.py", line 120, in open
    settings.configure(Settings(self.config.django_settings_module))
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tobi/Builds/bft_tasks/venv/lib/python3.11/site-packages/django/conf/__init__.py", line 190, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1128, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1142, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'config'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/tobi/Builds/bft_tasks/venv/bin/pylint", line 8, in <module>
    sys.exit(run_pylint())
             ^^^^^^^^^^^^
  File "/home/tobi/Builds/bft_tasks/venv/lib/python3.11/site-packages/pylint/__init__.py", line 35, in run_pylint
    PylintRun(argv or sys.argv[1:])
  File "/home/tobi/Builds/bft_tasks/venv/lib/python3.11/site-packages/pylint/lint/run.py", line 212, in __init__
    linter.check(args)
  File "/home/tobi/Builds/bft_tasks/venv/lib/python3.11/site-packages/pylint/lint/pylinter.py", line 694, in check
    with self._astroid_module_checker() as check_astroid_module:
  File "/usr/lib64/python3.11/contextlib.py", line 137, in __enter__
    return next(self.gen)
           ^^^^^^^^^^^^^^
  File "/home/tobi/Builds/bft_tasks/venv/lib/python3.11/site-packages/pylint/lint/pylinter.py", line 990, in _astroid_module_checker
    checker.open()
  File "/home/tobi/Builds/bft_tasks/venv/lib/python3.11/site-packages/pylint_django/checkers/foreign_key_strings.py", line 125, in open
    self.add_message(
  File "/home/tobi/Builds/bft_tasks/venv/lib/python3.11/site-packages/pylint/checkers/base_checker.py", line 164, in add_message
    self.linter.add_message(
  File "/home/tobi/Builds/bft_tasks/venv/lib/python3.11/site-packages/pylint/lint/pylinter.py", line 1315, in add_message
    self._add_one_message(
  File "/home/tobi/Builds/bft_tasks/venv/lib/python3.11/site-packages/pylint/lint/pylinter.py", line 1248, in _add_one_message
    self.stats.increase_single_module_message_count(
  File "/home/tobi/Builds/bft_tasks/venv/lib/python3.11/site-packages/pylint/utils/linterstats.py", line 309, in increase_single_module_message_count
    self.by_module[modname][type_name] += increase
    ~~~~~~~~~~~~~~^^^^^^^^^
KeyError: 'Command line or configuration file'

Tobi-De avatar Jan 17 '23 15:01 Tobi-De