Skip to content

"Django was not configured" since latest update #306

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

Closed
GBrachetta opened this issue Jan 17, 2021 · 9 comments
Closed

"Django was not configured" since latest update #306

GBrachetta opened this issue Jan 17, 2021 · 9 comments

Comments

@GBrachetta
Copy link

GBrachetta commented Jan 17, 2021

Since the last update I get the error:

Django was not configured. For more information runpylint --load-plugins=pylint_django --help-msg=django-not-configured

First time I see it, no other settings changed.
I use pipenv, and I'm on VSCode.
My pip freeze:

appdirs==1.4.4
asgiref==3.3.1
astroid==2.4.2
black==20.8b1
click==7.1.2
Django==3.1.5
djangorestframework==3.12.2
flake8==3.8.4
isort==5.7.0
lazy-object-proxy==1.4.3
mccabe==0.6.1
mypy-extensions==0.4.3
pathspec==0.8.1
pycodestyle==2.6.0
pyflakes==2.2.0
pylint==2.6.0
pylint-django==2.4.2
pylint-plugin-utils==0.6
pytz==2020.5
regex==2020.11.13
six==1.15.0
sqlparse==0.4.1
toml==0.10.2
typed-ast==1.4.2
typing-extensions==3.7.4.3
wrapt==1.12.1

And my settings.json for pylint are:

"python.linting.pylintArgs": [
    "--load-plugins",
    "pylint_django",
    "--disable=C0114, C0115, W0222",
    "--disable=imported-auth-user",
    "--disable=invalid-name",
    "--disable=line-too-long"
],

Am I doing anything wrong here?

@carlio
Copy link
Member

carlio commented Jan 17, 2021

The error message says "Django was not configured. For more information run pylint --load-plugins=pylint_django --help-msg=django-not-configured"

Did you run pylint --load-plugins=pylint_django --help-msg=django-not-configured?

@GBrachetta
Copy link
Author

GBrachetta commented Jan 17, 2021

I did but I get the text below. I'm a bit lost since this is the first time it happens and only since the last update...

:django-not-configured (E5110): *Django was not configured. For more information runpylint --load-plugins=pylint_django --help-msg=django-not-configured*
  Finding foreign-key relationships from strings in pylint-django requires
  configuring Django. This can be done via the DJANGO_SETTINGS_MODULE
  environment variable or the pylint option django-settings-module, eg: `pylint
  --load-plugins=pylint_django --django-settings-module=myproject.settings` .
  This can also be set as an option in a .pylintrc configuration file. Some
  basic default settings were used, however this will lead to less accurate
  linting. Consider passing in an explicit Django configuration file to match
  your project to improve accuracy. This message belongs to the django foreign
  keys referenced by strings checker.

I always ran it with no problems with my settings as described above from settings.json (vscode)

@GBrachetta
Copy link
Author

By the way I can confirm I don't get the error if I install v.2.3.0. It just happens with the latest release

@carlio
Copy link
Member

carlio commented Jan 17, 2021

The error message, the error message explanation and the documentation all tell you exactly what to do: https://github.com/PyCQA/pylint-django#usage

@GBrachetta
Copy link
Author

Okay, I'm not managing. Don't worry, I'll stay with 2.3.0

@Routhinator
Copy link

Routhinator commented Jan 25, 2021

I'm not quite grokking what has changed since the update, I updated my commands as per the docs, and now pylint-django is complaining about LOGGING_CONFIG?

Is pylint now trying to run Django @carlio ? Because that's a much more significant change...

I'm confused why it needs this.

pylint --load-plugins=pylint_django --django-settings-module=theden_django.settings theden_django/
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/pylint_django/checkers/foreign_key_strings.py", line 90, in open
    django.setup()
  File "/usr/local/lib/python3.7/dist-packages/django/__init__.py", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/usr/local/lib/python3.7/dist-packages/django/conf/__init__.py", line 82, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python3.7/dist-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.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/pylint_django/checkers/foreign_key_strings.py", line 114, in open
    django.setup()
  File "/usr/local/lib/python3.7/dist-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python3.7/dist-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/usr/local/lib/python3.7/dist-packages/django/apps/config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/builds/routhio/theden/django/theden_django/members/models.py", line 10, in <module>
    from versatileimagefield.fields import VersatileImageField, PPOIField
  File "/usr/local/lib/python3.7/dist-packages/versatileimagefield/fields.py", line 10, in <module>
    from .files import VersatileImageFieldFile, VersatileImageFileDescriptor
  File "/usr/local/lib/python3.7/dist-packages/versatileimagefield/files.py", line 8, in <module>
    from .mixins import VersatileImageMixIn
  File "/usr/local/lib/python3.7/dist-packages/versatileimagefield/mixins.py", line 5, in <module>
    from .datastructures import FilterLibrary
  File "/usr/local/lib/python3.7/dist-packages/versatileimagefield/datastructures/__init__.py", line 3, in <module>
    from .sizedimage import SizedImage
  File "/usr/local/lib/python3.7/dist-packages/versatileimagefield/datastructures/sizedimage.py", line 7, in <module>
    from ..utils import get_resized_path
  File "/usr/local/lib/python3.7/dist-packages/versatileimagefield/utils.py", line 5, in <module>
    import magic
  File "/usr/local/lib/python3.7/dist-packages/magic.py", line 201, in <module>
    raise ImportError('failed to find libmagic.  Check your installation')
ImportError: failed to find libmagic.  Check your installation
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/local/bin/pylint", line 8, in <module>
    sys.exit(run_pylint())
  File "/usr/local/lib/python3.7/dist-packages/pylint/__init__.py", line 22, in run_pylint
    PylintRun(sys.argv[1:])
  File "/usr/local/lib/python3.7/dist-packages/pylint/lint/run.py", line 349, in __init__
    linter.check(args)
  File "/usr/local/lib/python3.7/dist-packages/pylint/lint/pylinter.py", line 863, in check
    self.get_ast, self._iterate_file_descrs(files_or_modules)
  File "/usr/local/lib/python3.7/dist-packages/pylint/lint/pylinter.py", line 894, in _check_files
    with self._astroid_module_checker() as check_astroid_module:
  File "/usr/lib/python3.7/contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "/usr/local/lib/python3.7/dist-packages/pylint/lint/pylinter.py", line 1004, in _astroid_module_checker
    checker.open()
  File "/usr/local/lib/python3.7/dist-packages/pylint_django/checkers/foreign_key_strings.py", line 120, in open
    args=self.config.django_settings_module,
  File "/usr/local/lib/python3.7/dist-packages/pylint/checkers/base_checker.py", line 111, in add_message
    self.linter.add_message(msgid, line, node, args, confidence, col_offset)
  File "/usr/local/lib/python3.7/dist-packages/pylint/message/message_handler_mix_in.py", line 237, in add_message
    message_definition, line, node, args, confidence, col_offset
  File "/usr/local/lib/python3.7/dist-packages/pylint/message/message_handler_mix_in.py", line 290, in add_one_message
    self.stats["by_module"][self.current_name][msg_cat] += 1
KeyError: None

@Routhinator
Copy link

Ah, it now needs my runtime deps. I guess it's trying to introspect more of the modules?

@atodorov
Copy link
Contributor

Is pylint now trying to run Django @carlio ? Because that's a much more significant change...

It is not. It's doing django.setup() and Django is complaining of some missing settings which it thinks it needs.

@graingert
Copy link

graingert commented Jun 11, 2021

I worked around this using an init-hook

# pyproject.toml

[tool.pylint.MASTER]
# see https://github.com/PyCQA/pylint-django/issues/306#issuecomment-859515591
init-hook = "import runpy; runpy.run_path('./top_level_package/django_project/pylint_init_hook.py');'"
# ./top_level_package/django_project/pylint_init_hook.py
from __future__ import annotations

import os


def _configure_pylint_django():
    os.environ["DJANGO_SETTINGS_MODULE"] = "top_level_package.django_project.settings"
    os.environ["DJANGO_CONFIGURATION"] = "Dev"

    from configurations import importer  # type: ignore[import]

    importer.install()


if __name__ == "<run_path>":
    _configure_pylint_django()

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

5 participants