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

Failure to import settings?

Open bschollnick opened this issue 3 years ago • 7 comments

I am running into several issues...

But here's my structure:

├── pycache ├── django_jinja │   ├── pycache │   ├── builtins │   ├── contrib │   ├── management │   └── views ├── frontend │   ├── pycache │   ├── management │   └── testcode ├── numpy_git │   ├── benchmarks │   ├── branding │   ├── changelog │   ├── doc │   ├── numpy │   └── tools ├── quickbbs │   ├── pycache │   ├── middleware │   └── migrations ├── static │   ├── admin │   ├── css │   ├── debug_toolbar │   ├── fonts │   ├── grappelli │   ├── javascript │   ├── silk │   └── thumbnails └── templates ├── allauth └── frontend

` If I use the following, and my settings file does exist as quickbbs/settings.py

export DJANGO_SETTINGS_MODULE=quickbbs.settings
pylint --load-plugins pylint_django --django-settings-module=quickbbs.settings $1 $2 $3 $4

But it exceptions with an error indicating that pylint-django can't find the settings file?

(quickbbs) benjamin@Nerv quickbbs % djangolint.command frontend/ftypes.py
Traceback (most recent call last):
  File "/Users/benjamin/venvs/quickbbs/bin/pylint", line 8, in <module>
    sys.exit(run_pylint())
  File "/Users/benjamin/venvs/quickbbs/lib/python3.9/site-packages/pylint/__init__.py", line 24, in run_pylint
    PylintRun(sys.argv[1:])
  File "/Users/benjamin/venvs/quickbbs/lib/python3.9/site-packages/pylint/lint/run.py", line 381, in __init__
    linter.check(args)
  File "/Users/benjamin/venvs/quickbbs/lib/python3.9/site-packages/pylint/lint/pylinter.py", line 873, in check
    self._check_files(
  File "/Users/benjamin/venvs/quickbbs/lib/python3.9/site-packages/pylint/lint/pylinter.py", line 905, in _check_files
    with self._astroid_module_checker() as check_astroid_module:
  File "/opt/homebrew/Cellar/[email protected]/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/contextlib.py", line 117, in __enter__
    return next(self.gen)
  File "/Users/benjamin/venvs/quickbbs/lib/python3.9/site-packages/pylint/lint/pylinter.py", line 1014, in _astroid_module_checker
    checker.open()
  File "/Users/benjamin/venvs/quickbbs/lib/python3.9/site-packages/pylint_django/checkers/foreign_key_strings.py", line 90, in open
    django.setup()
  File "/Users/benjamin/venvs/quickbbs/lib/python3.9/site-packages/django/__init__.py", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/Users/benjamin/venvs/quickbbs/lib/python3.9/site-packages/django/conf/__init__.py", line 82, in __getattr__
    self._setup(name)
  File "/Users/benjamin/venvs/quickbbs/lib/python3.9/site-packages/django/conf/__init__.py", line 69, in _setup
    self._wrapped = Settings(settings_module)
  File "/Users/benjamin/venvs/quickbbs/lib/python3.9/site-packages/django/conf/__init__.py", line 170, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/opt/homebrew/Cellar/[email protected]/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'quickbbs.settings'

I've tried it without the export, and just with the command line option, and I've tried it with the export and without the command line option, same effect.

The export solved the LOGGING error, so I believe that pylint is seeing the settings file, I just don't understand why pylint-django isn't.

Is there some strange syntax that I'm missing? pylint-django use to work fine a while back.... But I haven't touched this project with pylint-django in a quite a while.

bschollnick avatar May 31 '21 15:05 bschollnick

I've came across the same problem.

A hack was to insert this:

import sys; sys.path.insert(0, '')

Right above the django.setup() line at pylint_django/checkers/foreign_key_strings.py:90

Edit: Setting PYTHONPATH="." also seemed to resolve the problem

lowerthansound avatar Jun 03 '21 17:06 lowerthansound

Same issue encountered here, similar DJANGO_SETTINGS_MODULE setup and pylint invocation

Unfortunately, none of the above hacks worked for me

python 3.9.5 pylint 2.8.3 pylint-django 2.4.4 django 3.2.4

Grenadingue avatar Jun 04 '21 09:06 Grenadingue

export DJANGO_SETTINGS_MODULE=quickbbs.settings
pylint --load-plugins pylint_django --django-settings-module=quickbbs.settings $1 $2 $3 $4

@bschollnick

  • what's your pwd
  • what's your Python path (or where's the quickbbs directory located in relation to pwd)
  • what are the contents of the quickbbs/ directory?

From what you've posted none of this is clear and I'm not seeing a settings.py file either.

atodorov avatar Jun 04 '21 10:06 atodorov

Sorry, I guess I wasn’t clear enough.

Current working directory - /gallery/quickbbs (which contains manage.py)

The django project- is named quickbbs, and has two subdirectories:

frontend quickbbs along with the directories as shown in the previous message

The quickbbs/quickbbs directory is the django main folder, which contains the settings.py, models.py, and etc..

Python path:

export ***@***.***/libexec/bin:$PATH"
export PATH="/usr/local/lib/python3.9/site-packages:$PATH"

Contents of the quickbbs/quickbbs directory (to be clear, since I’m running from /gallery/quickbbs, this is the quickbbs directory that is referenced in the command line):

(quickbbs) benjamin@Nerv quickbbs % ls quickbbs BBEditCheckSyntaxTemp-settings.pyc jinjaenv.py migrations urls.py init.py logger.py models.py urls.pyc init.pyc logger.pyc models.pyc wsgi copy.py pycache middleware settings.py wsgi.py asgi.py middleware.py settings.pyc wsgi.pyc

Now after updating pylint:

pylint 2.8.3 pylint-django 2.4.4

And changing:

export DJANGO_SETTINGS_MODULE=quickbbs

I am getting different results:

quickbbs) benjamin@Nerv quickbbs % pylint --load-plugins pylint_django frontend/views.py ************* Module frontend.views frontend/views.py:87:0: C0301: Line too long (119/100) (line-too-long) frontend/views.py:291:0: C0301: Line too long (104/100) (line-too-long) frontend/views.py:31:0: E0401: Unable to import 'frontend.archives3' (import-error) frontend/views.py:34:0: E0401: Unable to import 'frontend.ftypes' (import-error) frontend/views.py:35:0: E0401: Unable to import 'frontend.config' (import-error) frontend/views.py:36:0: E0401: Unable to import 'frontend.database' (import-error) frontend/views.py:37:0: E0401: Unable to import 'frontend.thumbnail' (import-error) frontend/views.py:39:0: E0401: Unable to import 'frontend.utilities' (import-error) frontend/views.py:42:0: E0401: Unable to import 'frontend.watchdogmon' (import-error) frontend/views.py:44:0: E0401: Unable to import 'frontend.web' (import-error) frontend/views.py:45:0: E0401: Unable to import 'frontend.web' (import-error) frontend/views.py:46:0: E0611: No name 'models' in module 'quickbbs' (no-name-in-module) frontend/views.py:46:0: E0401: Unable to import 'quickbbs.models' (import-error) frontend/views.py:239:0: C0116: Missing function or method docstring (missing-function-docstring) frontend/views.py:239:0: R0912: Too many branches (13/12) (too-many-branches) frontend/views.py:239:0: R0915: Too many statements (61/50) (too-many-statements) frontend/views.py:318:0: C0116: Missing function or method docstring (missing-function-docstring) frontend/views.py:337:0: C0116: Missing function or method docstring (missing-function-docstring) frontend/views.py:403:0: C0103: Function name "downloadFile" doesn't conform to snake_case naming style (invalid-name) frontend/views.py:403:26: W0613: Unused argument 'filename' (unused-argument)

Now I don’t understand why we’re unable to import quickbbs.models, and the other various import errors… But at least it eliminates the django not initialized error.

bschollnick avatar Jun 08 '21 11:06 bschollnick

Looks like I m not only the one facing this issues. Did any one get it working?

when I try to add this to my settings.json, pylint does not work

        "--django-settings-module",
        "app.setting",

before that I had only these lines in

        "--load-plugins",
        "pylint_django",

VS code output was. Django was not configured. For more information run pylint --load-plugins=pylint_django --help-msg=django-not-configured

Edit Update

I figured out what was the problem, If you have any Syntax Errors or Import Error in your files, Pylint stops working on VS code. I would recommend running and seeing if you get any Exceptions.

pylint --load-plugins=pylint_django --django-settings-module=<app_name>.settings L:/Projects/website/root/<app_name>

Here is my settings.json in vscode

        "--load-plugins",
        "pylint_django",
        "--django-settings-module",
        "diy_website.settings",
    ]

Hope this helps ! ✌

tauzahmd avatar Aug 01 '21 11:08 tauzahmd

@tazahmd thank you so much! I imported some settings from environmental variables and had no default values set for them. Since my .env files are read as part of manage.py, they were never loaded and therefore I got those errors. Now, it works perfectly 😄

larsschellhas avatar Nov 24 '21 16:11 larsschellhas

A clear answer to this is that the DJANGO_SETTINGS_MODULE should always be in relation to where your manage.py is located as compared to your settings.py file. If you are not clear what to put in DJANGO_SETTINGS_MODULE, open your manage.py file and look at a line in it similar to os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings"). Whatever value is being set here, same value should always be set in your DJANGO_SETTINGS_MODULE environment variable.

noopurphalak avatar Nov 27 '23 05:11 noopurphalak