简体   繁体   English

python导入错误cx_Oracle在macOS Sierra中

[英]python import error cx_Oracle in macOS Sierra

I have macOS Sierra 10.12.3, and I need to connect to an Oracle database through python. 我有macOS Sierra 10.12.3,我需要通过python连接到Oracle数据库。 I installed cx_Oracle following these instructions , using pip. 我按照这些说明使用pip安装了cx_Oracle。 I get the following error message when importing cx_Oracle in ipython (Python 3.5.2 |Anaconda 4.2.0 (x86_64)): 在ipython中导入cx_Oracle时,我收到以下错误消息(Python 3.5.2 | Anaconda 4.2.0(x86_64)):

In [1]: import cx_Oracle
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-d1ecebcba3d2> in <module>()
----> 1 import cx_Oracle

ImportError: dlopen(/Users/dufoudav/anaconda/lib/python3.5/site-packages/cx_Oracle.cpython-35m-darwin.so, 2): Library not loaded: @rpath/libclntsh.dylib.12.1
    Referenced from: /Users/dufoudav/anaconda/lib/python3.5/site-packages/cx_Oracle.cpython-35m-darwin.so
        Reason: image not found

Any ideas of how to solve this? 有关如何解决这个问题的任何想法?

Installing cx_Oracle for Python/ Mac OS Sierra 为Python / Mac OS Sierra安装cx_Oracle

Download the following files from Oracle Oracle下载以下文件

instantclient-basic-macos.x64-12.1.0.2.0.zip instantclient-sdk-macos.x64-12.1.0.2.0.zip instantclient-basic-macos.x64-12.1.0.2.0.zip instantclient-sdk-macos.x64-12.1.0.2.0.zip

Create a directory /usr/local/lib/share/oracle 创建目录/usr/local/lib/share/oracle

export ORACLE_HOME=/usr/local/lib/share/oracle
export VERSION=12.1.0.2.0
export ARCH=x86_64

mkdir -p $ORACLE_HOME

Copy Instant Client Basic & SDK to /usr/local/lib/share/oracle 将Instant Client Basic和SDK复制到/usr/local/lib/share/oracle

cp instantclient-basic-macos.x64-$VERSION.zip $ORACLE_HOME
cp instantclient-sdk-macos.x64-$VERSION.zip $ORACLE_HOME

Unpack both files to that directory: 将两个文件解压缩到该目录:

cd $ORACLE_HOME
tar -xzf instantclient-basic-macos.x64-$VERSION.zip
tar -xzf instantclient-sdk-macos.x64-$VERSION.zip

Create Symbolic links in the instant client folder in /user/local/lib/share/oracle /user/local/lib/share/oracle中的即时客户端文件夹中创建符号链接

cd instantclient_12_1
ln -s libclntsh.dylib.12.1 libclntsh.dylib
ln -s libocci.dylib.12.1 libocci.dylib

export DYLD_LIBRARY_PATH=$ORACLE_HOME
export LD_LIBRARY_PATH=$ORACLE_HOME

now you can get rid of the zip files you downloaded as they aren't needed anymore 现在你可以摆脱你下载的zip文件,因为它们不再需要了

cd /usr/local/lib/share/oracle
rm instantclient-basic-macos.x64-$VERSION.zip
rm instantclient-sdk-macos.x64-$VERSION.zip

move all the files & folders outside of /instantclient_12_1 to the parent directory /usr/local/lib/share/oracle 将/ instantclient_12_1之外的所有文件和文件夹移动到父目录/usr/local/lib/share/oracle

mv  -v $ORACLE_HOME/instantclient_12_1/* /$ORACLE_HOME/
rm -rf instantclient_12_1/

You should be able to install cx_Oracle now. 你现在应该可以安装cx_Oracle了。

Troubleshooting 故障排除

If you're experiencing cx_Oracle.DatabaseError: ORA-21561: OID generation failed Get the hostname of your computer by typing in terminal 如果您遇到cx_Oracle.DatabaseError: ORA-21561: OID generation failed通过键入终端获取计算机的主机名

hostname

copy this value and add it to your /etc/hosts file with your favorite text editor(i'm using vi in this case) 复制此值并使用您喜欢的文本编辑器将其添加到/etc/hosts文件中(在这种情况下我使用的是vi)

vi /etc/hosts

on the line that has 127.0.0.1 localhost at the end of it put a space and enter your hostname given to you earlier. 在最后有127.0.0.1 localhost的行上放一个空格并输入之前给你的主机名。 below, you see MYMACSNAME, replace that portion with your hostname you received 在下面,您看到MYMACSNAME,用您收到的主机名替换该部分

127.0.0.1       localhost MYMACSNAME

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM