渲染时的ImportError:没有名为app的模块

时间:2013-01-30 18:39:44

标签: python django pythonanywhere

我正在pythonanywhere上运行一个Twitter应用程序,该应用程序在localhost开发服务器上运行良好,但是当我在pythonanywhere上运行时,我收到此错误:

2013-01-30 20:04:12,843 :Traceback (most recent call last):
2013-01-30 20:04:12,843 :  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 272, in __call__
2013-01-30 20:04:12,843 :    response = self.get_response(request)
2013-01-30 20:04:12,844 :  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 169, in get_response
2013-01-30 20:04:12,844 :    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
2013-01-30 20:04:12,844 :  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 203, in handle_uncaught_exception
2013-01-30 20:04:12,844 :    return debug.technical_500_response(request, *exc_info)
2013-01-30 20:04:12,844 :  File "/usr/local/lib/python2.7/site-packages/django/views/debug.py", line 59, in technical_500_response
2013-01-30 20:04:12,845 :    html = reporter.get_traceback_html()
2013-01-30 20:04:12,845 :  File "/usr/local/lib/python2.7/site-packages/django/views/debug.py", line 151, in get_traceback_html
2013-01-30 20:04:12,845 :    return t.render(c)
2013-01-30 20:04:12,845 :  File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 123, in render
2013-01-30 20:04:12,845 :    return self._render(context)
2013-01-30 20:04:12,845 :  File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 117, in _render
2013-01-30 20:04:12,845 :    return self.nodelist.render(context)
2013-01-30 20:04:12,845 :  File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 744, in render
2013-01-30 20:04:12,846 :    bits.append(self.render_node(node, context))
2013-01-30 20:04:12,846 :  File "/usr/local/lib/python2.7/site-packages/django/template/debug.py", line 73, in render_node
2013-01-30 20:04:12,846 :    result = node.render(context)
2013-01-30 20:04:12,846 :  File "/usr/local/lib/python2.7/site-packages/django/template/debug.py", line 90, in render
2013-01-30 20:04:12,846 :    output = self.filter_expression.resolve(context)
2013-01-30 20:04:12,846 :  File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 536, in resolve
2013-01-30 20:04:12,847 :    new_obj = func(obj, *arg_vals)
2013-01-30 20:04:12,847 :  File "/usr/local/lib/python2.7/site-packages/django/template/defaultfilters.py", line 695, in date
2013-01-30 20:04:12,847 :    return format(value, arg)
2013-01-30 20:04:12,847 :  File "/usr/local/lib/python2.7/site-packages/django/utils/dateformat.py", line 285, in format
2013-01-30 20:04:12,847 :    return df.format(format_string)
2013-01-30 20:04:12,847 :  File "/usr/local/lib/python2.7/site-packages/django/utils/dateformat.py", line 30, in format
2013-01-30 20:04:12,847 :    pieces.append(force_unicode(getattr(self, piece)()))
2013-01-30 20:04:12,848 :  File "/usr/local/lib/python2.7/site-packages/django/utils/dateformat.py", line 191, in r
2013-01-30 20:04:12,848 :    return self.format('D, j M Y H:i:s O')
2013-01-30 20:04:12,848 :  File "/usr/local/lib/python2.7/site-packages/django/utils/dateformat.py", line 30, in format
2013-01-30 20:04:12,848 :    pieces.append(force_unicode(getattr(self, piece)()))
2013-01-30 20:04:12,848 :  File "/usr/local/lib/python2.7/site-packages/django/utils/encoding.py", line 71, in force_unicode
2013-01-30 20:04:12,848 :    s = unicode(s)
2013-01-30 20:04:12,848 :  File "/usr/local/lib/python2.7/site-packages/django/utils/functional.py", line 206, in __unicode_cast
2013-01-30 20:04:12,848 :    return self.__func(*self.__args, **self.__kw)
2013-01-30 20:04:12,849 :  File "/usr/local/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 81, in ugettext
2013-01-30 20:04:12,849 :    return _trans.ugettext(message)
2013-01-30 20:04:12,849 :  File "/usr/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 286, in ugettext
2013-01-30 20:04:12,849 :    return do_translate(message, 'ugettext')
2013-01-30 20:04:12,849 :  File "/usr/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 276, in do_translate
2013-01-30 20:04:12,849 :    _default = translation(settings.LANGUAGE_CODE)
2013-01-30 20:04:12,849 :  File "/usr/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 185, in translation
2013-01-30 20:04:12,850 :    default_translation = _fetch(settings.LANGUAGE_CODE)
2013-01-30 20:04:12,850 :  File "/usr/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 162, in _fetch
2013-01-30 20:04:12,850 :    app = import_module(appname)
2013-01-30 20:04:12,850 :  File "/usr/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
2013-01-30 20:04:12,850 :    __import__(name)
2013-01-30 20:04:12,850 :django.template.base.TemplateSyntaxError: Caught ImportError while rendering: No module named twitterApp

可能导致此错误的原因是什么?我应该在哪里看?

2 个答案:

答案 0 :(得分:3)

如果可能是由pythonanywhere中的wsgi.py配置问题引起的,在本地env中,django处理python路径和静态文件很好。 根据pythonanywhere部署文档中的part

  1. 在wsgi.py
  2. 中添加到sys.path的正确路径 假设您的Django设置文件位于/home/my_username/projects/my_project/settings.py'
    path = '/home/my_username/projects'
    if path not in sys.path:
        sys.path.append(path)
    os.environ['DJANGO_SETTINGS_MODULE'] = 'my_project.settings'
    from my_project.myapp.models import Kitchen, Sink
    

    请注意:需要my_project前缀。

    2.static文件应该处理好

    from django.contrib.staticfiles.urls import staticfiles_urlpatterns
    urlpatterns += staticfiles_urlpatterns()
    

    根据文件检查这两点应该修复

答案 1 :(得分:1)

我的应用程序存在完全相同的问题...问题原来是文件权限。如果app目录没有执行权限,则无法导入。