3

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

Roni X
  • 69
  • 2
  • 10
  • 1
    `django.utils.six` was removed from django 3.0 https://docs.djangoproject.com/en/3.0/releases/3.0/#removed-private-python-2-compatibility-apis . You can see that smart-select package still using it in error stacktrace – Linh Nguyen Jan 13 '20 at 02:13
  • @RoniX [**Check this answer**](https://stackoverflow.com/a/59420098/12578202), this answer explain a lot, such as Why, How to solve, how to identify etc... – JPG Jan 13 '20 at 02:27
  • 4
    Does this answer your question? [ImportError: cannot import name 'six' from 'django.utils'](https://stackoverflow.com/questions/59193514/importerror-cannot-import-name-six-from-django-utils) – JPG Jan 13 '20 at 02:28

2 Answers2

6

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 virtual environment's Django install. Using virtualenv that would be here: /path/to/python/ 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

The python3 -c of the script in quotes depends heavily on being able to determine the location of the site_packages directory, and doesn't work in some virtual environments. YMMV

Lord Elrond
  • 13,430
  • 7
  • 40
  • 80
2

i think you have upgraded Django 2 version to Django 3

for transaction from django 2 to 3 you should make following steps:

replace any statement from on_delete='CASCADE' to on_delete=models.CASCADE

if you had install Django-autocomplete-light remove it and install it again (i prefer Django-autocomplete-light==3.8.1)

if you had installed djangorestframework remove it and install it again ( i prefer djangorestframework==3.12.2)

i hope it will success for you and every body

Youssri Abo Elseod
  • 671
  • 1
  • 9
  • 23