makemigrations报错ImportError: cannot import name 'path' from 'django.urls'

错误信息

PS D:\python\django\guanxiangzhiji> python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 21, in 
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "D:\app\anaconda\lib\site-packages\django\core\management\__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "D:\app\anaconda\lib\site-packages\django\core\management\__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:\app\anaconda\lib\site-packages\django\core\management\base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "D:\app\anaconda\lib\site-packages\django\core\management\base.py", line 327, in execute
    self.check()
  File "D:\app\anaconda\lib\site-packages\django\core\management\base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "D:\app\anaconda\lib\site-packages\django\core\management\base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "D:\app\anaconda\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "D:\app\anaconda\lib\site-packages\django\core\checks\urls.py", line 16, in check_url_config
    return check_resolver(resolver)
  File "D:\app\anaconda\lib\site-packages\django\core\checks\urls.py", line 26, in check_resolver
    return check_method()
  File "D:\app\anaconda\lib\site-packages\django\urls\resolvers.py", line 256, in check
    for pattern in self.url_patterns:
  File "D:\app\anaconda\lib\site-packages\django\utils\functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "D:\app\anaconda\lib\site-packages\django\urls\resolvers.py", line 407, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "D:\app\anaconda\lib\site-packages\django\utils\functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "D:\app\anaconda\lib\site-packages\django\urls\resolvers.py", line 400, in urlconf_module
    return import_module(self.urlconf_name)
  File "D:\app\anaconda\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "", line 1006, in _gcd_import
  File "", line 983, in _find_and_load
  File "", line 967, in _find_and_load_unlocked
  File "", line 677, in _load_unlocked
  File "", line 728, in exec_module
  File "", line 219, in _call_with_frames_removed
  File "D:\django\guanxiangzhiji\urls.py", line 17, in 
    from django.urls import path
ImportError: cannot import name 'path' from 'django.urls' (D:\app\anaconda\lib\site-packages\django\urls\__init__.py)

原因分析

urls.py中,

from django.urls import path

这句语法错误。
因为之前为解决Generator expression must be parenthesized错误,把django从2.2.4降到了1.11.22。而path类是在2.X版本才有的,因此报错。

解决方案

将django升级回最新版本。

PS D:\django\guanxiangzhiji> pip uninstall django
Uninstalling Django-1.11.22:
  Would remove:
    d:\app\anaconda\lib\site-packages\django-1.11.22.dist-info\*
    d:\app\anaconda\lib\site-packages\django\*
    d:\app\anaconda\scripts\django-admin.exe
    d:\app\anaconda\scripts\django-admin.py
Proceed (y/n)? y
  Successfully uninstalled Django-1.11.22
  
PS D:\django\guanxiangzhiji> pip install django
Collecting django
  Using cached https://files.pythonhosted.org/packages/d6/57/66997ca6ef17d2d0f0ebcd860bc6778095ffee04077ca8985928175da358/Django-2.2.4-py3-none-any.whl
Requirement already satisfied: pytz in d:\app\anaconda\lib\site-packages (from django) (2018.9)
Requirement already satisfied: sqlparse in d:\app\anaconda\lib\site-packages (from django) (0.3.0)
Installing collected packages: django
Successfully installed django-2.2.4

PS D:\django\guanxiangzhiji> python manage.py makemigrations
Migrations for 'smodel':
  smodel\migrations\0001_initial.py
    - Create model Stka

PS D:\django\guanxiangzhiji>

你可能感兴趣的:(Python)