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

KeyError: 'Command line or configuration file' #389

Open
Tobi-De opened this issue Jan 17, 2023 · 2 comments
Open

KeyError: 'Command line or configuration file' #389

Tobi-De opened this issue Jan 17, 2023 · 2 comments

Comments

@Tobi-De
Copy link

Tobi-De commented Jan 17, 2023

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 #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'
@KJSanchez
Copy link

KJSanchez commented Mar 7, 2023

This solution worked for me. Add this to your .pylintrc. This assumes your pylintrc is in the root directory of your project.

[MASTER]
init-hook="from pylint.config import find_pylintrc; import os, sys; sys.path.append(os.path.dirname(find_pylintrc()))"

@GaNiziolek
Copy link

See this awser, seems to be a similar problem

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