【问题标题】:ImportError: No module named bottle. Python3.5.1ImportError:没有名为瓶子的模块。 Python3.5.1
【发布时间】:2017-10-12 10:02:54
【问题描述】:

我想将 python 3.5 与瓶子和 apache 一起使用。

但是当我从浏览器访问adapter.wsgi时,

内部服务器错误

▼error_log

[error] ImportError: No module named os

[error] ImportError: No module named bottle


应用

▼/etc/httpd/conf.d/wsgi.conf

LoadModule wsgi_module modules/mod_wsgi.so

WSGIPythonHome /opt/rh/rh-python35/root/usr/bin/python3

<FilesMatch \.wsgi$>
    SetHandler wsgi-script
    Options +ExecCGI
</FilesMatch>

<FilesMatch \.py$>
    SetHandler wsgi-script
    Options +ExecCGI
</FilesMatch>

▼adapter.wsgi

# -*- coding:utf-8 -*-
import sys, os
dirpath = os.path.dirname(os.path.abspath(__file__))
sys.path.append(dirpath)
os.chdir(dirpath)
import bottle
import index
application = bottle.default_app()

▼index.py

# -*- coding:utf-8 -*-
from bottle import route, run, template
from bottle import TEMPLATE_PATH

@route('/')
def index():
    return "HELLO WORLD!"

if __name__ == '__main__':
    run(host='hogetest.com', port=80, debug=True, reloader=True)

现状

$ python -V     

Python 3.5.1

$ which python  

别名 python='/opt/rh/rh-python35/root/usr/bin/python3'

    /opt/rh/rh-python35/root/usr/bin/python3

【问题讨论】:

  • 那么您是在问如何删除python 2.6.6,因为您不使用它或如何修复ImportError?另外,如果你有 python 2.6.6 并且想要使用 python 3 模块并不重要,重要的是你使用哪个 python 解释器。
  • 你不想删除预装系统python 2.6,它好吧导致问题
  • 我想使用 Python 3 模块。我该如何解决这个错误?

标签: python apache mod-wsgi bottle


【解决方案1】:

在您的终端中使用此命令将 python3 用作所有与 python 相关的工作的默认值

alias python='/usr/bin/python3'

如果出现权限错误,可以在开头使用sudo

【讨论】:

    【解决方案2】:

    您的WSGIPythonHome 指令一开始就错误。尝试改用:

    WSGIPythonHome /opt/rh/rh-python35/root/usr
    

    参数应该与sys.prefix 用于 Python 安装的参数相同。

    不过,使用 SCL Python 版本可能会导致其他复杂性。

    如果这不起作用,请找出 Python 共享库的 .so 的完整路径并使用:

    LoadFile /opt/rh/rh-python35/root/usr/lib/libpython3.5.so
    LoadModule wsgi_module modules/mod_wsgi.so
    WSGIPythonHome /opt/rh/rh-python35/root/usr
    

    更改LoadFile 指令以匹配共享库的实际完整路径。它可以改为使用名称libpython3.5m.so

    根据您从哪里获得mod_wsgi.so 文件,即使这样也可能不起作用。如果那来自系统mod_wsgi 包,那么它将不会针对 SCL Python 版本进行编译。在这种情况下,您将不得不卸载系统mod_wsgi 包并自己从源代码编译mod_wsgi,最好使用pip install 方法,然后使用mod_wsgi-express module-config 提供的配置。见:

    【讨论】:

    • 我尝试回答但错误“目标 WSGI 脚本无法作为 Python 模块加载。”最后我重做了一切,我用 make install python 3.6 成功完成了。
    猜你喜欢
    • 2012-02-25
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 2020-07-22
    • 2013-03-11
    相关资源
    最近更新 更多