pylint-dev / pylint-django

Pylint plugin for improving code analysis for when using Django
Other
595 stars 117 forks source link

ImproperlyConfigured exception about LOGGING_CONFIG #309

Open sylvain1811 opened 3 years ago

sylvain1811 commented 3 years ago

I'm getting the following error when I run pylint on my project using pylint-django==2.4.2:

> poetry run pylint --django-settings-module=myapp.settings myapp
Traceback (most recent call last):
  File "/home/sylvain/LNS/mylns-api/.venv/lib/python3.7/site-packages/pylint_django/checkers/foreign_key_strings.py", line 90, in open
    django.setup()
  File "/home/sylvain/LNS/mylns-api/.venv/lib/python3.7/site-packages/django/__init__.py", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/home/sylvain/LNS/mylns-api/.venv/lib/python3.7/site-packages/django/conf/__init__.py", line 82, in __getattr__
    self._setup(name)
  File "/home/sylvain/LNS/mylns-api/.venv/lib/python3.7/site-packages/django/conf/__init__.py", line 67, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
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.

I have a .pylintrc file loading pylint-django, and I think I correctly use --django-settings-module=myapp.settings command line argument. I also tried to add django-settings-module=myapp.settings in my .pylintrc, still failed.

I've read comments on #306, especially this one and I think it's an issue of pylint-django itself and not about configuration, so I create this issue.

Workaround: downgrade to 2.3.0

dragonpaw commented 3 years ago

For what it's worth, I'm having this happen too, but only if I use pylint's --django-settings-module. If I set DJANGO_SETTINGS_MODULE first, it goes away. And I looked at #306 and I'm pretty sure he was having the same issue.

# pylint --load-plugins pylint_django --django-settings-module=config.settings.base ticketing
Running insecure for localhost.
Traceback (most recent call last):
  File "/Users/ash/.pyenv/versions/django/lib/python3.9/site-packages/pylint_django/checkers/foreign_key_strings.py", line 90, in open
    django.setup()
  File "/Users/ash/.pyenv/versions/django/lib/python3.9/site-packages/django/__init__.py", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/Users/ash/.pyenv/versions/django/lib/python3.9/site-packages/django/conf/__init__.py", line 79, in __getattr__
    self._setup(name)
  File "/Users/ash/.pyenv/versions/django/lib/python3.9/site-packages/django/conf/__init__.py", line 60, 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.
...

But...

# DJANGO_SETTINGS_MODULE=config.settings.base pylint --load-plugins pylint_django ticketing
************* Module ticketing
ticketing/__init__.py:4:19: R1728: Consider using a generator instead 'tuple(int(num) if num.isdigit() else num for num in __version__.replace('-', '.', 1).split('.'))' (consider-using-generator)
...

So it's not that my Django config is broken, it's not. The app runs fine. It's something specific with how init is handled with the cli option vs. the environment variable. Hope this helps you track it down...

Routhinator commented 3 years ago

Confirmed, downgrading to 2.3.0 fixed this for me as well.

Using the ENV variable instead of the parameter as @dragonpaw did; did not

twig commented 3 years ago

Cheers @Routhinator , downgrading saved my sanity