首页 文章

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

提问于
浏览
0

我试图一起使用Bottle.py和mod_wsgi,但我得到一个我无法解决的奇怪错误 . 当我尝试发出curl请求时(“curl http://localhost/myapi/hello1 ") I get the error: " ImportError:没有名为server ". However, if I take out the " import server " line in my app.wsgi file and try " curl http://localhost/myapi/hello2的模块”它运行正常 . 我已经尝试添加到WSGIPythonPath,WSGIDaemonProcess等等,但没有任何工作 . 有没有人看到我可能做错了什么?

Note: "/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

Using Versions:

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

1 回答

  • 0

    添加我发现的解决方案对我有用:

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

相关问题