python - pyinstaller 包 cx_Oracle 问题(在 CentOS 上)

标签 python linux centos pyinstaller cx-oracle

我写了一个非常简单的Python脚本(db.py):

#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import cx_Oracle

dsn_tns = cx_Oracle.makedsn("192.168.100.15", "1521", "zhy")
print dsn_tns
conn = cx_Oracle.connect("winvoice", "winvoice", dsn_tns)
cursor = conn.cursor()
print cursor

在控制台中运行正常:

[ddgg@office11 1]$ ./db.py
/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py:1256: UserWarning: /home/ddgg/.python-eggs is writable by group/others and vulnerable to attack when used with get_resource_filename. Consider a more secure location (set with .set_extraction_path or the PYTHON_EGG_CACHE environment variable).
  warnings.warn(msg, UserWarning)
(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.100.15)(PORT=1521)))(CONNECT_DATA=(SID=zhy)))
<cx_Oracle.Cursor on <cx_Oracle.Connection to winvoice@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.100.15)(PORT=1521)))(CONNECT_DATA=(SID=zhy)))>>
[ddgg@office11 1]$ 

然后我使用 pyinstaller 对其进行打包,只需使用:pyinstaller db.py, 然后生成db.spec:

# -*- mode: python -*-

block_cipher = None

a = Analysis(['db.py'],
             pathex=['/workcopy/sae/rtgame/1'],
             hiddenimports=[],
             hookspath=None,
             runtime_hooks=None,
             excludes=None,
             cipher=block_cipher)
pyz = PYZ(a.pure,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          exclude_binaries=True,
          name='db',
          debug=False,
          strip=None,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=None,
               upx=True,
               name='db')

一切顺利,没有错误,但是当我运行它时,我得到:

[ddgg@office11 1]$ dist/db/db
Traceback (most recent call last):
  File "<string>", line 3, in <module>
ImportError: No module named cx_Oracle
[ddgg@office11 1]$ 

出了什么问题? 在python控制台中,cx_Oracle可以正确导入:

[ddgg@office11 1]$ python
Python 2.7.10 (default, Sep 10 2015, 14:06:03) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-55)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py:1256: UserWarning: /home/ddgg/.python-eggs is writable by group/others and vulnerable to attack when used with get_resource_filename. Consider a more secure location (set with .set_extraction_path or the PYTHON_EGG_CACHE environment variable).
  warnings.warn(msg, UserWarning)

有什么想法吗?谢谢。

最佳答案

尝试创建一个名为 hook-cx_Oracle.py 的文件,其中包含以下内容:

import os
import glob
import itertools

try:
    # PY_EXTENSION_SUFFIXES is unavailable in older versions
    from PyInstaller.hooks.hookutils import PY_EXTENSION_SUFFIXES
except ImportError:
    try:
        from importlib.machinery import EXTENSION_SUFFIXES as PY_EXTENSION_SUFFIXES
    except ImportError:
        import imp
        PY_EXTENSION_SUFFIXES = set([f[0] for f in imp.get_suffixes()
                                     if f[2] == imp.C_EXTENSION])

def hook(mod):
    module_directory = os.path.dirname(mod.__file__)
    bundled = []

    for libname, ext in itertools.product((mod.__name__, ),
                                          PY_EXTENSION_SUFFIXES):
        bundled.extend(glob.glob(os.path.join(module_directory, libname + ext)))

    for f in bundled:
        name = os.path.join(mod.__name__, os.path.basename(f))
        if hasattr(mod, 'pyinstaller_binaries'):
            mod.pyinstaller_binaries.append((name, f, 'BINARY'))
        else: # mod.pyinstaller_binaries is unavailable in older versions
            mod.binaries.append((name, f, 'BINARY'))

    return mod

构建时,将放置文件的目录的路径作为 --additional-hooks-dir 参数的值提供,如下所示:

--additional-hooks-dir=<path_to_directory_of_hook_file>

关于python - pyinstaller 包 cx_Oracle 问题(在 CentOS 上),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32500416/

相关文章:

linux - 如何从Core Dump中提取EBP和ESP?

linux - 从命令行创建文件

c - 线程 - 访问资源避免饥饿

docker - 是否提供官方的 centos-atomic 或 centos-minimal docker 镜像?

docker - 如何在 Centos 上安装特定版本的 Docker?

python - 全局名称 'pk' 未定义 django

python - 如何查找字符串列表中的字母序列?

php - Centos x86_64 PHP 5.2.14 SQLITE 缺失? (网络 repo )

python - 并行修改切片中的 3D numpy 数组

python - 将 pandas.Series 中的时间戳转换为 datetime.datetime