Mod_wsgi Apache Bottle.py: 无法导入 python 模块 "server"
Mod_wsgi Apache Bottle.py: Cannot import python module "server"
我正在尝试同时使用 Bottle.py 和 mod_wsgi,但出现了一个我无法修复的奇怪错误。
当我尝试发出 curl 请求时("curl http://localhost/myapi/hello1") I get the error: "ImportError: No module named server". However, if I take out the "import server" line in my app.wsgi file and try "curl http://localhost/myapi/hello2" 它工作得很好。我尝试添加到 WSGIPythonPath、WSGIDaemonProcess 等,但没有任何效果。有没有人看到我可能正在做的事情错了?
注意:“/usr/local/www/wsgi-script”路径已成功添加到 sys.path,因此这不是问题所在。我在“/usr/local/www/wsgi-scripts”目录下也确实有一个init.py文件,权限都是777.
[root@my_vm httpd]# ls -lt /usr/local/www/wsgi-scripts/
total 8
-rwxrwxrwx. 1 root root 95 Mar 21 14:42 server.py
-rwxrwxrwx. 1 root root 318 Mar 21 14:37 app.wsgi
-rwxrwxrwx. 1 root root 0 Mar 20 19:53 __init__.py
使用版本:
Apache/2.4.6(红帽企业 Linux)
Python 2.7.5
瓶子 0.12.9
/usr/local/www/wsgi-scripts/server.py
import bottle
from bottle import route
@route('/hello1')
def hello():
return "Hello World!"
/usr/local/www/wsgi-scripts/app.wsgi
import os
import sys
import bottle
from bottle import route
server_loc = "/usr/local/www/wsgi-scripts"
if not server_loc in sys.path:
sys.path.insert(0, server_loc)
os.chdir(os.path.dirname(__file__))
print(sys.path)
@route('/hello2')
def hello():
return "Hello World!\n"
import server
application = bottle.default_app()
/etc/httpd/conf.d/myapi.conf
<VirtualHost *:80>
ServerName localhost
WSGIScriptAlias /myapi /usr/local/www/wsgi-scripts/app.wsgi
<Directory /usr/local/www/wsgi-scripts>
Require all granted
</Directory>
</VirtualHost>
编辑:添加python回溯
/etc/httpd/logs/error_log
[Thu Mar 22 13:39:18.234573 2018] [core:notice] [pid 18172] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
[Thu Mar 22 13:39:29.628352 2018] [:error] [pid 18174] ['/usr/local/www/wsgi-scripts', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages']
[Thu Mar 22 13:39:29.628514 2018] [:error] [pid 18174] [client ::1:35838] mod_wsgi (pid=18174): Target WSGI script '/usr/local/www/wsgi-scripts/app.wsgi' cannot be loaded as Python module.
[Thu Mar 22 13:39:29.628525 2018] [:error] [pid 18174] [client ::1:35838] mod_wsgi (pid=18174): Exception occurred processing WSGI script '/usr/local/www/wsgi-scripts/app.wsgi'.
[Thu Mar 22 13:39:29.628539 2018] [:error] [pid 18174] [client ::1:35838] Traceback (most recent call last):
[Thu Mar 22 13:39:29.628556 2018] [:error] [pid 18174] [client ::1:35838] File "/usr/local/www/wsgi-scripts/app.wsgi", line 18, in <module>
[Thu Mar 22 13:39:29.628625 2018] [:error] [pid 18174] [client ::1:35838] import server
[Thu Mar 22 13:39:29.628645 2018] [:error] [pid 18174] [client ::1:35838] ImportError: No module named server
添加我找到的对我有用的解决方案:
当我将 server.py 移动到目录内的另一个目录(又名 /usr/local/www/wsgi-scripts/server.py --> /usr/local/www/wsgi-scripts/inner_dir/server.py)时,执行 "import inner_dir.server" 而不是"import server",它开始工作了。
我正在尝试同时使用 Bottle.py 和 mod_wsgi,但出现了一个我无法修复的奇怪错误。 当我尝试发出 curl 请求时("curl http://localhost/myapi/hello1") I get the error: "ImportError: No module named server". However, if I take out the "import server" line in my app.wsgi file and try "curl http://localhost/myapi/hello2" 它工作得很好。我尝试添加到 WSGIPythonPath、WSGIDaemonProcess 等,但没有任何效果。有没有人看到我可能正在做的事情错了?
注意:“/usr/local/www/wsgi-script”路径已成功添加到 sys.path,因此这不是问题所在。我在“/usr/local/www/wsgi-scripts”目录下也确实有一个init.py文件,权限都是777.
[root@my_vm httpd]# ls -lt /usr/local/www/wsgi-scripts/
total 8
-rwxrwxrwx. 1 root root 95 Mar 21 14:42 server.py
-rwxrwxrwx. 1 root root 318 Mar 21 14:37 app.wsgi
-rwxrwxrwx. 1 root root 0 Mar 20 19:53 __init__.py
使用版本:
Apache/2.4.6(红帽企业 Linux)
Python 2.7.5
瓶子 0.12.9
/usr/local/www/wsgi-scripts/server.py
import bottle
from bottle import route
@route('/hello1')
def hello():
return "Hello World!"
/usr/local/www/wsgi-scripts/app.wsgi
import os
import sys
import bottle
from bottle import route
server_loc = "/usr/local/www/wsgi-scripts"
if not server_loc in sys.path:
sys.path.insert(0, server_loc)
os.chdir(os.path.dirname(__file__))
print(sys.path)
@route('/hello2')
def hello():
return "Hello World!\n"
import server
application = bottle.default_app()
/etc/httpd/conf.d/myapi.conf
<VirtualHost *:80>
ServerName localhost
WSGIScriptAlias /myapi /usr/local/www/wsgi-scripts/app.wsgi
<Directory /usr/local/www/wsgi-scripts>
Require all granted
</Directory>
</VirtualHost>
编辑:添加python回溯
/etc/httpd/logs/error_log
[Thu Mar 22 13:39:18.234573 2018] [core:notice] [pid 18172] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
[Thu Mar 22 13:39:29.628352 2018] [:error] [pid 18174] ['/usr/local/www/wsgi-scripts', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages']
[Thu Mar 22 13:39:29.628514 2018] [:error] [pid 18174] [client ::1:35838] mod_wsgi (pid=18174): Target WSGI script '/usr/local/www/wsgi-scripts/app.wsgi' cannot be loaded as Python module.
[Thu Mar 22 13:39:29.628525 2018] [:error] [pid 18174] [client ::1:35838] mod_wsgi (pid=18174): Exception occurred processing WSGI script '/usr/local/www/wsgi-scripts/app.wsgi'.
[Thu Mar 22 13:39:29.628539 2018] [:error] [pid 18174] [client ::1:35838] Traceback (most recent call last):
[Thu Mar 22 13:39:29.628556 2018] [:error] [pid 18174] [client ::1:35838] File "/usr/local/www/wsgi-scripts/app.wsgi", line 18, in <module>
[Thu Mar 22 13:39:29.628625 2018] [:error] [pid 18174] [client ::1:35838] import server
[Thu Mar 22 13:39:29.628645 2018] [:error] [pid 18174] [client ::1:35838] ImportError: No module named server
添加我找到的对我有用的解决方案:
当我将 server.py 移动到目录内的另一个目录(又名 /usr/local/www/wsgi-scripts/server.py --> /usr/local/www/wsgi-scripts/inner_dir/server.py)时,执行 "import inner_dir.server" 而不是"import server",它开始工作了。