Skip to content

Editing Django settings.py causes django.core.exceptions.ImproperlyConfigured #362

Open
@tomaz-suller

Description

@tomaz-suller

When editing the Django project's settings.py, when calling pylint we get a Django error message saying the config was not properly set, even though we correctly pass the flag --django-settings-module.

The problem is particular to modifications to settings.py, since modifications to any other file are still correctly processed by pylint.

Steps to reproduce:

  1. Install pylint and pylint-django
  2. Open a Django app (I cannot supply code for mine since it's closed source)
  3. Edit the settings app; in our tests, we did
INSTALLED_APPS = [
...
"django_extensions",
]

(installing django-extensions is not required though)
4. Run pylint with pylint-django

pylint --load-plugins=pylint_django --django-settings-module=myapp.settings

Traceback

Traceback (most recent call last):
  File "/mnt/ubuntu_home/tomaz/git/Estágio/Scala/scala_alocacao/venv/lib/python3.10/site-packages/pylint_django/checkers/foreign_key_strings.py", line 92, in open
    django.setup()
  File "/mnt/ubuntu_home/tomaz/git/Estágio/Scala/scala_alocacao/venv/lib/python3.10/site-packages/django/__init__.py", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/mnt/ubuntu_home/tomaz/git/Estágio/Scala/scala_alocacao/venv/lib/python3.10/site-packages/django/conf/__init__.py", line 82, in __getattr__
    self._setup(name)
  File "/mnt/ubuntu_home/tomaz/git/Estágio/Scala/scala_alocacao/venv/lib/python3.10/site-packages/django/conf/__init__.py", line 63, 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 "/mnt/ubuntu_home/tomaz/git/Estágio/Scala/scala_alocacao/venv/lib/python3.10/site-packages/pylint_django/checkers/foreign_key_strings.py", line 120, in open
    settings.configure(Settings(self.config.django_settings_module))
  File "/mnt/ubuntu_home/tomaz/git/Estágio/Scala/scala_alocacao/venv/lib/python3.10/site-packages/django/conf/__init__.py", line 170, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 992, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'scala_alocacao'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/mnt/ubuntu_home/tomaz/git/Estágio/Scala/scala_alocacao/venv/bin/pylint", line 8, in <module>
    sys.exit(run_pylint())
  File "/mnt/ubuntu_home/tomaz/git/Estágio/Scala/scala_alocacao/venv/lib/python3.10/site-packages/pylint/__init__.py", line 22, in run_pylint
    PylintRun(argv or sys.argv[1:])
  File "/mnt/ubuntu_home/tomaz/git/Estágio/Scala/scala_alocacao/venv/lib/python3.10/site-packages/pylint/lint/run.py", line 358, in __init__
    linter.check(args)
  File "/mnt/ubuntu_home/tomaz/git/Estágio/Scala/scala_alocacao/venv/lib/python3.10/site-packages/pylint/lint/pylinter.py", line 1073, in check
    self._check_files(
  File "/mnt/ubuntu_home/tomaz/git/Estágio/Scala/scala_alocacao/venv/lib/python3.10/site-packages/pylint/lint/pylinter.py", line 1108, in _check_files
    with self._astroid_module_checker() as check_astroid_module:
  File "/usr/lib/python3.10/contextlib.py", line 135, in __enter__
    return next(self.gen)
  File "/mnt/ubuntu_home/tomaz/git/Estágio/Scala/scala_alocacao/venv/lib/python3.10/site-packages/pylint/lint/pylinter.py", line 1236, in _astroid_module_checker
    checker.open()
  File "/mnt/ubuntu_home/tomaz/git/Estágio/Scala/scala_alocacao/venv/lib/python3.10/site-packages/pylint_django/checkers/foreign_key_strings.py", line 125, in open
    self.add_message(
  File "/mnt/ubuntu_home/tomaz/git/Estágio/Scala/scala_alocacao/venv/lib/python3.10/site-packages/pylint/checkers/base_checker.py", line 112, in add_message
    self.linter.add_message(
  File "/mnt/ubuntu_home/tomaz/git/Estágio/Scala/scala_alocacao/venv/lib/python3.10/site-packages/pylint/lint/pylinter.py", line 1631, in add_message
    self._add_one_message(
  File "/mnt/ubuntu_home/tomaz/git/Estágio/Scala/scala_alocacao/venv/lib/python3.10/site-packages/pylint/lint/pylinter.py", line 1564, in _add_one_message
    self.stats.increase_single_module_message_count(
  File "/mnt/ubuntu_home/tomaz/git/Estágio/Scala/scala_alocacao/venv/lib/python3.10/site-packages/pylint/utils/linterstats.py", line 300, in increase_single_module_message_count
    self.by_module[modname][type_name] += increase
KeyError: '/mnt/ubuntu_home/tomaz/git/Estágio/Scala/scala_alocacao/pylintrc'

System configuration

pip freeze

asgiref==3.5.1
astroid==2.11.4
black==22.3.0
cfgv==3.3.1
click==8.1.3
dill==0.3.4
distlib==0.3.4
Django==3.2.13
django-phonenumber-field==5.2.0
django-smart-selects==1.5.9
filelock==3.6.0
identify==2.5.0
isort==5.10.1
lazy-object-proxy==1.7.1
mccabe==0.7.0
mypy-extensions==0.4.3
mysqlclient==2.0.3
nodeenv==1.6.0
numpy==1.21.6
pathspec==0.9.0
phonenumbers==8.12.26
Pillow==9.1.0
platformdirs==2.5.2
pre-commit==2.16.0
pylint==2.13.0
pylint-django==2.5.3
pylint-plugin-utils==0.7
python-dateutil==2.8.2
pytz==2022.1
PyYAML==6.0
reportlab==3.6.2
six==1.16.0
sqlparse==0.4.2
toml==0.10.2
tomli==2.0.1
virtualenv==20.14.1
wrapt==1.14.1

pylintrc

[MASTER]

fail-on=E
fail-under=7.0
load-plugins=pylint_django
py-version=3.8

[MESSAGES CONTROL]

disable=missing-module-docstring,
        missing-class-docstring,
        missing-function-docstring

[REPORTS]

output-format=colorized
reports=no
score=yes

[BASIC]

good-names=i,
           j,
           k,
           ex,
           obj,
           Run,
           _

[DJANGO FOREIGN KEYS REFERENCED BY STRINGS]

django-settings-module=scala_alocacao.settings

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