Search code examples
pythonpython-3.xdjangopython-3.8pylint

pylint-django shows error ModuleNotFoundError: No module named 'myproj'


I have installed pylint and pylint-django. Then followed docs.

My django project is structured as follows:

/path/to/project/myproj/settings_test.py
/path/to/project/manage.py

Running the following command:

cd /path/to/project/
export DJANGO_SETTINGS_MODULE=myproj.settings_test
find . -name "*.py" | xargs pylint --load-plugins pylint_django --django-settings-module=myproj.settings_test

I face with this error: errors:

Traceback (most recent call last):
  File "/home/ar2015/.local/bin/pylint", line 8, in <module>
    sys.exit(run_pylint())
  File "/home/ar2015/.local/lib/python3.8/site-packages/pylint/__init__.py", line 35, in run_pylint
    PylintRun(argv or sys.argv[1:])
  File "/home/ar2015/.local/lib/python3.8/site-packages/pylint/lint/run.py", line 207, in __init__
    linter.check(args)
  File "/home/ar2015/.local/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 685, in check
    with self._astroid_module_checker() as check_astroid_module:
  File "/usr/lib/python3.8/contextlib.py", line 113, in __enter__
    return next(self.gen)
  File "/home/ar2015/.local/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 982, in _astroid_module_checker
    checker.open()
  File "/home/ar2015/.local/lib/python3.8/site-packages/pylint_django/checkers/foreign_key_strings.py", line 92, in open
    django.setup()
  File "/home/ar2015/.local/lib/python3.8/site-packages/django/__init__.py", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/home/ar2015/.local/lib/python3.8/site-packages/django/conf/__init__.py", line 82, in __getattr__
    self._setup(name)
  File "/home/ar2015/.local/lib/python3.8/site-packages/django/conf/__init__.py", line 69, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/ar2015/.local/lib/python3.8/site-packages/django/conf/__init__.py", line 170, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'myproj'

Note: The following did not help:

python3 manage.py makemigrations
python3 manage.py migrate
pip install django-filter
touch __init__.py
export DJANGO_SETTINGS_MODULE=myproj

.

Steps for reproduction:

Note this is a MWE and getting around the problem will not fix it:

django-admin startproject myproj
cd myproj/
find . -name "*.py" | xargs pylint --load-plugins pylint_django # fine
export DJANGO_SETTINGS_MODULE=myproj.settings
find . -name "*.py" | xargs pylint --load-plugins pylint_django # error: ModuleNotFoundError: No module named 'myproj'

.


Solution

  • Most likely your settings module is not on the Python import search path. To add it for the duration of the current session, run from inside your project directory:

    export PYTHONPATH=$PYTHONPATH:$PWD

    How to make this permanent is explained in this answer.