使用MacOSX导入cx_Oracle(python)

时间:2011-11-16 20:56:06

标签: python macos cx-oracle

在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 version 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.

Oracle 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的问题,但没有关于此问题 - 谢谢)

2 个答案:

答案 0 :(得分:2)

I ran into this problem today and was able to solve it by changing path to the libraries referenced in the InstantClient binaries to the actual locations on the filesystem. This blog http://blog.caseylucas.com/2013/03/03/oracle-sqlplus-and-instant-client-on-mac-osx-without-dyld_library_path/ provides detailed explanation and the script to adjust all binaries. The only problem is that it uses @executable_path , which does not seem to work anymore with Python 2.7 & El Capitan (I'm not really sure what is responsible for the security exception). Replacing @executable_path with the actual path works just fine.

To summarize, steps to make it work:

  • install InstantClient to /usr/local/instantclient_11_2
  • make sure that cx_Oracle.so shared object that you use is at /Library/Python/2.7/site-packages/cx_Oracle.so
  • copy the following script to /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
    
    • run the script with root permissions.

答案 1 :(得分:-1)

卸载所有内容。

然后安装oracle即时客户端:

然后使用pip安装cx_oracle。

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

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

它有效......