导入错误:无法从包中的'django.conf.urls'导入名称'url'



我有一个将django web应用程序从Django-3.2.14迁移到Django-4.0的项目。迁移后,当我运行服务器时,我会立即收到控制台输出中显示的错误。当我跟踪这些更改时,它跟踪到django-compat包,django-background-tasks包正在调用它。my_env是我的病毒环境。

我们有最新版本的django-compat,它是1.0.15,django-background-tasks,它是1.2.5。

你能调查一下吗?

提前谢谢。

控制台:

(myenv) D:New_Foldergithubprojectproject_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_Foldergithubmyenvlibsite-packagescompt__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_Foldergithubmyenvlibsite-packagesdjangoconfurls__init__.py)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:Program FilesPython39libthreading.py", line 980, in _bootstrap_inner
self.run()
File "C:Program FilesPython39libthreading.py", line 917, in run
self._target(*self._args, **self._kwargs)
File "D:New_Foldergithubmyenvlibsite-packagesdjangoutilsautoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "D:New_Foldergithubmyenvlibsite-packagesdjangocoremanagementcommandsrunserver.py", line 125, in inner_run
autoreload.raise_last_exception()
File "D:New_Foldergithubmyenvlibsite-packagesdjangoutilsautoreload.py", line 87, in raise_last_exception
raise _exception[1]
File "D:New_Foldergithubmyenvlibsite-packagesdjangocoremanagement__init__.py", line 398, in execute
autoreload.check_errors(django.setup)()
File "D:New_Foldergithubmyenvlibsite-packagesdjangoutilsautoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "D:New_Foldergithubmyenvlibsite-packagesdjango__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "D:New_Foldergithubmyenvlibsite-packagesdjangoappsregistry.py", line 116, in populate
app_config.import_models()
File "D:New_Foldergithubmyenvlibsite-packagesdjangoappsconfig.py", line 304, in import_models
self.models_module = import_module(models_module_name)
File "C:Program FilesPython39libimportlib__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_Foldergithubmyenvlibsite-packagesbackground_taskmodels.py", line 9, in <module>
from compat import StringIO
File "D:New_Foldergithubmyenvlibsite-packagescompat__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'

更新的答案

遗憾的是,目前没有支持的django-background-tasks版本。如果不迁移到任何其他包,您似乎无法升级到django 4.0,而其他包的工作与django-background-tasks相同。

  • https://django-background-tasks.readthedocs.io/en/latest/#supported-版本和兼容性

旧答案

background_task使用的是django-compat,新的django版本不支持它。

D:New_Foldergithubmyenvlibsite-packagesbackground_taskmodels.py

from compat import StringIO

据我在stacktrace中看到的,不再需要使用django-compat包了。background_task程序包应直接使用默认io程序包中的StringIO

您将需要background_task软件包的更新版本,该软件包不再使用django-compat软件包。据我所见,没有一个新版本没有依赖性。

  • https://django-background-tasks.readthedocs.io/en/latest/
  • https://github.com/arteria/django-compat
  • https://docs.python.org/3/library/io.html#io.StringIO
Django已经从4.0版本中删除了URL。查看此处的文档:

https://django-background-tasks.readthedocs.io/en/latest/#supported-版本和兼容性

或者,您可以将其降级回3.2.10,从而消除问题

相关内容

  • 没有找到相关文章

最新更新