2012-01-20 179 views
6

我創建了這個目錄結構的蟒蛇網絡應用:無法導入模塊

# cd /usr/local/www/myapp 

modules 
    layout 
     __init__.py 
     layout.py 
packages 
public 
myapp.wsgi 

我已經把我的PYTHONPATH到:

/usr/local/www/myapp/modules:/usr/local/www/myapp/packages 

在myapp.wsgi我嘗試這樣做:

import layout 

但我得到內部服務器錯誤。爲什麼?

這是我myapp.wsgi(如果我刪除了進口輪廓線,它的工作原理):

import sys 
import wsgiref 
import layout  
def application(environ, start_response): 
     response_status = '200 OK' 
     response_body = 'Hello! ' 
     response_headers = [] 
     content_type = ('Content-type', 'text-plain') 
     content_length = ('Content-Length', str(len(response_body))) 
     response_headers.append(content_type) 
     response_headers.append(content_length) 
     start_response(response_status, response_headers) 
     return [response_body] 

完整的錯誤消息我得到:

Internal Server Error 

The server encountered an internal error or misconfiguration and was unable to complete your request. 

Please contact the server administrator, [email protected] and inform them of the time the error occurred, and anything you might have done that may have caused the error. 

More information about this error may be available in the server error log. 

我的虛擬主機配置:

<VirtualHost *:80> 

    ServerName localhost 
    ServerAlias localhost 
    ServerAdmin [email protected] 

    DocumentRoot /usr/local/www/myapp/public 

    <Directory /usr/local/www/myapp/public> 
    Order allow,deny 
    Allow from all 
    </Directory> 

    WSGIScriptAlias//usr/local/www/myapp/myapp.wsgi 

    <Directory /usr/local/www/myapp> 
    Order allow,deny 
    Allow from all 
    </Directory> 

</VirtualHost> 

錯誤來自/var/log/httpd-error.log:

[Fri Jan 20 15:31:03 2012] [error] [client 192.168.201.123] mod_wsgi (pid=1725): Target WSGI script '/usr/local/www/myapp/myapp.wsgi' cannot be loaded as Python module. 
[Fri Jan 20 15:31:03 2012] [error] [client 192.168.201.123] mod_wsgi (pid=1725): Exception occurred processing WSGI script '/usr/local/www/myapp/myapp.wsgi'. 
[Fri Jan 20 15:31:03 2012] [error] [client 192.168.201.123] Traceback (most recent call last): 
[Fri Jan 20 15:31:03 2012] [error] [client 192.168.201.123] File "/usr/local/www/myapp/myapp.wsgi", line 3, in <module> 
[Fri Jan 20 15:31:03 2012] [error] [client 192.168.201.123]  import layout 
[Fri Jan 20 15:31:03 2012] [error] [client 192.168.201.123] ImportError: No module named layout 

輸出打印的sys.path的:

enter image description here

+1

至少包含您正在收到的完整且準確的錯誤消息。 – unwind

+0

你如何運行網絡服務器?這很可能不會使用您的環境,因此爲您自己的shell設置PYTHONPATH將無濟於事。 – geoffspear

+0

我使用Apache mod_wsgi,併爲/ usr/local/www/myapp創建了一個虛擬主機。 –

回答

8

首先嚐試:

python /usr/local/www/myapp/myapp.wsgi 

是否正確裝入?

如果是,那麼可能你有一些環境(在~/.bashrc等),這是你的應用程序所需要的。嘗試::

# to wipe-out extra env 
env -i bash 
# try again 
python /usr/local/www/myapp/myapp.wsgi 

驗證您的shell中使用的是與Apache WSGI使用的python相同的python。

如果您myapp.wsgi需要任何額外的ENV正確加載,那麼你可以做的一個:

  • 設置python path在Apache中,或
  • 在運行時設置您myapp.wsgi

要在你的WSGI代碼中設置,這裏是示例代碼。

import os, sys 
EXTRA_DIR = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', '..')) 
if EXTRA_DIR not in sys.path: 
    sys.path.append(EXTRA_DIR) 

放入您的myapp.wsgi文件的開頭。

+0

我嘗試了所有這些。沒有工作。 env -i bash不起作用,因爲我使用的是FreeBSD。 –

+0

如果您不能執行'env -i',則手動檢查您的環境'env'命令和您的配置文件腳本。你使用virtualenv還是這樣的? –

+0

'python/usr/local/www/myapp/myapp.wsgi'的輸出是什麼? –

1

你有__init.__pylayout文件夾,但它應該是__init__.py。這個時期是錯位的。我不確定這是否是您的文章中的拼寫錯誤,但如果這是您的文件看起來會導致此問題。

+0

這只是一個錯字。抱歉。 –

0

模塊目錄還需要一個__init__.py文件被定義爲一個包。

+1

'modules'不是一個包(根據指定的'PYTHONPATH')。 – jrennie

0

我也有類似的問題,這解決了它:

chmod a+x myapp.wsgi 
0

根據http://webpy.org/install#apachemodwsgi

If you get an "ImportError: No module named web" in your apache error.log file, you could try setting the absolute path in code.py before importing web:

import sys, os 
abspath = os.path.dirname(__file__) 
sys.path.append(abspath) 
os.chdir(abspath) 
import web 

它極大地爲我工作。