如何在 El Capitan 上安装 cx_Oracle

How to install cx_Oracle on El Capitan

据我所知,有一些与 SIP 相关的变化导致安装变得困难。

这些页面有安装的背景和建议。 http://sourceforge.net/p/cx-oracle/mailman/message/34534872/, http://stefanoapostolico.com/2015/10/08/install_cx_oracle_with_sip_enabled.html

将所有这些放在一起,这是我将它安装到我的 virtualenv 中的最佳机会,但唉,还是不行。

我得到的错误是:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: dlopen(/Users/me/sx_direct_env/lib/python2.7/site-packages/cx_Oracle.so, 2): Library not loaded: /b/227/rdbms/lib/libclntsh.dylib.10.1
  Referenced from: /Users/me/sx_direct_env/lib/python2.7/site-packages/cx_Oracle.so
  Reason: image not found

以下是我的所有安装步骤:

$ cd /Users/me/sx_direct_env/lib/python2.7
$ mkdir oracle
$ cd oracle
$ export ORACLE_HOME=$PWD
$ export DYLD_LIBRARY_PATH=$ORACLE_HOME
$ export LD_LIBRARY_PATH=$ORACLE_HOME
$ export PATH=$PATH:$ORACLE_HOME
$ unzip ~/Downloads/instantclient-basic-macos.x64-11.2.0.4.0.zip
$ unzip ~/Downloads/instantclient-sdk-macos.x64-11.2.0.4.0.zip
$ mv instantclient_11_2/* .
$ rmdir instantclient_11_2
$ curl -O https://raw.githubusercontent.com/kubo/fix_oralib_osx/master/fix_oralib.rb
$ ruby -a fix_oralib.rb 
adrci:
   add rpath: @loader_path
   change install name
     from: /ade/b/3071542110/oracle/rdbms/lib/libclntsh.dylib.11.1
       to: @rpath/libclntsh.dylib.11.1
   change install name
     from: /ade/dosulliv_ldapmac/oracle/ldap/lib/libnnz11.dylib
       to: @rpath/libnnz11.dylib

genezi:
   add rpath: @loader_path
   change install name
     from: /ade/b/3071542110/oracle/rdbms/lib/libclntsh.dylib.11.1
       to: @rpath/libclntsh.dylib.11.1

libclntsh.dylib.11.1:
   add rpath: @loader_path
   change identification name
     from: /ade/b/3071542110/oracle/rdbms/lib/libclntsh.dylib.11.1
       to: @rpath/libclntsh.dylib.11.1
   change install name
     from: /ade/dosulliv_ldapmac/oracle/ldap/lib/libnnz11.dylib
       to: @rpath/libnnz11.dylib

libnnz11.dylib:
   change identification name
     from: /ade/dosulliv_ldapmac/oracle/ldap/lib/libnnz11.dylib
       to: @rpath/libnnz11.dylib

libocci.dylib.11.1:
   change identification name
     from: /ade/b/3071542110/oracle/rdbms/lib/libocci.dylib.11.1
       to: @rpath/libocci.dylib.11.1

libociei.dylib:
   add rpath: @loader_path
   change install name
     from: /ade/b/3071542110/oracle/rdbms/lib/libclntsh.dylib.11.1
       to: @rpath/libclntsh.dylib.11.1

libocijdbc11.dylib:
   add rpath: @loader_path
   change install name
     from: /ade/b/2475221476/oracle/rdbms/lib/libclntsh.dylib.11.1
       to: @rpath/libclntsh.dylib.11.1
   change install name
     from: /ade/b/2475221476/oracle/ldap/lib/libnnz11.dylib
       to: @rpath/libnnz11.dylib

uidrvci:
   add rpath: @loader_path
   change install name
     from: /ade/b/3071542110/oracle/rdbms/lib/libclntsh.dylib.11.1
       to: @rpath/libclntsh.dylib.11.1
   change install name
     from: /ade/dosulliv_ldapmac/oracle/ldap/lib/libnnz11.dylib
       to: @rpath/libnnz11.dylib

$ pip install cx_oracle
Collecting cx-oracle
Installing collected packages: cx-oracle
Successfully installed cx-oracle-5.2

$ python -c "import cx_Oracle"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: dlopen(/Users/me/sx_direct_env/lib/python2.7/site-packages/cx_Oracle.so, 2): Library not loaded: /b/227/rdbms/lib/libclntsh.dylib.10.1
  Referenced from: /Users/me/sx_direct_env/lib/python2.7/site-packages/cx_Oracle.so
  Reason: image not found

我想我已经修好了。基本上我上面发布的步骤中的所有内容都是正确的。

但我最终将其用于 pip 安装,以确保它下载一个新副本并重建(并且可能获得更高版本??)

$ pip install --no-cache-dir --allow-external --allow-unverified cx_oracle

然后我可以毫无问题地导入 cx_oracle,但在连接到外部服务器时出现错误“'ORA-21561: OID generation failed'”。

然后我按照 instructions here 并在 /etc/hosts 文件中添加了一行我的主机名,现在一切正常了。

例如,在 /etc/hosts

末尾添加这样一行

127.0.0.1 localhost 我的主机名

感谢格雷格的指导。

我必须为我的 cx_Oracle pip 安装创建一个符号 link 才能工作(使用您在上面提供的参数)。您可能希望将这些添加到您的说明中。

ln -s libclntsh.dylib.11.1 libclntsh.dylib

我还按照此安装指南的建议为 libocci 创建了另一个 link:https://gist.github.com/thom-nic/6011715

ln -s libocci.dylib.11.1 libocci.dylib

我尝试了上述操作,但在尝试使用 Oracle instantclient 12.1 安装 cx_Oracle 时出现以下错误:

[535]: /opt/instantclient_12_1 $ python -c "import cx_Oracle"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: dlopen(/Library/Python/2.7/site-packages/cx_Oracle.so, 2): 
        Library not loaded: @rpath/libclntsh.dylib.12.1
  Referenced from: /Library/Python/2.7/site-packages/cx_Oracle.so
  Reason: image not found

注意错误中的 @rpath - 结果是在构建 cx_Oracle 库时 (对于 instantclient 12.1) 在 El Capitan 上,gcc 编译器期望设置 -rpath 标志以知道在哪里可以找到上述动态链接库 (*.dylib)。默认情况下,在 instantclient 12.1 上,pip 不会为您执行此操作。

# Set -rpath option before installing...this will use $ORACLE_HOME during compilation
export FORCE_RPATH=TRUE
pip install cx_Oracle
# And verify cx_Oracle was correctly installed
python -c "import cx_Oracle"
# If this line fails install cx_Oracle with:
#   pip install --no-cache-dir --allow-external --allow-unverified cx_oracle

python -c "import cx_Oracle" 应该不会报告错误。

有关完整的安装指南(包括 instantclient 下载和配置),请查看我在 http://thelaziestprogrammer.com/sharrington/databases/oracle/install-cx_oracle-mac 的 post 了解详细信息。

要在 OS X 上安装 cx_Oracle,请从 http://www.oracle.com/technetwork/topics/intel-macsoft-096467.html 下载 OS X 的 64 位 Instant Client 基本和 sdk 包。使用此版本的 Instant Client,您可以连接到 10g、11g 和 12c 数据库。此版本的链接方式避免了 Apple 最近引入的 SIP 问题(影响了 Instant Client 11.2)。

然后 运行 类似于:

unzip instantclient-basic-macos.x64-12.1.0.2.0.zip
unzip instantclient-sdk-macos.x64-12.1.0.2.0.zip
cd instantclient_12_1
ln -s libclntsh.dylib.12.1 libclntsh.dylib
cd ..
export ORACLE_HOME=`pwd`/instantclient_12_1
export FORCE_RPATH=1
pip install cx_Oracle

您可能需要也可能不需要其他 pip 选项,如

中所述

另请注意: 如果您的 python 运行 为 32 位,而安装 cx_Oracle 为 64 位,您也会遇到此问题。 为避免此问题,请始终以超级用户 'su' 的身份 运行 执行命令并弄清楚 'su' python 是 32 位还是 64 位。 检查你的 python 位 How do I determine if my python shell is executing in 32bit or 64bit mode on OS X?

强制使用 64 位 运行:

arch -x86_64 /usr/bin/python27

强制使用 32 位 运行 :

默认写入com.apple.versioner.python Prefer-32-Bit -bool yes