[英]TemplateSyntaxError: Caught ImportError while rendering: cannot import name utils
I'm having trouble to get this django application running on my CentOS 6 / Apache/2.2.15. 我在CentOS 6 / Apache / 2.2.15上运行这个django应用程序时遇到了麻烦。 When I run the app I get this error:
当我运行应用程序时,我收到此错误:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 272, in __call__
response = self.get_response(request)
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 169, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 203, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/usr/local/lib/python2.7/site-packages/django/views/debug.py", line 59, in technical_500_response
html = reporter.get_traceback_html()
File "/usr/local/lib/python2.7/site-packages/django/views/debug.py", line 151, in get_traceback_html
return t.render(c)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 123, in render
return self._render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 117, in _render
return self.nodelist.render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 744, in render
bits.append(self.render_node(node, context))
File "/usr/local/lib/python2.7/site-packages/django/template/debug.py", line 73, in render_node
result = node.render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/debug.py", line 90, in render
output = self.filter_expression.resolve(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 536, in resolve
new_obj = func(obj, *arg_vals)
File "/usr/local/lib/python2.7/site-packages/django/template/defaultfilters.py", line 695, in date
return format(value, arg)
File "/usr/local/lib/python2.7/site-packages/django/utils/dateformat.py", line 285, in format
return df.format(format_string)
File "/usr/local/lib/python2.7/site-packages/django/utils/dateformat.py", line 30, in format
pieces.append(force_unicode(getattr(self, piece)()))
File "/usr/local/lib/python2.7/site-packages/django/utils/dateformat.py", line 191, in r
return self.format('D, j M Y H:i:s O')
File "/usr/local/lib/python2.7/site-packages/django/utils/dateformat.py", line 30, in format
pieces.append(force_unicode(getattr(self, piece)()))
File "/usr/local/lib/python2.7/site-packages/django/utils/encoding.py", line 71, in force_unicode
s = unicode(s)
File "/usr/local/lib/python2.7/site-packages/django/utils/functional.py", line 206, in __unicode_cast
return self.__func(*self.__args, **self.__kw)
File "/usr/local/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 81, in ugettext
return _trans.ugettext(message)
File "/usr/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 286, in ugettext
return do_translate(message, 'ugettext')
File "/usr/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 276, in do_translate
_default = translation(settings.LANGUAGE_CODE)
File "/usr/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 185, in translation
default_translation = _fetch(settings.LANGUAGE_CODE)
File "/usr/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 162, in _fetch
app = import_module(appname)
File "/usr/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/__init__.py", line 3, in <module>
from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/helpers.py", line 3, in <module>
from django.contrib.admin.util import (flatten_fieldsets, lookup_field,
File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/util.py", line 1, in <module>
from django.db import models
File "/usr/local/lib/python2.7/site-packages/django/db/__init__.py", line 78, in <module>
connection = connections[DEFAULT_DB_ALIAS]
File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 93, in __getitem__
backend = load_backend(db['ENGINE'])
File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 33, in load_backend
return import_module('.base', backend_name)
File "/usr/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/usr/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 9, in <module>
from django.db import utils
TemplateSyntaxError: Caught ImportError while rendering: cannot import name utils
Searching for this error I've found a few solutions, tried all but none worked: 搜索这个错误我找到了一些解决方案,尝试过但都没有工作:
Here's my files: 这是我的文件:
vhost.conf: vhost.conf:
NameVirtualHost 10.7.0.17:80
NameVirtualHost 10.7.0.17:8080
<VirtualHost 10.7.0.17:80>
ServerAdmin webmaster@www.domain.com
ServerName www.domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/www.domain.com/html/
ErrorLog /var/www/www.domain.com/logs/error.log
CustomLog /var/www/www.domain.com/logs/access.log combined
WSGIDaemonProcess www.domain.com processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup www.domain.com
Alias /vlv/media/ /var/www/www.domain.com/django/vlv_vistoria/media/
WSGIScriptAlias /vlv /var/www/www.domain.com/django/vlv_vistoria/django.wsgi
<Directory /var/www/www.domain.com/django/vlv_vistoria>
Order allow,deny
Allow from all
Options Indexes FollowSymLinks
</Directory>
<Directory /var/www/www.domain.com/django/vlv_vistoria/media>
Order allow,deny
Allow from all
Options Indexes FollowSymLinks
</Directory>
</VirtualHost>
<VirtualHost 10.7.0.17:8080>
ServerAdmin webmaster@www.domain.com
ServerName www.domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/www.domain.com/html/
ErrorLog /var/www/www.domain.com/logs/sistema_error.log
CustomLog /var/www/www.domain.com/logs/sistema_access.log combined
WSGIDaemonProcess sistema.www.domain.com processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup sistema.www.domain.com
Alias /sistema/media/ /var/www/www.domain.com/django/sistema/media/
WSGIScriptAlias /sistema /var/www/www.domain.com/django/sistema/django.wsgi
<Directory /var/www/www.domain.com/django/sistema>
Order allow,deny
Allow from all
Options Indexes FollowSymLinks
</Directory>
<Directory /var/www/www.domain.com/django/sistema/media>
Order allow,deny
Allow from all
Options Indexes FollowSymLinks
</Directory>
</VirtualHost>
django.wsgi django.wsgi
import os, sys
sys.path.append('/var/www/www.domain.com/django')
sys.path.append('/var/www/www.domain.com/django/sistema')
sys.path.append('/usr/local/lib/python2.7/site-packages/django')
root = os.path.join(os.path.dirname(__file__), '..')
sys.path.insert(0, root)
os.path.dirname(__file__)
sys.path.insert(0, root)
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
os.environ["ORACLE_HOME"] = "/ora00/app/oracle/product/client"
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Thanks in advance for any help. 在此先感谢您的帮助。
One of the causes for this in the past has been module import path issues. 其中一个原因是模块导入路径问题。
Specifically, using a mod_wsgi installation compiled against one version of Python, and then using WSGIPythonPath or other means of setting the Python path to refer to a Python virtual environment constructed using a different Python version. 具体来说,使用针对一个Python版本编译的mod_wsgi安装,然后使用WSGIPythonPath或其他设置Python路径的方法来引用使用不同Python版本构建的Python虚拟环境。
So, validate what version of Python mod_wsgi was compiled and installed for. 因此,验证为其编译和安装的Python mod_wsgi的版本。 If that is not Python 2.7, but an older Python version, you can get this specific problem.
如果这不是Python 2.7,而是较旧的Python版本,则可以解决此特定问题。
For how to check what mod_wsgi was compiled for use: 有关如何检查编译的mod_wsgi的用法:
http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation#Python_Shared_Library http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation#Python_Shared_Library
http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation#Python_Installation_In_Use http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation#Python_Installation_In_Use
It may also be worthwhile checking patch level revision differences of Python as well when looking at what mod_wsgi was compiled for. 在查看编译的mod_wsgi时,检查Python的补丁级别修订版差异也是值得的。 I do recollect that there was an issue a one point like this between Python 2.7.1 and 2.7.2 on there about.
我记得在Python 2.7.1和2.7.2之间存在一个问题。
That your Python site-packages is under /usr/local then quite possible that mod_wsgi using a different version under /usr. 您的Python站点包在/ usr / local之下,那么mod_wsgi很可能在/ usr下使用不同的版本。
In case someone stumbles over this while searching for this issue on Pyramid and sqlalchemy: 如果在Pyramid和sqlalchemy上搜索此问题时有人绊倒了这个:
In conjunction with Pyramid, I had the same error ("ImportError: cannot import name util"). 与Pyramid一起,我有同样的错误(“ImportError:无法导入名称util”)。 After hours of trial and error the only reason turned out that in production.ini I had set:
经过几个小时的反复试验,唯一的原因就是在production.ini中我设定了:
[server:main]
use = egg:waitress#main
host = 0.0.0.0
port = 80
I was also using Apache2 on port 80, seemed odd that this should be the same. 我也在端口80上使用Apache2,看起来很奇怪,这应该是相同的。 After changing the port to eg 6543, the error disappeared and the WSGI server worked.
将端口更改为例如6543后,错误消失并且WSGI服务器正常工作。 None of the apache2-settings (WSGIPythonHome,WSGIPythonPath) where necessary.
必要时,没有apache2-settings(WSGIPythonHome,WSGIPythonPath)。
Apparently unrelated and I don't know why the port-conflict caused this error, but it might help someone else with this issue. 显然不相关,我不知道为什么端口冲突导致了这个错误,但它可能会帮助其他人解决这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.