py2exe 和 cx_Oracle 加载失败 cx_Oracle.pyd

py2exe and cx_Oracle failed loading cx_Oracle.pyd

给定以下python程序

testconnect.py

from sqlalchemy.dialects import oracle
from sqlalchemy import create_engine

url = 'oracle+cx_oracle://user:password@oracle-rds-01.....amazonaws.com:1521/orcl'

e = create_engine(url)

e.connect()
print('Connected')

setup.py

setup(
  options={
    'py2exe': {
      'bundle_files': 1,
      'compressed': True,
      'dll_excludes': ['OCI.dll'],
      'includes':['cx_Oracle']
    }
  },
  console=["testconnect.py"],
  zipfile=None
)

我得到以下回溯

Traceback (most recent call last):
  File "testconnect.py", line 7, in <module>
    e = create_engine(url)
  File "c:\Python34\lib\site-packages\sqlalchemy\engine\__init__.py", line 386, in create_engine
    return strategy.create(*args, **kwargs)
  File "c:\Python34\lib\site-packages\sqlalchemy\engine\strategies.py", line 75, in create
    dbapi = dialect_cls.dbapi(**dbapi_args)
  File "c:\Python34\lib\site-packages\sqlalchemy\dialects\oracle\cx_oracle.py", line 769, in dbapi
    import cx_Oracle
  File "c:\Python34\lib\site-packages\zipextimporter.py", line 109, in load_module
    self.get_data)
ImportError: MemoryLoadLibrary failed loading cx_Oracle.pyd: The specified module could not be found. (126)

我尝试在 setup.py 中使用 'includes',导入 cx_Oracle 但无济于事。

我已经尝试 bundle_files=3 并使用 'data_files=' 将 cx_Oracle.pyd 文件复制到 dist 目录中,但我仍然遇到同样的问题

我需要对我的 setup.py 做哪些更改才能捕获 cx_Oracle.pyd 文件以便加载

更新:

问题是我使用的是在安装 cx_Oracle 和即时客户端之前打开的 cmd 控制台,以使用 py2exe

构建 exe

我关闭控制台并重新打开它,windows 能够找到合适的文件

这现在在我的 Windows 10 笔记本电脑(64 位)

上运行正常

但是当我尝试将此 EXE 部署到我的客户端计算机(64 位 windows 2008)时,我仍然得到以下信息

D:\Milliman>testconnect.exe
Traceback (most recent call last):
  File "testconnect.py", line 1, in <module>
  File "c:\Python34\lib\site-packages\zipextimporter.py", line 109, in load_module
ImportError: MemoryLoadLibrary failed loading cx_Oracle.pyd: The specified module could not be found. (126)

提前感谢您的帮助 安迪

需要在目标机器上安装 Oracle 即时客户端(或等效客户端)。 cx_Oracle 没有它就无法工作。它(可能)试图找到的 DLL 是 OCI.DLL.

终于明白是怎么回事了

我们部署的机器是 windows 2008 Server 64 位 但是它上面有 Oracle 32 位客户端

我试图用 Cx_Oracle 64 位部署 py2exe python 3.4 应用程序 64 位 这是找到 32 位 OCI.dll 并且无法加载

我的解决方案是将 64 位 Instant Client 打包到 data_files 中。 然后在我的app中修改app内的路径

import os
if os.path.exists('./instant_client'):
  pth = os.environ.get('path')
  pth = '{0};{1}'.format('./instant_client' ,pth)
  os.environ['path'] = pth

这样我可以保证 cx_Oracle 会找到正确的 OCI.dll 而不会干扰全局路径和该机器上已经安装的 oracle