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

Failure to import settings? #325

Open
bschollnick opened this issue May 31, 2021 · 7 comments · May be fixed by #363
Open

Failure to import settings? #325

bschollnick opened this issue May 31, 2021 · 7 comments · May be fixed by #363

Comments

@bschollnick
Copy link

bschollnick commented May 31, 2021

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.

@lowerthansound
Copy link

lowerthansound commented Jun 3, 2021

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

@Grenadingue
Copy link

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

@atodorov
Copy link
Contributor

atodorov commented Jun 4, 2021

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.

@bschollnick
Copy link
Author

bschollnick commented Jun 8, 2021 via email

@tauzahmd
Copy link

tauzahmd commented Aug 1, 2021

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 ! ✌

@larsschellhas
Copy link

@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 😄

TheAfroOfDoom added a commit to TheAfroOfDoom/pylint-django that referenced this issue Jun 4, 2022
Both `DJANGO_SETTINGS_MODULE` and `DJANGO_CONFIGURATION` are required for `django.configurations.importer` to install, else it throws an exception.
If the `importer` is not installed, `django.setup()` fails on `configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)` when trying to access uninitialized settings.

fixes pylint-dev#309 (pylint-dev#309 (comment))
fixes pylint-dev#325
might relate to pylint-dev#362
@TheAfroOfDoom TheAfroOfDoom linked a pull request Jun 4, 2022 that will close this issue
@noopurphalak
Copy link

noopurphalak commented Nov 27, 2023

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.

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

Successfully merging a pull request may close this issue.

7 participants