0

在 python 脚本中导入 cx_Oracle 失败。

我安装了 cx_Oracle,使用“pip install cx_oracle” - 工作正常,报告安装。

现在当我尝试:

import cx_Oracle

我收到以下错误

Traceback (most recent call last):
  File "reader.py", line 9, in <module>
    import cx_Oracle
ImportError: dlopen(/Library/Python/2.7/site-packages/cx_Oracle.so, 2): Symbol not found: _OCIAttrGet
  Referenced from: /Library/Python/2.7/site-packages/cx_Oracle.so
  Expected in: flat namespace
 in /Library/Python/2.7/site-packages/cx_Oracle.so

其他信息:

Python 版本 2.7 / mac os 10.7.2 (Lion)

$ python
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin    
Type "help", "copyright", "credits" or "license" for more information.

甲骨文 10.2

$ sqlplus -version    
SQL*Plus: Release 10.2.0.4.0 - Production

另外,我的 ORACLE_HOME 文件夹中根本没有 /bin 目录,我只安装了即时客户端和 SDK。

ox_Oracle

$ pip freeze
PyRSS2Gen==1.0.0
...
cx-Oracle==5.1.1

(发现很多关于安装 cx_Oracle 的问题,但没有关于这个 - 谢谢)

4

2 回答 2

2

我今天遇到了这个问题,并且能够通过将 InstantClient 二进制文件中引用的库的路径更改为文件系统上的实际位置来解决它。该博客http://blog.caseylucas.com/2013/03/03/oracle-sqlplus-and-instant-client-on-mac-osx-without-dyld_library_path/ 提供了详细的解释和调整所有二进制文件的脚本。唯一的问题是它使用了 @executable_path ,它似乎不再适用于 Python 2.7 和 El Capitan (我不确定是什么导致了安全异常)。用实际路径替换 @executable_path 就可以了。

总而言之,使其工作的步骤:

  • 将 InstantClient 安装到 /usr/local/instantclient_11_2
  • 确保您使用的 cx_Oracle.so 共享对象位于 /Library/Python/2.7/site-packages/cx_Oracle.so
  • 将以下脚本复制到 /usr/local/instantclient_11_2

    #!/bin/sh
    # script to change the dynamic lib paths and ids for oracle instant client
    # exes and libs
    (echo /Library/Python/2.7/site-packages/cx_Oracle.so ; find . -maxdepth 1 -type f \( -perm -1 -o \( -perm -10 -o -perm -100 \) \) -print ) | while read exe
    do
        echo adjusting executable $exe
        baseexe=`basename $exe`
        otool -L $exe | awk '/oracle/ {print $1}' | while read lib
        do
            echo adjusting lib $lib
            baselib=`basename $lib`
            if [ "$baseexe" = "$baselib" ]
            then
                echo changing id to $baselib for $exe
                install_name_tool -id $baselib $exe
            else
                echo changing path id for $lib in $exe
                install_name_tool -change $lib /usr/local/instantclient_11_2/$baselib $exe
            fi
        done
    done
    
    • 以 root 权限运行脚本。
于 2015-12-10T01:49:40.363 回答
-1

卸载一切。

然后安装 oracle 即时客户端:

然后使用 pip 安装 cx_oracle。

然后将路径设置为指向 32 位版本的 oracle。

  • 编辑主目录中的 .profile 文件,并使用以下行添加到您的 oracle bin 主目录的路径:
  • 导出 PATH=$PATH:/usr/local/lib/instantclient/

它有效...

于 2011-12-29T18:37:51.197 回答