Django-background-tasks throws import exception after upgrade to Django 4.0

Hi all,

I just upgraded my Django 3.2.10 project (Python 3.8 on Windows 10) to Django 4.0 and just this change causes the django-background-tasks 1.2.5 to fail on import:

Traceback (most recent call last):
  File "py_code/UI/manage.py", line 21, in <module>
    main()
  File "py_code/UI/manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "D:\data\Python\RoyalRangers\venv\lib\site-packages\django\core\management\__init__.py", line 425, in execute_from_command_line
    utility.execute()
  File "D:\data\Python\RoyalRangers\venv\lib\site-packages\django\core\management\__init__.py", line 401, in execute
    django.setup()
  File "D:\data\Python\RoyalRangers\venv\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "D:\data\Python\RoyalRangers\venv\lib\site-packages\django\apps\registry.py", line 114, in populate
    app_config.import_models()
  File "D:\data\Python\RoyalRangers\venv\lib\site-packages\django\apps\config.py", line 300, in import_models
    self.models_module = import_module(models_module_name)
  File "c:\program files\python38\lib\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 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "D:\data\Python\RoyalRangers\venv\lib\site-packages\background_task\models.py", line 9, in <module>
    from compat import StringIO
  File "D:\data\Python\RoyalRangers\venv\lib\site-packages\compat\__init__.py", line 48, in <module>
    from django.conf.urls.defaults import url, include, handler404, handler500  # pyflakes:ignore
ModuleNotFoundError: No module named 'django.conf.urls.defaults'

Downgrading back to 3.2.10 removes the problem. Any idea what might be wrong?

Thanks Vranoch

It’s not compatible with 4.0 - see Django Background Tasks — django-background-tasks latest documentation.

Also FYI the deprecation is noted here: Django 4.0 release notes | Django documentation | Django

  • django.conf.urls.url() is removed.

That library is not currently what I’d call well maintained.

Aha … thanks so much.

It’s caused by django-compat, used by django_background_tasks just for StringIO
When you’re otherwise not using django-compat you can, as a quickfix, comment out all problematic code in site-packages/compat/__ init __.py

Then remove the providing_args argument to all calls to django.dispatch.Signal in django_background_tasks itself. Then it runs again.

That’s what I did, and with the pressure off I need to think about my future background tasks…

Hi, thanks for the hint. I already solved the problem with converting from django-tasks to Celery. It is a bit overkill for my purposes but it finally works. Thanks