django smart selects on Django version 3.0.1 - error ImportError: cannot import name 'six' from 'django.utils'

旧城冷巷雨未停 提交于 2020-01-15 01:48:47

问题


Installed django-smart-selects (pip install django-smart-selects) and is not working on django version 3.0.1

I configured using the official installation guide.

enter code here $ python manage.py runserver
    Watching for file changes with StatReloader
    Exception in thread django-main-thread:
    Traceback (most recent call last):
      File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
        self.run()
      File "/usr/lib/python3.7/threading.py", line 865, in run
        self._target(*self._args, **self._kwargs)
      File "/home/mxcloud3/Desktop/django/venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
        fn(*args, **kwargs)
      File "/home/mxcloud3/Desktop/django/venv/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
        autoreload.raise_last_exception()
      File "/home/mxcloud3/Desktop/django/venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 76, in raise_last_exception
        raise _exception[1]
      File "/home/mxcloud3/Desktop/django/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
        autoreload.check_errors(django.setup)()
      File "/home/mxcloud3/Desktop/django/venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
        fn(*args, **kwargs)
      File "/home/mxcloud3/Desktop/django/venv/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
        apps.populate(settings.INSTALLED_APPS)
      File "/home/mxcloud3/Desktop/django/venv/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate
        app_config.import_models()
      File "/home/mxcloud3/Desktop/django/venv/lib/python3.7/site-packages/django/apps/config.py", line 211, in import_models
        self.models_module = import_module(models_module_name)
      File "/home/mxcloud3/Desktop/django/venv/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 "/home/mxcloud3/Desktop/django/polls/models.py", line 2, in <module>
        from smart_selects.db_fields import GroupedForeignKey
      File "/home/mxcloud3/Desktop/django/venv/lib/python3.7/site-packages/smart_selects/db_fields.py", line 6, in <module>
        from django.utils import six
    ImportError: cannot import name 'six' from 'django.utils' (/home/mxcloud3/Desktop/django/venv/lib/python3.7/site-packages/django/utils/__init__.py)

Snippets of installation

models.py

from django.db import models
from smart_selects.db_fields import GroupedForeignKey

class Recipe(models.Model):
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    subcategory = GroupedForeignKey(Subcategory, "category", on_delete=models.CASCADE)

settings.py

INSTALLED_APPS = [
    'polls.apps.PollsConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'smart_selects',
]

JQUERY_URL = True


回答1:


The smart_selects library executes from django.utils import six somewhere in its code, which causes an import error because that package was removed in django 3.0.

If you can't update the offending package (which you can't in this case) the only solution would be to patch it yourself, or to wait until the owner of the library patches it.

Patching it yourself is trivial:

  • pip3 install six
  • navigate to your local Django package. For me, that is /usr/local/lib/python3.8/site-packages/django/utils/__init__.py
  • add import six

Or even better, do it with a bash one-liner:

pip3 install six && echo import six >"$(python3 -c "import sys; print(tuple(filter(lambda x: 'site-packages' in x, sys.path))[0])")"/django/utils/__init__.py

Note that you will have to run this script from within your virtual environment for it to apply to the correct django package. YMMV based on which virtual environment you use.



来源:https://stackoverflow.com/questions/59709646/django-smart-selects-on-django-version-3-0-1-error-importerror-cannot-import

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!