python报错集合

我们在测试一些功能的时候要注意模块名不能和内置模块名重复,下面的AttributeError就是hashlib的文件名和内置的模块名重复导致的;改掉自己写的.py文件名即可。
AttributeError: partially initialized module 'hashlib' has no attribute 'md5' (most likely due to a circular import)

我们可以看到报错:django.core.exceptions.ImproperlyConfigured: WSGI application 'DRF.wsgi.application' could not be loaded; Error importing module.

我们遇到报错要先注意下报错点,然后再去进一步推理报错的原因;比如这个报错是Django项目报错的,pycharm提示的很明显就是rest_framework 注册位置写错了,应该在INSTALLED_APPS 的 应用程序里注册。

报错提示:ImportError: rest_framework doesn't look like a module path

System check identified no issues (0 silenced).
April 12, 2021 - 22:22:49
Django version 2.2.19, using settings 'DRF.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "...\lib\site-packages\django\utils\module_loading.py", line 13, in import_string
    module_path, class_name = dotted_path.rsplit('.', 1)
ValueError: not enough values to unpack (expected 2, got 1)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "...\lib\site-packages\django\core\servers\basehttp.py", line 45, in get_internal_wsgi_application
    return import_string(app_path)
  File "...\lib\site-packages\django\utils\module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "...\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 790, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "...ts\DRF\DRF\wsgi.py", line 16, in <module>
    application = get_wsgi_application()
  File "...\lib\site-packages\django\core\wsgi.py", line 13, in get_wsgi_application
    return WSGIHandler()
  File "...\lib\site-packages\django\core\handlers\wsgi.py", line 135, in __init__
    self.load_middleware()
  File "...\lib\site-packages\django\core\handlers\base.py", line 35, in load_middleware
    middleware = import_string(middleware_path)
  File "...\lib\site-packages\django\utils\module_loading.py", line 15, in import_string
    raise ImportError("%s doesn't look like a module path" % dotted_path) from err
ImportError: rest_framework doesn't look like a module path

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "...\lib\threading.py", line 950, in _bootstrap_inner
    self.run()
  File "...\lib\threading.py", line 888, in run
    self._target(*self._args, **self._kwargs)
  File "...\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "...\lib\site-packages\django\core\management\commands\runserver.py", line 137, in inner_run
    handler = self.get_handler(*args, **options)
  File "...\lib\site-packages\django\contrib\staticfiles\management\commands\runserver.py", line 27, in get_handler
    handler = super().get_handler(*args, **options)
  File "...\lib\site-packages\django\core\management\commands\runserver.py", line 64, in get_handler
    return get_internal_wsgi_application()
  File "...\lib\site-packages\django\core\servers\basehttp.py", line 47, in get_internal_wsgi_application
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: WSGI application 'DRF.wsgi.application' could not be loaded; Error importing module.

在写后端接口,迁移数据库的时候遇到E04错误

ERRORS:
auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'User.groups'.
	HINT: Add or change a related_name argument to the definition for 'User.groups' or 'User.groups'.
auth.User.user_permissions: (fields.E304) Reverse accessor for 'User.user_permissions' clashes with reverse accessor for 'User.user_permissions'.
	HINT: Add or change a related_name argument to the definition for 'User.user_permissions' or 'User.user_permissions'.
user.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'User.groups'.
	HINT: Add or change a related_name argument to the definition for 'User.groups' or 'User.groups'.
user.User.user_permissions: (fields.E304) Reverse accessor for 'User.user_permissions' clashes with reverse accessor for 'User.user_permissions'.
	HINT: Add or change a related_name argument to the definition for 'User.user_permissions' or 'User.user_permissions'.

这里是因为要使用 django默认的认证系统(AbstractUser)在生成迁移文件之前,要先在settings文件里面做一个配置

# user代表应用  , User代表应用下的模型类
AUTH_USER_MODEL='user.User'

配置这里之后,就能接触,错误中的,冲突问题,迁移文件时,不会默认生成“ auth_user” 这张表而是生成 我们指定的这个模型类对应的表(看一下我的)。就不会冲突报错了。

posted @ 2021-01-12 20:41  只有时间是永恒  阅读(535)  评论(0编辑  收藏  举报