为什么这段代码会产生这个错误?

时间:2017-01-02 17:34:46

标签: python


    # -*- coding: utf-8 -*-
    from __future__ import print_function
    import io
    import os
    from os.path import dirname, join

    from alembic import command
    from alembic.config import Config
    import baker

    from webapp.database import db
    from webapp.main import app
    import webapp.areas.manage
    import webapp.users.manage
    import webapp.planner.manage
    import webapp.tests.manage


    @baker.command
    def load_data():

    traceback (most recent call last):
      File "manage.py", line 11, in 
        from webapp.database import db
      File "/var/www/project01/webapp/database/__init__.py", line 3, in 
        from .main import db
      File "/var/www/project01/webapp/database/main.py", line 6, in 
        from ..app import app
      File "/var/www/project01/webapp/app.py", line 37, in 
        default_timezone="America/Lima"
      File "/usr/local/lib/python2.7/dist-packages/allspeak/allspeak.py", line 30, in __init__
        super(Allspeak, self).__init__(*args, **kwargs)
      File "/usr/local/lib/python2.7/dist-packages/allspeak/i18n.py", line 33, in __init__
        self.load_translations()
      File "/usr/local/lib/python2.7/dist-packages/allspeak/i18n.py", line 50, in load_translations
        self.translations = self.reader.load_translations(locales=locales)
      File "/usr/local/lib/python2.7/dist-packages/allspeak/reader.py", line 129, in load_translations
        self._update_translations(translations, filepath)
      File "/usr/local/lib/python2.7/dist-packages/allspeak/reader.py", line 96, in _update_translations
        data = self._load_file(filepath)
      File "/usr/local/lib/python2.7/dist-packages/allspeak/reader.py", line 90, in _load_file
        return self._extract_locales(data)
      File "/usr/local/lib/python2.7/dist-packages/allspeak/reader.py", line 81, in _extract_locales
        for locale, trans in data.items()
    AttributeError: 'NoneType' object has no attribute 'items'

2 个答案:

答案 0 :(得分:1)

没有代码,我不得不猜测这一点。属性错误意味着您正在尝试访问不存在的对象的属性。

例如,如果我有一个名为矩形的对象。它有两个属性宽度和长度;现在如果我尝试使用这个矩形并尝试访问一个名为radius的属性,它将产生一个错误,因为该对象的属性不存在。

答案 1 :(得分:1)

File "/var/www/comunidadsm/env/local/lib/python2.7/site-packages/allspeak/reader.py", line 79, in _extract_locales
for locale, trans in data.items()
AttributeError: 'NoneType' object has no attribute 'items'

Perhaps changing that line to:
for locale,trans in (data.items() if data else [])

Not sure if that syntax will work right here, but it is an attempt

如果数据不是None则传递空列表,跳过循环。 可能不是最好的解决方案,但你可以找出结果 在它之后。