Python 无法导入名称 simplejson - 安装 simplejson 后

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28048943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:40:31  来源:igfitidea点击:

Cannot import name simplejson - After installing simplejson

pythondjango

提问by Tameen Malik

I have Django Version 1.7 and Python Version 2.7.5 - I used pip install simplejson and apt-get install python-simplejson commands to solve this problem but it still shows me this exception. Is there any compatibility issue between Django and Python or what is the solution to get out of this exception:

我有 Django 版本 1.7 和 Python 版本 2.7.5 - 我使用 pip install simplejson 和 apt-get install python-simplejson 命令来解决这个问题,但它仍然向我显示这个异常。Django 和 Python 之间是否存在任何兼容性问题或解决此异常的解决方案是什么:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/root/test_env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/root/test_env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/root/test_env/local/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/root/test_env/local/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/root/test_env/local/lib/python2.7/site-packages/django/apps/config.py", line 123, in create
    import_module(entry)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/root/test_env/local/lib/python2.7/site-packages/extdirect.django-0.3-py2.7.egg/extdirect/django/__init__.py", line 3, in <module>
    from providers import ExtRemotingProvider, ExtPollingProvider
  File "/root/test_env/local/lib/python2.7/site-packages/extdirect.django-0.3-py2.7.egg/extdirect/django/providers.py", line 4, in <module>
    from django.utils import simplejson
ImportError: cannot import name simplejson

采纳答案by Thomas Orozco

Your code isn't compatible with the version of Django you are using.

您的代码与您使用的 Django 版本不兼容。

Django used to ship with simplejsonin django.utils, but this was removed in Django 1.5:

Django 曾经附带simplejsonin django.utils,但在 Django 1.5 中删除了

django.utils.simplejson

Since Django 1.5 drops support for Python 2.5, we can now rely on the json module being available in Python's standard library, so we've removed our own copy of simplejson. You should now import json instead of django.utils.simplejson.

Unfortunately, this change might have unwanted side-effects, because of incompatibilities between versions of simplejson – see the backwards-incompatible changes section. If you rely on features added to simplejson after it became Python's json, you should import simplejson explicitly.

django.utils.simplejson

由于 Django 1.5 不再支持 Python 2.5,我们现在可以依赖 Python 标准库中提供的 json 模块,因此我们删除了自己的 simplejson 副本。您现在应该导入 json 而不是 django.utils.simplejson。

不幸的是,由于 simplejson 版本之间的不兼容,此更改可能会产生不需要的副作用——请参阅向后不兼容的更改部分。如果在 simplejson 成为 Python 的 json 后依赖添加到 simplejson 的功能,则应显式导入 simplejson。



You should update the code in extdirect's providers.pyto import jsoninstead, or use the version of Django it was designed to work with.

您应该更新代码extdirectproviders.pyimport json替代,或者用的Django版本,它的目的是要与工作。