簡體   English   中英

WSGI: ImportError: No module named hello (module in the same directory of the main.py file)

[英]WSGI: ImportError: No module named hello (module in the same directory of the main .py file)

這不是帶有 virtualenv 和 mod_wsgi 的 Apache 的副本:ImportError: No module named 'django'因為這里我沒有使用任何 virtualenv,而且我也沒有嘗試導入另一個框架的模塊(例如 django),而只是同一目錄中的一個模塊。


這是我的設置:

/var/www/test/app.py

import os, time, sys
from bottle import route, run, template, default_app

os.chdir(os.path.dirname(os.path.abspath(__file__)))

import hello

@route('/')
def index():
    return 'Hello world Python ' + sys.version

application = default_app()

/var/www/test/hello.py

# just an example module
def test():
    print 'hello'

阿帕奇配置:

<VirtualHost *:80>
  ServerName example.com
  <Directory />
    Require all granted
  </Directory>
  WSGIScriptAlias / /var/www/test/app.py
  WSGIDaemonProcess test user=www-data group=www-data processes=5 threads=5 display-name=test python-path=/var/www/test/
</VirtualHost>

然后我得到:

導入錯誤:沒有名為 hello 的模塊

什么是不正確的? WSGIDaemonProcess... python-path=/var/www/test/不應該幫助加載模塊hello嗎?

Apache 不會更改到當前工作目錄,因此它不會在您認為的位置搜索 hello。

您可以通過以下方式更改 app.py。

import os, time, sys
from bottle import route, run, template, default_ap
# add the scripts directory to the python path so that hello can be found
sys.path.insert(0, os.path.realpath(os.path.dirname(__file__)))

優點是您不必弄亂 apache 配置文件

解決辦法是:

  • 有一個WSGIDaemonProcess... python-path=...事實上,

  • 還有一個WSGIScriptAlias... process-group=... (不知道為什么這個process-group參數鏈接到允許或不允許從同一目錄加載模塊,但它有效!)

例子:

WSGIScriptAlias / /var/www/test/app.py process-group=test
WSGIDaemonProcess test user=www-data group=www-data processes=5 threads=5 display-name=test python-path=/var/www/test/

另請參閱: https ://bottlepy.org/docs/dev/deployment.html#apache-mod-wsgi

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM