Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: cannot import name SortedDict - only on Python 2.7, not 2.6

I am trying to get a Django application up and running. The libraries are a bit out of date (its currently on Django 1.4).

I have got it more or less working when I tried python 2.6, but when I change to a virtualenv using python 2.7 I get the error:

Unhandled exception in thread started by <function wrapper at 0x7f057b17c320>
Traceback (most recent call last):
File "/home/colin/software/virtualenvs/barshool/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/colin/software/virtualenvs/barshool/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/home/colin/software/virtualenvs/barshool/lib/python2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/home/colin/software/virtualenvs/barshool/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/colin/software/virtualenvs/barshool/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/colin/software/virtualenvs/barshool/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/home/colin/software/virtualenvs/barshool/lib/python2.7/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/colin/software/virtualenvs/barshool/lib/python2.7/site-packages/transmeta/__init__.py", line 7, in <module>
from django.utils.datastructures import SortedDict
ImportError: cannot import name SortedDict

I set up the virtualenvs with the same requirements files.

What is the difference between the versions that could be causing this?

like image 766
wobbily_col Avatar asked Jan 05 '16 14:01

wobbily_col


2 Answers

SortedDict was removed in Django 1.9. Make sure you have installed the same version of Django in the new virtual environment as the old one.

If it was your own code that was using SortedDict, the long term fix would be to update the code to use collections.OrderedDict, which was added in Python 2.7. In your case, it looks like the import error is coming from a third party app, which you'll have to update or replace to make the code work with Django 1.9.

like image 67
Alasdair Avatar answered Nov 19 '22 09:11

Alasdair


This seems a persistent bug. Meant the same issue while working with django==1.11.1 and djangorestframework==3.9.1. It was particularly djangorestframework (drf) which was importing the sortedDict, so i instead updated drf. The latest version at this time is djangorestframework==3.10.3 from the python foundation. This drf upgrade solved my challenge.

Note: I was working with python 3.6.8, on Ubuntu 18.04.3 LTS (just in case you need those specifics)

like image 23
MwamiTovi Avatar answered Nov 19 '22 11:11

MwamiTovi