简体   繁体   中英

ImportError: cannot import name 'url' from 'django.conf.urls' in packages

I have a project of migrating django web app from Django-3.2.14 to Django-4.0 . Immediately after migration, when I run the server I get the error as shown in Console output. When I traced the changes, it traced to django-compat package, which was being called by django-background-tasks package. my_env is my virutal environment.

We have latest version of django-compat , which is 1.0.15 and django-background-tasks , which is 1.2.5.

Could you please look into this?

Thank you in advance.

Console:

(myenv) D:\New_Folder\github\project\project_name>python manage.py runserver
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "D:\New_Folder\github\myenv\lib\site-packages\compt\__init__.py", line 46, in <module>
    from django.conf.urls import url, include, handler404, handler500
ImportError: cannot import name 'url' from 'django.conf.urls' (D:\New_Folder\github\myenv\lib\site-packages\django\conf\urls\__init__.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files\Python39\lib\threading.py", line 980, in _bootstrap_inner
    self.run()
  File "C:\Program Files\Python39\lib\threading.py", line 917, in run
    self._target(*self._args, **self._kwargs)
  File "D:\New_Folder\github\myenv\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "D:\New_Folder\github\myenv\lib\site-packages\django\core\management\commands\runserver.py", line 125, in inner_run
    autoreload.raise_last_exception()
  File "D:\New_Folder\github\myenv\lib\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception
    raise _exception[1]
  File "D:\New_Folder\github\myenv\lib\site-packages\django\core\management\__init__.py", line 398, in execute
    autoreload.check_errors(django.setup)()
  File "D:\New_Folder\github\myenv\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "D:\New_Folder\github\myenv\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "D:\New_Folder\github\myenv\lib\site-packages\django\apps\registry.py", line 116, in populate
    app_config.import_models()
  File "D:\New_Folder\github\myenv\lib\site-packages\django\apps\config.py", line 304, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Program Files\Python39\lib\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 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "D:\New_Folder\github\myenv\lib\site-packages\background_task\models.py", line 9, in <module>
    from compat import StringIO
  File "D:\New_Folder\github\myenv\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'

Updated answer

Sadly there is currently no supported version of django-background-tasks . It seems like you will not be able to upgrade to django 4.0 without migrate to any other package, which will do the same job as django-background-tasks .

Old Answer

The package background_task is using the django-compat , which is not supported with new django versions.

D:\New_Folder\github\myenv\lib\site-packages\background_task\models.py

from compat import StringIO

As far as I can see in the stacktrace, there is no need to use the django-compat package anymore. The background_task package should use StringIO directly from the default io package.

You will need an updated version of the background_task package, which is not using the django-compat package anymore. As far as I can see there is no new version whithout the dependency.

Django-4.0 当前删除了 url,您可以执行以下操作:-

from django.urls import re_path as url

Django has removed the URL from version 4.0. Check the documentation here:

https://django-background-tasks.readthedocs.io/en/latest/#supported-versions-and-compatibility

Alternatively, you can degrade it back to 3.2.10, which removes the problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM