manage.py and simplejson call error resolution

1. Call manage.py and report an error

When initializing after installing Mathics , an open source alternative to Mathematica , calling manage.py reports an error:

Traceback (most recent call last):
  File "mathics/manage.py", line 3, in <module>
    from django.core.management import execute_manager
ImportError: cannot import name execute_manager

After searching, I found a solution (see https://docs.djangoproject.com/en/1.4/releases/1.4/#updated-default-project-layout-and-manage-py for the cause of this error ), which needs to be modified The call to manage.py.

For the manage.py used by Mathics, manage.py before modification:

#!/usr/bin/env python

from django.core.management import execute_manager
try:
    import settings  # Assumed to be in the same directory.
except ImportError:
    import sys
    sys.stderr.write(
        """Error: Can't find the file 'settings.py' in the directory containing
%r. It appears you've customized things.\n
You'll have to run django-admin.py, passing it your settings module.
(If the file settings.py does indeed exist, it's causing an ImportError.)\n"""
        % __file__)
    sys.exit(1)

if __name__ == "__main__":
    execute_manager(settings)

    # fix known PyPy bug (see https://bugs.pypy.org/issue1116)
    import gc
    gc.collect()
    gc.collect()

Modified manage.py:

#!/usr/bin/env python

import os, sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

    # fix known PyPy bug (see https://bugs.pypy.org/issue1116)
    import gc
    gc.collect()
    gc.collect()

The main change is to change import execute_manager to import execute_from_command_line. See the link above for more detailed modification instructions.


2. Using django.utils.simplejson to report an error

After the above initialization is successful, Mathics still cannot start normally, and the error is as follows:

Traceback (most recent call last):
  File "/usr/local/bin/mathics", line 9, in <module>
    load_entry_point('Mathics==0.6.0rc1', 'console_scripts', 'mathics')()
  File "/usr/local/lib/python2.7/dist-packages/Mathics-0.6.0rc1-py2.7.egg/mathics/main.py", line 214, in main
    definitions = Definitions(add_builtin=True)
  File "/usr/local/lib/python2.7/dist-packages/Mathics-0.6.0rc1-py2.7.egg/mathics/core/definitions.py", line 49, in __init__
    from mathics.builtin import modules, contribute
  File "/usr/local/lib/python2.7/dist-packages/Mathics-0.6.0rc1-py2.7.egg/mathics/builtin/__init__.py", line 21, in <module>
    from mathics.builtin import (
  File "/usr/local/lib/python2.7/dist-packages/Mathics-0.6.0rc1-py2.7.egg/mathics/builtin/graphics.py", line 9, in <module>
    from django.utils import simplejson
ImportError: cannot import name simplejson

The error message appears to be caused by the absence of the simplejson module in "django.utils". After searching, we found that simplejson has been removed in the latest version of django (see https://docs.djangoproject.com/en/ dev/releases/1.7/#features-removed-in-1-7 ), and this module can be replaced by json from the standard library. The solution is to change all places where simplejson is used in the py script to json. As in the graphics.py mentioned in the error message

from django.utils import simplejson

Change it to:

import json as simplejson


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325483033&siteId=291194637