【爬坑】 haystack ImportError: cannot import name 'six' from 'django.utils'

問題

Django 3.0 使用 haystack 報錯 ImportError: cannot import name 'six' from 'django.utils'

原因

Django 3.x 版本移除了部分用於兼容Python2 的 API
參見 Removed private Python 2 compatibility APIs

解決辦法

方案一

安裝 six

pip install six

將安裝好的 six 文件複製到 'djangoutils 目錄下,安裝庫位於 Python 安裝目錄下 Lib\site-packages 內或虛擬環境目錄 Lib\site-packages 內。

注:
haystack 中的 inputs.py 中使用 django.utils.encoding.python_2_unicode_compatible()six.python_2_unicode_compatible() 的別名,則需要更改 Lib\site-packages\haystack\inputs.py

from django.utils.encoding import force_text, python_2_unicode_compatible

改爲

from django.utils.encoding import force_text
from django.utils.six import python_2_unicode_compatible

方案二

使用Django 2.x

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章