Django couldn't find "django-filter" or "django-cors-headers" when I use Pantsbuild

I configured Pantsbuild for our Django projects, and everything worked neatly. Here my BUILD file:

python_requirements(
    name="reqs",
)

python_sources(
    name="lib",
    dependencies=[
        ":reqs",
    ],
)

pex_binary(
    name="manage",
    entry_point="manage.py",
    restartable=True,
)

and requirements.txt:

django==4.1.1
djangorestframework==3.13.1
django-cors-headers==3.13.0
django-filter==22.1

but when I added django-filter, I faced an error:

Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/home/xurvan/monorepo/jango/manage.py", line 8, in <module>
    import django_filters
ModuleNotFoundError: No module named 'django_filters'

my manage.py file is like this:

#!/usr/bin/env python

import os
import sys

import django_filters


def main():
    os.environ.setdefault(
        'DJANGO_SETTINGS_MODULE',
        'monorepo.jango.app.settings',
    )
    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    print(django_filters.__version__)
    main()

The interesting point is that print(django_filters.__version__) works. And I can see the version correctly but then the error occurs.

I also installed Django Rest Framework, and it works fine. But I have the same problem with django-cors-headers. I don't understand the difference.

If React Front-end and Backend as DRF use this snippet.

CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOWED_ORIGINS = [
    "http://localhost:3000",
]

I could not understand the reason behind strange behavior, but all we need to do is to disable Django autoreloader. As official Pantsbuild group wrote in their Github:

with runserver we turn off Django's autoreloader, since we rely on Pants's own file-watching instead, by setting restartable=True on the pex_binary targets for manage.py

So running code with the following command solved my problem:

./pants run src/jango:manage -- runserver --noreload
Back to Top