对修改的Django项目执行命令出现异常。
$python manage.py runserver
报错日志:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/user/apps/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/home/user/apps/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/user/apps/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 257, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/home/user/apps/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 39, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/usr/lib/python3.8/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 848, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/user/apps/venv/lib/python3.8/site-packages/django/contrib/staticfiles/management/commands/runserver.py", line 3, in <module>
from django.core.management.commands.runserver import (
File "/home/user/apps/venv/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 10, in <module>
from django.core.servers.basehttp import (
File "/home/user/apps/venv/lib/python3.8/site-packages/django/core/servers/basehttp.py", line 14, in <module>
from wsgiref import simple_server
File "/usr/lib/python3.8/wsgiref/simple_server.py", line 13, in <module>
from http.server import BaseHTTPRequestHandler, HTTPServer
File "/usr/lib/python3.8/http/server.py", line 628, in <module>
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
File "/usr/lib/python3.8/http/server.py", line 879, in SimpleHTTPRequestHandler
if not mimetypes.inited:
AttributeError: module 'mimetypes' has no attribute 'inited'
查看重点问题所在:
File "/usr/lib/python3.8/http/server.py", line 879, in SimpleHTTPRequestHandler
if not mimetypes.inited:
AttributeError: module 'mimetypes' has no attribute 'inited'
点击源文件查看位于server.py第879行的详细内容:



在server.py中的头部是“import mimetypes”,mimetypes是内置库。查看mimetypes.py源代码,可见变量"inited"是存在的。可见是导入重名的“mimetypes”库导致的异常。仔细查找发现是项目自定义相同名称的模块文件。

命名自定义的mimetypes.py为mimetypesd.py。

重新执行命令:
$python manage.py runserver

|