Mod_wsgi Apache Bottle.py:无法导入python模块“server”

时间:2018-03-21 14:52:35

标签: python apache server mod-wsgi bottle

我正在尝试一起使用Bottle.py和mod_wsgi,但我收到一个奇怪的错误,我无法修复。 当我尝试发出curl请求(“curl http://localhost/myapi/hello1”)时,我收到错误:“ImportError:No module named server”。但是,如果我在app.wsgi文件中取出“import server”行并尝试“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 traceback

的/ etc / httpd的/记录/ 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

1 个答案:

答案 0 :(得分:0)

添加我觉得有用的解决方案:

当我将server.py移动到目录中的另一个目录时(又名/usr/local/www/wsgi-scripts/server.py - &gt; / usr / local / www / wsgi-scripts / inner_dir / server .py),并执行“import inner_dir.server”而不是“import server”,它开始工作。

相关问题