pyinstaller打包cx_Oracle庫問題處理記錄

studywell發表於2018-11-02

pyinstaller打包cx_Oracle庫問題處理記錄

這兩天打包程式,遇到坑無數,記錄一下。


綜合網上各類參考資訊摘錄,地址如下:https://www.cnblogs.com/BigFishFly/p/6337014.html

1.下載cx_Oracle

在windows下不要使用easy_install或者pip,因為這樣安裝不會同步環境,並報錯:

distutils.errors.DistutilsSetupError: cannot locate Oracle include files in...

因此下載cx_Oracle5.3.exe檔案安裝


2. 下載Oracle Instant Client或使用完整的資料庫客戶端安裝包,版本需11.2以上。

資料庫客戶端版本和python版本及位數需一致。

下載地址:

64位:

32位:

下載後解壓到某一路徑,並將該路徑新增到系統的環境變數,命名為ORACLE_HOME,將ORACLE_HOME新增到path下,否則會報錯

cx_Oracle.InterfaceError: Unable to acquire Oracle environment handle

該目錄下可自建network\admin資料夾,存放tnsnames.ora, sqlnet.ora or oraaccess.xml檔案


3.安裝VC環境

Oracle Instant Client libraries需要VC環境,並匹配對應的位數和版本,對應關係如下:

For Instant Client 18 or 12.2 install VS 2013

For Instant Client 12.1 install VS 2010

For Instant Client 11.2 install VS 2005 64-bit or VS 2005 32-bit


4.解決中文亂碼問題

4.1設定環境變數(全域性,可能對其它Oracle客戶端產生影響)

“計算機”->“系統屬性”->“高階系統設定”->“系統屬性/高階 選項卡”->“環境變數”->“系統變數”->“新建”->

變數名:NLS_LANG

變數值:SIMPLIFIED CHINESE_CHINA.ZHS16GBK


4.2 python環境下設定

import os  

os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'  

#或者os.environ['NLS_LANG'] = 'AMERICAN_AMERICA.AL32UTF8'

 

5.連線資料庫

import cx_Oracle  

db=cx_Oracle.connect('system/oracle@10.98.156.148/oral')

cursor = db.cursor()

cursor.execute ("select * from dba_users where user_id=62")

row = cursor.fetchall ()

print (row[0])

db.commit()  

cursor.close ()

db.close(

如上配置後,不需要再本機上安裝Oracle,也不需要新建NETWORK/ADMIN目錄新增tnsnames.ora檔案,更不要把oci.dll移到site-packages



6.打包後執行報錯:cx_oracle_Error object at 0x000

環境:win2008r2_x64+py2.7.13_x64上

錯誤提示:cx_Oracle.DatabaseError: DPI-1050: Oracle Client library must be at version 11.2

解決:確認當前版本時11以上,如是,將安裝路徑下Lib和安裝路徑都新增到path變數前面。



7.win2008r2 x64 直接執行指令碼無問題,執行打包程式報錯.無法定位程式輸入點ucrtbase.terminate 於動態連結庫api-ma-win-crt-runtime-11-1-0.dll上

解決步驟:

A、開啟CMD,輸入 winver.exe ,回車執行,檢視當前Windows版本

如果版本為7600,則首先安裝:windows6.1-KB976932-X64.exe(連結),將版本升級為?7601:Service Pack 1,再進行第2步;

如果版本如上圖所示,為7601:Service Pack 1,直接進行第2步;

B、安裝:Microsoft .NET Framework 4.6.1 NDP461-KB3102438-Web.exe(連結);

C、安裝:Visual C++ Redistributable for Visual Studio 2015 vc_redist.x64.exe(連結);

D、一般情況下,到第3步問題已經解決,但是如果再一次安裝軟體,還是會出錯“無法定位程式輸入點ucrtbase.terminate於動態連結庫api-ms-win-crt-runtime-|1-1-0.dll”,那麼可以安裝補丁:Windows6.1-KB2999226-x64 .msu(連結);



8.錯誤提示:cx_Oracle.InterfaceError: Unable to acquire Oracle environment handle

如命令列執行報此錯誤:將instantclient目錄下的所有*.dll檔案複製到Python27\Lib\site-packages目錄下,可解決問題。

如程式命令列執行無問題,命令列sqlplus直連資料庫無問題,但打包後執行報錯。則如下解決

網上類似情況:

問題的原因是pyinstall打包時沒將oracle連結庫打包進去,導致換個環境就報錯。

先正常打包一遍,生成spec檔案,如

python -F checkreport.py -i dl.ico

再編輯程式目錄下checkreport.spec檔案,修改a.binaries行,修改後如下。須將oraociei11.dll複製到本目錄下

a.binaries+[('oraociei11.dll','oraociei11.dll','BINARY')],

然後重新編譯一遍;

pyinstaller checkreport.spec

測試後在別機器上執行正常。

官網中關於ODBC的資料


On Windows

Patching the Instant Client ODBC driver on Windows can be done only by manually copying the ODBC driver shared library files and supporting library files from a patched ORACLE_HOME or from an unpacked Oracle Database Bundle patch. These should be copied into the Instant Client directory. Generating an Instant Client ODBC package is not available on Windows. 

The files that must be copied to the Instant Client directory:

ODBC driver shared library files: sqora32.dll, sqoras32.dll, sqresus.dll, sqresja.dll

Required additional files when using Oracle Basic Instant Client:

For 12c: oraociei12.dll, orannzsbb12.dll, oci.dll, oraons.dll, ociw32.dll, oraociei12.sym, orannzsbb12.sym, oci.sym, ociw32.sym

For 11g: oraociei11.dll, orannzsbb11.dll, oci.dll, ociw32.dll, oraociei11.sym, orannzsbb11.sym, oci.sym, ociw32.sym

Required additional files when using Oracle Basic Light Instant Client:

For 12c: oraociicus12.dll, orannzsbb12.dll, oci.dll, oraons.dll, ociw32.dll, oraociicus12.sym, orannzsbb12.sym, oci.sym, ociw32.sym

For 11g: oraociicus11.dll, orannzsbb11.dll, oci.dll, ociw32.dll, oraociicus11.sym, orannzsbb11.sym, oci.sym, ociw32.sym

Note: While copying from the Oracle Database Bundle patch, some of the files above may be missing. This implies that those files are unchanged and do not need to be patched.




9.打包程式執行報錯:UnicodeDecodeError: 'utf-8' codec can't decode byte

本機環境:win10_x64,anaconda_3 python 3.6.5_x64,cx_oracle 7

程式在命令列執行無問題,用pyinstaller打包後,本機執行無問題,在別機器執行報錯,報錯提示如下

C:\Users\super>D:\checkreportv1\checkreportv1\checkreportp36w10.exe

Traceback (most recent call last):

  File "checkreport.py", line 9, in <module>

  File "<frozen importlib._bootstrap>", line 971, in _find_and_load

  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked

  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked

  File "d:\programdata\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module

  File "oradb.py", line 4, in <module>

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb2 in position 53: invalid start byte [6380] Failed to execute script checkreport


網搜,同類情況

使用Pyinstaller打包含有cx_Oracle模組遇到的問題


該問題暫沒找到解決辦法。找了一個win2008r2_x64+py2.7.13_x64+cx_Oracle_5.13環境,打包後轉發系統執行正常。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29519108/viewspace-2218516/,如需轉載,請註明出處,否則將追究法律責任。

相關文章