macos下 python安裝cx_oracle

1) 首先經過命令安裝cx_Oraclehtml

  • pip install cx_Oracle
  • 也可在pycharm裏面安裝,File->Default Setting ->Project Interpreter->添加cx_Oracle->Install package;
  • 若是安裝失敗,點擊Manage Repositories :更換倉庫地址爲:http://pypi.douban.com/simple/
  • 去官網下載源碼包:cx_Oracle-5.2.1.tar.gz;
  • https://pypi.python.org/pypi/cx_Oracle/5.2.1#downloads

2)去oracle官網下載mac版的64bit的client basic 和client sdkpython

http://www.oracle.com/technetwork/topics/intel-macsoft-096467.html數據庫

 

3)編譯安裝:macos

  • sudo su #切換到root用戶
  • mkdir /Users/guanguan/oracle #建立oracle文件
  • mv /Users/guanguan/Downloads/instantclient-* /Users/guanguan/oracle #將下載的兩個Oracle包放到/Users/guanguan/oracle目錄下
  • cd /Users/guanguan/oracle 進入oracle文件中
  • unzip instantclient-basic-macos.64-12.1.0.4.0.zip #解壓
  • unzip instantclient-sdk-macos.64-12.1.0.4.0.zip #解壓
  • cd instantclient_12_1/sdk
  • unzip ottclasses.zip
  • cd ..
  • cp -R ./sdk/* .
  • cp -R ./sdk/include .
  • ln -s  libocci.dylib.12.1 libocci.dylib
  • ln -s libclntsh.dylib.12.1 libclntsh.dylib

4)更改環境變量:bootstrap

  • vi ~/.bash_profile
  • export ORACLE_HOME=/Users/guanguan/oracle/instantclient_12_1
  • export DYLD_LIBRARY_PATH=$ORACLE_HOME
  • export LD_LIBRARY_PATH=$ORACLE_HOME
  • 而後輸入source ~/.bash_profile 或者 .  ~/.bash_profile使環境變量生效

5)測試環境變量是否生效bash

  • echo $ORACLE_HOME
  •  /Users/guanguan/oracle/instantclient_12_1

6)而後解壓安裝cx_Oracle:oracle

  • tar -zxvf cx_Oracle-5.2.1.tar.gz
  • cd cx_Oracle-5.2.1
  • python setup.py build
  • python setup.py install

7)測試cx_Oracle安裝是否成功學習

  • python
  • import cx_Oracle

#運行結果結果:測試

➜  ~ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>>fetch

或者 python -c "import cx_Oracle"

➜  ~ python -c "import cx_Oracle"
➜  ~

此時說明已經安裝成功啦~

報錯信息:

sh-3.2# python -c "import cx_Oracle"
/Library/Python/2.7/site-packages/cx_Oracle-5.2.1-py2.7-macosx-10.11-intel.egg/cx_Oracle.py:3: UserWarning: Module cx_Oracle was already imported from /Library/Python/2.7/site-packages/cx_Oracle-5.2.1-py2.7-macosx-10.11-intel.egg/cx_Oracle.pyc, but /Users/guanguan/oracle/cx_Oracle-5.2.1 is being added to sys.path
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "build/bdist.macosx-10.11-intel/egg/cx_Oracle.py", line 7, in <module>
  File "build/bdist.macosx-10.11-intel/egg/cx_Oracle.py", line 6, in __bootstrap__
ImportError: dlopen(/var/root/.python-eggs/cx_Oracle-5.2.1-py2.7-macosx-10.11-intel.egg-tmp/cx_Oracle.so, 2): Library not loaded: @rpath/libclntsh.dylib.12.1
  Referenced from: /var/root/.python-eggs/cx_Oracle-5.2.1-py2.7-macosx-10.11-intel.egg-tmp/cx_Oracle.so
  Reason: image not found

 

解決方法:(刪除以前安裝的cx_Oracle,設置export FORCE_RPATH=TRUE,從新安裝cx_Oracle)

sh-3.2# export FORCE_RPATH=TRUE
sh-3.2# pip install cx_Oracle
Requirement already satisfied: cx_Oracle in /Library/Python/2.7/site-packages/cx_Oracle-5.2.1-py2.7-macosx-10.11-intel.egg
sh-3.2# cd /Library/Python/2.7/site-packages/
sh-3.2# rm -f cx_Oracle-5.2.1-py2.7-macosx-10.11-intel.egg
sh-3.2# pip install cx_Oracle
Collecting cx_Oracle
  Using cached cx_Oracle-5.2.1.tar.gz
Installing collected packages: cx-Oracle
  Running setup.py install for cx-Oracle ... done
Successfully installed cx-Oracle-5.2.1

再次試驗:

sh-3.2# python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>> exit()

 

==========================================

python對cx_Oracle的簡單操做:

#! /usr/bin/python
import cx_Oracle
import os
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8' #解決oracle數據庫中的中文查出來是???
等亂碼的問題


dsnStr = cx_Oracle.makedsn("127.0.0.1", "1521", "orcl")
conn = cx_Oracle.connect(user="test", password="test", dsn=dsnStr)

c=conn.cursor()
x=c.execute('select *  from TEST.TEST p WHERE ID<2')
print (x.fetchone())
c.close()
conn.close()
學習連接:http://joelvasallo.com/?p=276
相關文章
相關標籤/搜索