目前,我已经将Django的版本从升级2.0.6到,3.0并且在调用python manage.py shellcommand时突然出现以下错误:
2.0.6
3.0
python manage.py shellcommand
ImportError: cannot import name 'six' from 'django.utils' (/path-to-project/project/venv/lib/python3.7/site-packages/django/utils/init.py)
完整跟踪:
Traceback (most recent call last): File "manage.py", line 13, in <module> execute_from_command_line(sys.argv) File "/path-to-project/project/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line utility.execute() File "/path-to-project/project/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 377, in execute django.setup() File "/path-to-project/project/venv/lib/python3.7/site-packages/django/__init__.py", line 24, in setup apps.populate(settings.INSTALLED_APPS) File "/path-to-project/project/venv/lib/python3.7/site-packages/django/apps/registry.py", line 91, in populate app_config = AppConfig.create(entry) File "/path-to-project/project/venv/lib/python3.7/site-packages/django/apps/config.py", line 90, in create module = import_module(entry) File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1006, in _gcd_import File "<frozen importlib._bootstrap>", line 983, in _find_and_load File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 677, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 728, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "/path-to-project/project/venv/lib/python3.7/site-packages/corsheaders/__init__.py", line 1, in <module> from .checks import check_settings # noqa: F401 File "/path-to-project/project/venv/lib/python3.7/site-packages/corsheaders/checks.py", line 7, in <module> from django.utils import six
Django 3.0.0发行说明指定删除了某些私有的Python 2兼容性API。其中有django.utils.six。
django.utils.six
对于此错误,@ WillemVanOnsem特别指出该模块corsheaders正在引用该模块。
corsheaders
对于遇到相同问题的其他人,查看stacktrace的最后一行上的文件路径可以帮助识别有问题的模块。我看到的另一个例子是:
... File "/path/to/project/venv/lib/python3.8/site-packages/parler/utils/conf.py", line 10, in <module> from django.utils import six ImportError: cannot import name 'six' from 'django.utils' (/path/to/project/venv/lib/python3.8/site-packages/django/utils/__init__.py)
在这种情况下,导致问题的模块为parler。希望这对遇到此问题的其他人有所帮助。
parler