在線安裝
$ wget https://bootstrap.pypa.io/get-pip.py或者將網頁中的代碼復制到get-pip.py中再執行
$ python get-pip.py
$ pip -V #查看pip版本
舊版本的下載地址: http://sourceforge.net/projects/cx-oracle/files/
# 先安裝 rpm
$ yum install rpm
# oracle-instantclient-basic-10.2.0.3-1.x86_64.rpm 請到 Oracle 官網下載
rpm -ivh oracle-instantclient-basic-10.2.0.3-1.x86_64.rpm
rpm -ivh cx_Oracle-5.1.2-10g-py27-1.x86_64.rpm
# 有這個文件表示安裝成功,根據 python 的位置,也可能在其他地方
ls /usr/lib/python2.7/site-packages/cx_Oracle.so
問題 1
import cx_Oracle
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: libclntsh.so.10.1: cannot open shared object file: No such file or directory
vim /etc/ld.so.conf.d/oracle.conf
添加/usr/lib/oracle/10.2.0.3/client64/lib/
This tells ldconfig to also look for libraries in the lib folder of the Instant Client installation. To update the library cache just call ldconfig without any parameter. This will take a while since ldconfig will re-read every configured library folder and add its content to the library cache. The new oracle.conf file has to be owned by the root user as well as ldconfig has to be called as the root user. Afterwards so should be able to use the cx_Oracle module in your Python shell:
這樣做之后,有時仍然出現問題。參照這里的方法,需要de >設置環境變量de>:
方法1:
export PATH
在/etc/profile文件中添加變量,該變量將會對Linux下所有用戶有效,並且是“永久的”。
vi /etc/profile
在文件末尾添加
LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/lib/oracle/10.2.0.3/client64/lib
export LD_LIBRARY_PATH
要是剛才的修改馬上生效,需要執行以下代碼
source /etc/profile
這時再查看系統環境變量,就能看見剛才加的東西已經生效了
echo $LD_LIBRARY_PATH
方法2: 直接運行export命令定義變量,只對當前shell(BASH)有效(臨時的)
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/lib/oracle/10.2.0.3/client64/lib
問題 2
import cx_Oracle
ImportError: No module named cx_Oracle
cd /usr/lib/python2.7/site-packages/
cp cx_Oracle.so /usr/lib64/python2.7/site-packages/cx_Oracle.so
cp cx_Oracle-5.1.2-py2.7.egg-info /usr/lib64/python2.7/site-packages/cx_Oracle-5.1.2-py2.7.egg-info
如果是 Ubuntu 系統則需要注意
For Debian and derivatives, this sys.path is augmented with directories for packages distributed within the distribution. Local addons go into /usr/local/lib/python/dist-packages, Debian addons install into /usr/{lib,share}/python/dist-packages. /usr/lib/python/site-packages is not used.
cd /usr/lib/python2.7
sudo mv site-packages/cx_Oracle* dist-packages/
sudo rmdir site-packages/
sudo ln -s dist-packages site-packages
sudo ldconfig
Thanks to http://bayo.opadeyi.net/2011/07/setting-up-cxoracle-on-ubuntu-1104.html
SQLAlchemy Oracle 的中文問題
你需要設置 de >NLS_LANGde> 環境變量,否則你讀取出來的中文可能是亂碼,或者當 insert 的數據有中文時會導致 Unicode 編碼錯誤。
你可以在 Python 代碼中這么設置環境變量
# 設置編碼,否則:
# 1. Oracle 查詢出來的中文是亂碼
# 2. 插入數據時有中文,會導致
# UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-7: ordinal not in range(128)
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'