首页 > 解决方案 > ImportError:Apache 中没有名为 cx_Oracle 的模块

问题描述

我在 CentOS7 上用 apache 部署烧瓶

我希望 Oracle 数据库在用户访问时插入或选择。

所以我安装 cx_Oracle

pip install cx_Oracle

并在虚拟环境上运行烧瓶。

flask run

它可以正常工作。

但是我使用 apache 访问 localhost:80,它发生内部服务器错误 500。

在错误日志中

[Fri Oct 23 09:12:34.623473 2020] [mpm_prefork:notice] [pid 475] AH00170: caught SIGWINCH, shutting down gracefully
[Fri Oct 23 09:12:35.678302 2020] [lbmethod_heartbeat:notice] [pid 510] AH02282: No slotmem from mod_heartmonitor
[Fri Oct 23 09:12:35.681622 2020] [mpm_prefork:notice] [pid 510] AH00163: Apache/2.4.6 (CentOS) mod_wsgi/3.4 Python/2.7.5 configured -- resuming normal operations
[Fri Oct 23 09:12:35.681665 2020] [core:notice] [pid 510] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
[Fri Oct 23 09:12:38.074902 2020] [:error] [pid 513] [client 172.17.0.1:33640] mod_wsgi (pid=513): Target WSGI script '/var/www/hitme/wsgi.py' cannot be loaded as Python module.
[Fri Oct 23 09:12:38.074946 2020] [:error] [pid 513] [client 172.17.0.1:33640] mod_wsgi (pid=513): Exception occurred processing WSGI script '/var/www/hitme/wsgi.py'.
[Fri Oct 23 09:12:38.074973 2020] [:error] [pid 513] [client 172.17.0.1:33640] Traceback (most recent call last):
[Fri Oct 23 09:12:38.074994 2020] [:error] [pid 513] [client 172.17.0.1:33640]   File "/var/www/hitme/wsgi.py", line 10, in <module>
[Fri Oct 23 09:12:38.075058 2020] [:error] [pid 513] [client 172.17.0.1:33640]     from app import app as application
[Fri Oct 23 09:12:38.075070 2020] [:error] [pid 513] [client 172.17.0.1:33640]   File "/var/www/hitme/app/__init__.py", line 4, in <module>
[Fri Oct 23 09:12:38.075138 2020] [:error] [pid 513] [client 172.17.0.1:33640]     import cx_Oracle
[Fri Oct 23 09:12:38.075162 2020] [:error] [pid 513] [client 172.17.0.1:33640] ImportError: No module named cx_Oracle

我按照这篇文章使用 apache 部署烧瓶。

https://dev.to/sm0ke/flask-deploy-with-apache-on-centos-minimal-setup-2kb7

并安装 cx_Oracle 并将其复制到我认为需要它的地方。

这里是我的树

 /etc/httpd/modules/
              |-cx_Oracle.cpython-36m-x86_64-linux-gnu.so
              |-cx_Oracle-8.0.1.dist-info

/var/www/hitme/lib/python3.6/site-packages
                                |-cx_Oracle.cpython-36m-x86_64-linux-gnu.so
                                |-cx_Oracle-8.0.1.dist-info

/usr/local/lib/python3.6/site-packages
                                |-cx_Oracle.cpython-36m-x86_64-linux-gnu.so
                                |-cx_Oracle-8.0.1.dist-info

但它仍然给出错误。

还有其他方法吗?

标签: pythonoracleapacheflask

解决方案


确保通过下载将所需的 Oracle Instant Client 安装到 Apache 用户

https://download.oracle.com/otn_software/linux/instantclient/199000/instantclient-basic-linux.x64-19.9.0.0.0dbru.zip

并解压缩到 Apache 用户拥有或可读取和执行的文件夹 - 可以说

/apache/oracle

之后,确保 PATH、ORACLE_HOME 和 LD_LIBRARY_PATH 正确设置为 Apache 用户 - 即

export ORACLE_HOME=/apache/oracle
export PATH=$PATH:$ORACLE_HOME
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME

如果您将 Apache 作为服务启动,那么您可能需要将变量设置为 root 或启动 apache 的用户。


推荐阅读