3

我收到错误

ImportError:无法导入名称 OrderedDict

在我的 ubuntu 14.04 机器上本地安装服务器时。
我已经尝试手动安装orderdict,也尝试升级kombu,但它仍然给出错误。完整的回溯添加在下面-

rishav@Swastik:~/open-event-orga-server$ sudo python create_db.py
Traceback(最近一次调用最后):文件“create_db.py”,第 5 行,
从应用程序导入 current_app
文件“/home/rishav/open -event-orga-server/app/ init .py”,第 23 行,
从 celery 导入 Celery
文件“/usr/local/lib/python2.7/dist-packages/celery/init .py ”,第 133 行,在
from celery import 5 # noqa File "/usr/local/lib/python2.7/dist-packages/celery/five.py", line 153, in
from kombu.utils.compat import OrderedDict # noqa
ImportError: cannot import name OrderedDict

4

3 回答 3

6

这解决了我的问题:

  1. 将 kombu 降级到 2.5.16。

  2. 重新安装 django-celery

这是日志:

(virtualEnv) D:\Project\staging\modone\settings>pip install django-celery
Collecting django-celery
  Downloading django_celery-3.2.1-py2-none-any.whl (63kB)
    100% |################################| 71kB 83kB/s
Collecting django>=1.8 (from django-celery)
  Using cached Django-1.10.5-py2.py3-none-any.whl
Collecting celery<4.0,>=3.1.15 (from django-celery)
  Downloading celery-3.1.25-py2.py3-none-any.whl (526kB)
    100% |################################| 532kB 122kB/s
Requirement already satisfied: pytz>dev in d:\project\installations\virtualenv\lib\site-packages (from celery<4.0,>=3.1.15->django-celery)
Collecting kombu<3.1,>=3.0.37 (from celery<4.0,>=3.1.15->django-celery)
  Using cached kombu-3.0.37-py2.py3-none-any.whl
Collecting billiard<3.4,>=3.3.0.23 (from celery<4.0,>=3.1.15->django-celery)
  Downloading billiard-3.3.0.23-cp27-none-win32.whl (102kB)
    100% |################################| 102kB 114kB/s
Requirement already satisfied: anyjson>=0.3.3 in d:\project\installations\virtualenv\lib\site-packages (from kombu<3.1,>=3.0.37->celery<4.0,>=3.1.15->
django-celery)
Collecting amqp<2.0,>=1.4.9 (from kombu<3.1,>=3.0.37->celery<4.0,>=3.1.15->django-celery)
  Using cached amqp-1.4.9-py2.py3-none-any.whl
Installing collected packages: django, amqp, kombu, billiard, celery, django-celery
  Found existing installation: Django 1.4.5
    Uninstalling Django-1.4.5:
      Successfully uninstalled Django-1.4.5
  Found existing installation: amqp 1.0.13
    Uninstalling amqp-1.0.13:
      Successfully uninstalled amqp-1.0.13
  Found existing installation: kombu 2.5.16
    Uninstalling kombu-2.5.16:
      Successfully uninstalled kombu-2.5.16
  Found existing installation: billiard 2.7.3.28
    Uninstalling billiard-2.7.3.28:
      Successfully uninstalled billiard-2.7.3.28
  Found existing installation: celery 3.0.18
    Uninstalling celery-3.0.18:
      Successfully uninstalled celery-3.0.18
Successfully installed amqp-1.4.9 billiard-3.3.0.23 celery-3.1.25 django-1.10.5 django-celery-3.2.1 kombu-3.0.37
于 2017-01-28T13:22:23.470 回答
3

我在带有kombu 4.0.x的python 2.7.8上遇到了同样的问题,我通过将kombu降级到3.0.x(这是我需要的版本)解决了这个问题。

没有时间调查问题的原因,但这可能是相关的......

于 2017-01-11T02:51:45.650 回答
2

ImportError如果未正确导入(例如 with orderedDictOrdereddict或) ,也会发生ordereddict

方法一

from collections import OrderedDict

d = {'banana': 3, 'apple': 4, 'pear': 1, 'orange': 2}

# dictionary sorted by key
print(OrderedDict(sorted(d.items(), key=lambda t: t[0])))
print(OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)]))

方法二

import collections

d = {'banana': 3, 'apple': 4, 'pear': 1, 'orange': 2}

# dictionary sorted by key
print(collections.OrderedDict(sorted(d.items(), key=lambda t: t[0])))
print(collections.OrderedDict(
    [('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)]))

输出

OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])
OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])

资源

8.3.5.1。OrderedDict 示例和食谱

于 2019-10-20T17:37:54.403 回答