2012-09-02 67 views
1

我無法讓我的CentOS 6/Apache/2.2.15上運行這個django應用程序。當我運行應用程序,我得到這個錯誤:TemplateSyntaxError:在呈現時捕獲ImportError:無法導入名稱utils

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 

搜索這個錯誤我已經找到了幾個解決方案,嘗試了所有,但沒有工作:

  1. 製作WSGI運行在守護進程模式。
  2. 檢查Django安裝路徑是否在Python PAth中。
  3. 我認爲它可能是運行在同一個Apache VirtualHost上的另一個應用程序,所以我把另一個VH(另一個端口),但沒有工作。
  4. 我有2分貝連接:Oracle和PostgreSQL在這個應用程序,甲骨文是好的,也許問題是postgresql_psycopg2。我已經安裝好了。

這裏是我的文件:

vhost.conf:

NameVirtualHost 10.7.0.17:80 
    NameVirtualHost 10.7.0.17:8080 

    <VirtualHost 10.7.0.17:80> 
     ServerAdmin [email protected] 
     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 [email protected] 
     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

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() 

提前任何幫助感謝。

+0

剛剛檢查psycopg2,它導入OK。問題依然存在。 – lmisael

+0

'sys.path.append('/ usr/local/lib/python2.7/site-packages/django')'是什麼原因?如果你想在'sys.path'中添加'django',你應該添加django所在的根文件夾(即'sys.path.append('/ usr/local/lib/python2.7/site-packages') ') – yedpodtrzitko

+0

感謝您的建議,我會解決它。事實上,我只需要把它拿出來,因爲site-packages已經在PythonPath中了。 – lmisael

回答

3

過去的原因之一是模塊導入路徑問題。

具體而言,使用針對一個版本的Python編譯的mod_wsgi安裝,然後使用WSGIPythonPath或其他方式設置Python路徑來引用使用不同Python版本構建的Python虛擬環境。

因此,驗證編譯和安裝的Python mod_wsgi的版本。如果這不是Python 2.7,而是一個較老的Python版本,那麼可以得到這個特定的問題。

對於如何檢查什麼的mod_wsgi編譯使用:

http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation#Python_Shared_Library

http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation#Python_Installation_In_Use

它也可以看的mod_wsgi編譯什麼時候是Python中的有價值的檢查補丁級別版本的差異,以及對於。我確實記得在Python 2.7.1和2.7.2之間有這樣一個問題。

您的Python站點包位於/ usr/local之下,因此mod_wsgi很有可能在/ usr下使用不同的版本。

+0

我必須通過在.bash_profile中將我的django應用程序路徑添加到PYTHON_PATH來解決問題 – lmisael

+0

我的mod_wsgi是使用Python Django使用的相同版本編譯的。我想這沒有問題。非常感謝您的幫助! – lmisael

+0

這是PYTHONPATH,而不是PYTON_PATH,Apache甚至不應該使用它。用mod_wsgi.so上運行ldd的結果修改你的問題,確保你沒有設置LD_LIBRARY_PATH,所以我們可以驗證是正確的。同時指出您是否有用於全系統Python安裝的/usr/bin/python2.7。因爲在非標準位置中有第二個Python 2.7,除非您設置WSGIPythonHome來使用它,否則它仍然會使用系統Python 2.7(如果存在)。所以請加上質疑詢問你的結果,以便確認。 –

0

,以防有人絆倒此同時尋找在金字塔這一問題,並SQLAlchemy的:

與金字塔相結合,我有同樣的錯誤(「導入錯誤:無法導入名UTIL」)。經過試驗的時間和錯誤的唯一原因,原來,在production.ini我已經設置:

[server:main] 
use = egg:waitress#main 
host = 0.0.0.0 
port = 80 

我還對80端口的Apache2使用,似乎有些奇怪,這應該是相同的。將端口改爲例如6543,錯誤消失,WSGI服務器工作。在必要時,不需要apache2設置(WSGIPythonHome,WSGIPythonPath)。

顯然不相關,我不知道爲什麼端口衝突導致了這個錯誤,但它可能會幫助別人解決這個問題。

相關問題