2012-07-08 47 views
0

這是一件很奇怪的事情,我無法理解它。無法在apache服務器上的django中加載模塊錯誤

這是短暫的

project/ 
     apache/ django.wsgi 
     project/ __init__.py, settings.py, urls.py .. 
     services/ 
       __init__.py 
       apis/ 
        __init__.py 
        fparser.py 
       wservice.py 
     ... 

     profile.py 

所以我的Django的文件目錄結構,一切都運行在開發服務器,甚至在Heroku(gunicorn)罰款,但不工作在Apache(本地主機)

時我打開的頁面:

其顯示

Exception Type: ImportError at/
Exception Value: cannot import name website_feed_address 

這個website_feed_address位於profile.py import error被發現在fparser.py

我該如何解決它?

編輯:

django.wsgi

import os, sys 

sys.path.append('d:/code/projects-dev/project') 


os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings' 

import django.core.handlers.wsgi 

application = django.core.handlers.wsgi.WSGIHandler() 

,而在上面的問題中提到的項目目錄層次結構是d:/code/projects-dev/

編輯2

這些都是Apache日誌錯誤

[Sun Jul 08 23:14:04 2012] [notice] Parent: Received restart signal -- Restarting the server. 
httpd.exe: Could not reliably determine the server's fully qualified domain name, using 124.123.136.220 for ServerName 
[Sun Jul 08 23:14:04 2012] [warn] mod_wsgi: Compiled for Python/2.7. 
[Sun Jul 08 23:14:04 2012] [warn] mod_wsgi: Runtime using Python/2.7.2. 
[Sun Jul 08 23:14:05 2012] [notice] Child 5912: Exit event signaled. Child process is ending. 
[Sun Jul 08 23:14:05 2012] [warn] RSA server certificate CommonName (CN) `127.0.0.1' does NOT match server name!? 
[Sun Jul 08 23:14:05 2012] [notice] Apache/2.2.22 (Win32) mod_wsgi/3.3 Python/2.7.2 mod_ssl/2.2.22 OpenSSL/0.9.8t configured -- resuming normal operations 
[Sun Jul 08 23:14:05 2012] [notice] Server built: Jan 28 2012 11:16:39 
[Sun Jul 08 23:14:05 2012] [notice] Parent: Created child process 2120 
httpd.exe: Could not reliably determine the server's fully qualified domain name, using 124.123.136.220 for ServerName 
[Sun Jul 08 23:14:05 2012] [warn] RSA server certificate CommonName (CN) `127.0.0.1' does NOT match server name!? 
httpd.exe: Could not reliably determine the server's fully qualified domain name, using 124.123.136.220 for ServerName 
[Sun Jul 08 23:14:05 2012] [warn] mod_wsgi: Compiled for Python/2.7. 
[Sun Jul 08 23:14:05 2012] [warn] mod_wsgi: Runtime using Python/2.7.2. 
[Sun Jul 08 23:14:05 2012] [warn] RSA server certificate CommonName (CN) `127.0.0.1' does NOT match server name!? 
[Sun Jul 08 23:14:05 2012] [notice] Child 2120: Child process is running 
[Sun Jul 08 23:14:06 2012] [notice] Child 2120: Acquired the start mutex. 
[Sun Jul 08 23:14:06 2012] [notice] Child 5912: Released the start mutex 
[Sun Jul 08 23:14:06 2012] [notice] Child 2120: Starting 64 worker threads. 
[Sun Jul 08 23:14:06 2012] [notice] Child 2120: Starting thread to listen on port 443. 
[Sun Jul 08 23:14:06 2012] [notice] Child 2120: Starting thread to listen on port 80. 
[Sun Jul 08 23:14:07 2012] [notice] Child 5912: Terminating 126 threads that failed to exit. 
[Sun Jul 08 23:14:07 2012] [notice] Child 5912: All worker threads have exited. 
[Sun Jul 08 23:14:07 2012] [notice] Child 5912: Child process is exiting 

編輯3

這是怎麼profile.py,fparser.py看

profile.py只包含一些變量,元組就像settings.py。它只是在這種情況下導入變量website_feed_address

這是fparser.py

from profile import website_feed_address 
import feedparser 


class FParser(object): 
     def __init__(self): 
      self.pFeed = feedparser.parse(website_feed_address) 
     # rest of the code goes... 

我只是想使用FParser類website_feed_address沒有 把它作爲一個對象參數。有沒有更好的方法呢?或者 別的,我可以像這樣使用它嗎?

+0

你如何定義Apache使用的PYTHONPATH?你如何導入'website_feed_address'? – Rodrigue 2012-07-08 15:34:44

+0

@Rodrigue以及我使用Eclipse ..大部分PATH類的東西都由它來照顧。關於django.wsgi,我在上面的問題中增加了更多細節。請檢查它 – Surya 2012-07-08 15:39:44

回答

1

最有可能的問題是,你在你的項目中使用包相對import語句有,但是Apache不正確的Python路徑中的知識。例如,有了這個特定的導入錯誤,我假設你的fparser.py你正在做類似from profile import website_feed_address的事情。您可以嘗試在您的django中明確包含您的項目包。WSGI:

sys.path.append('d:/code/projects-dev/project') 
sys.path.append('d:/code/projects-dev/project/project') 

一個更好的建議是,你設置了virtualenv爲你的項目。這樣做的好處將允許您的項目包含自己的站點包環境,這樣您安裝的任何依賴項都將與項目環境保持一致。然後在Apache中,你可以告訴mod_wsgi的地方Python環境是:

# or wherever you decide to create the virtualenv 
WSGIPythonHome d:/code/projects-dev/project/project 

通過Apache設置PYTHONPATH

您也可以嘗試setting the PYTHONPATH爲在Apache的conf你的WSGI應用:

WSGIPythonPath "d:/code/projects-dev/project/project" 

*請注意,該鏈接區分了wsgi嵌入模式和守護進程模式。

完全替代辦法來解決進口問題

雖然我以前的建議正在解決的PYTHONPATH和導入您的profile模塊的基礎上,您只需使用它在一個恆定的通過,我將事實建議你只是利用settings.py的,因爲那是什麼它是(申報常數):

settings.py

WEBSITE_FEED_ADDRESS = "http://foo.com" 

fparser.py

from django.conf import settings 

... 
    self.pFeed = feedparser.parse(settings.WEBSITE_FEED_ADDRESS) 

您可以相當肯定的是,Django的環境將始終提供給您的設置模塊。常用的方法是讓應用程序添加到設置中,以提供默認常量。

+0

保存虛擬env一段時間。我會認爲這是最後的選擇。我不知道爲什麼..但我只是不喜歡每次都用虛擬環境下車.. – Surya 2012-07-08 18:15:18

+0

我添加了第二行sys.path.append ..它不工作。 – Surya 2012-07-08 18:15:56

+1

我認爲virtualenv是一個很好的開始。它始終確保您的項目具有封閉的環境,並且您不依賴外部包。 – jdi 2012-07-08 18:23:25

0

如果將profile.py移動到項目文件夾中,它將位於包中。然後,你可以這樣做:

from project.profile import website_feed_address 
+0

profile.py很好。沒有錯誤 – Surya 2012-07-08 18:13:15

相關問題