关于python安装cx_oracle出现异常情况处理

时间:2022-06-28 00:02:21
相信大家安装的时候可能会出现报错: cx_ Oracle  ImportError: DLL load failed: %1 不是有效的 Win32 应用 程序
开始我到处百度这个错误,发现大家基本都是说系统位数、python位数及cx_Oracle位数要统一,但这正是困扰了我很久,因为我的环境都是64位,WIN764,python64,cx_Oracle下载的依然是64,让我百思不得其解,于是不停的百度,但依然没有是说位数问题,一次偶然间发现有人说需要下载64位oracle clicen,将oci.dll拷贝D:\Python27\Lib\site-packages目录即可,具体可参考http://www.07net01.com/zhishi/390562.html

但是本人试了以后发现依然报错,只不过错误不一样了,报错cx_oracle ImportError: DLL load failed: 找不到指定的程序,于是继续百度,后面发现还是说将oci拷贝到D:\Python27目录即可,但是我却一直报错,不停的卸载cx_oracle,再重装不同版本,试了很久,依然不行,后面突然一想,会不会可能是我cx_oracle版本问题,因为我的客户端使用的是Oracle 11g InstantClient版本,但数据库确是oracle12c的,之前cx_oracle下载的版本一直是12c,于是再卸载重装11g的cx_oracle,再import cx_oracle,此时终于不报错了!

关于python安装cx_oracle出现异常情况处理
卸载重装cx_oracle会报错,只需执行以下代码即可,如下:
#
# script to register Python 2.0 or later for use with win32all
# and other extensions that require Python registry settings
#
# written by Joakim Loew for Secret Labs AB / PythonWare
#
# source:
# http://www.pythonware.com/products/works/articles/regpy20.htm
#
# modified by Valentine Gogichashvili as described in http://www.mail-archive.com/distutils-sig@python.org/msg10512.html

import sys

from _winreg import *

# tweak as necessary
version = sys.version[:3]
installpath = sys.prefix

regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)
installkey = "InstallPath"
pythonkey = "PythonPath"
pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (
installpath, installpath, installpath
)

def RegisterPy():
try:
reg = OpenKey(HKEY_CURRENT_USER, regpath)
except EnvironmentError as e:
try:
reg = CreateKey(HKEY_CURRENT_USER, regpath)
SetValue(reg, installkey, REG_SZ, installpath)
SetValue(reg, pythonkey, REG_SZ, pythonpath)
CloseKey(reg)
except:
print "*** Unable to register!"
return
print "--- Python", version, "is now registered!"
return
if (QueryValue(reg, installkey) == installpath and
QueryValue(reg, pythonkey) == pythonpath):
CloseKey(reg)
print "=== Python", version, "is already registered!"
return
CloseKey(reg)
print "*** Unable to register!"
print "*** You probably have another Python installation!"

if __name__ == "__main__":
RegisterPy()