【问题标题】:cx_Oracle not recognized by pythonpython 无法识别 cx_Oracle
【发布时间】:2018-04-28 00:08:55
【问题描述】:

更新:确切的解决方案在标记答案下方的 cmets 中。


在企业级 redhat linux 环境中,Python 似乎找不到连接数据库所需的 cx_Oracle 包。查看日志:

[user@redhat ~]$ echo $PYTHONPATH
~/.local/lib/python3.4/site-packages
[user@redhat ~]$ cd $PYTHONPATH
[user@redhat site-packages]$ ls -l
total 1912
drwxrwxr-x  2 user user     131 Apr 20 12:00 cx_Oracle-6.2.1.dist-info
-rwxrwxr-x  1 user user 1900997 Apr 20 12:00 cx_Oracle.cpython-34m.so
-rw-rw-r--  1 user user     290 Apr 20 12:00 easy-install.pth
drwxrwxr-x  4 user user     114 Apr 20 12:00 et_xmlfile
drwxrwxr-x  2 user user     117 Apr 20 12:00 et_xmlfile-1.0.1-py3.4.egg-info
drwxrwxr-x  2 user user     150 Apr 20 12:00 jdcal-1.4.dist-info
-rw-rw-r--  1 user user   12553 Apr 20 12:00 jdcal.py
-rw-rw-r--  1 user user   13386 Apr 20 12:00 jdcal.pyc
drwxrwxr-x 21 user user    4096 Apr 20 12:00 openpyxl
drwxrwxr-x  2 user user     154 Apr 20 12:00 openpyxl-2.5.3-py3.4.egg-info
drwxrwxr-x  2 user user      61 Apr 23 12:00 __pycache__
-rw-rw-r--  1 user user    2389 Apr 23 12:00 site.py
-rw-rw-r--  1 user user    1743 Apr 23 12:00 site.pyc
[user@redhat site-packages]$ python
Python 2.7.5 (default, May  3 2017, 07:55:04)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-14)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named cx_Oracle
>>> import openpyxl
openpyxl/xml/__init__.py:15: UserWarning: The installed version of lxml is too old to be used with openpyxl
  warnings.warn("The installed version of lxml is too old to be used with openpyxl")
>>> exit()
[user@redhat site-packages]$

请注意,cx_Oracle 和 openpyxl 都是在一个命令中使用 pip 安装的:pip install --user cx_Oracle openpyxl 它们都在同一个位置并以相同的方式拉入 python,但是看到了 openpyxl 而没有看到 cx_Oracle。我在这里做错了什么?

【问题讨论】:

    标签: python python-2.7 cx-oracle


    【解决方案1】:

    您似乎正在运行 Python 2.7,但您安装的 cx_Oracle 是为 Python 3.4 运行的! cx_Oracle 是一个纯 C 模块,因此只能在为其编译的 Python 中工作。另一个模块(openpyxl)具有纯 Python 模块,因此它至少会部分加载。您需要运行 Python 3.4 或为 Python 2.7 重新安装这两个模块。

    【讨论】:

    • 我很确定 pip 只是将它放在 python3.4 文件夹中。官方 cx_Oracle 文档说它与 2.7 兼容:oracle.github.io/python-cx_Oracle
    • 是的,cx_Oracle 与 Python 2.7 兼容。但是你必须确保你有一个为 Python 2.7 编译的模块,而不是为 Python 3.4 编译的!
    • 谢谢!自从 pip 被重命名为 pip3.4 以来,我在弄清楚如何做这件事上遇到了问题,我没有意识到有一个单独的模块 pip 可以在 python 中使用它。解决方案是:pip3.4 install --user pip 然后python2.7 -m pip install --user cx_Oracle
    猜你喜欢
    • 2019-07-11
    • 2017-05-16
    • 2016-08-28
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 2020-11-10
    • 2015-10-31
    相关资源
    最近更新 更多