将 cx_freeze 与 mysql-connector 一起使用

Posted

技术标签:

【中文标题】将 cx_freeze 与 mysql-connector 一起使用【英文标题】:use cx_freeze with mysql-connector 【发布时间】:2014-06-30 07:34:36 【问题描述】:

我正在尝试从功能齐全的 python 3.4 脚本制作一个 exe 程序,但我无法嵌入有关官方 mysql 连接器的依赖项。 这是一个有问题的示例代码:

import mysql.connector
from settings import *

connLocal  = mysql.connector.connect( host   =  DB_CRM_HOST,
                                      user   =  DB_CRM_USER,
                                      passwd =  DB_CRM_PASS,
                                      db     =  DB_CRM_DB )
cursorLocal = connLocal.cursor ()
sqlStr = "SELECT * FROM Users"
cursorLocal.execute( sqlStr)
for row in  cursorLocal.fetchall():
    print(row)

这是我的设置脚本:

'''script per il setup'''
import sys
from cx_Freeze import setup, Executable

EXCLUDES = ['_ssl',  # Exclude _ssl
            'pyreadline', 'difflib', 'doctest', 'locale',
            'optparse', 'pickle', 'calendar']  # Exclude standard library
PACKAGES = []
INCLUDES = []
SCRIPT_NAME = "sync_crm2web.py"
EXE_NAME = "sync_crm2web.exe"
PRJ_NAME = "sync_crm2web"
VERSION = 1.0
AUTHOR = "Antonio"
DESCRIPTION = "Sincronizzazione crm sito internet"
BASE = "Console" #"Win32GUI"

#------------------------------------------------------------------------------
BUILD_EXE_OPTIONS = "packages": PACKAGES,
                     "excludes": EXCLUDES,
                     "includes": INCLUDES,
                     "path": sys.path,
                     'append_script_to_exe':False,
                     'build_exe':"dist/bin",
                     'compressed':True,
                     'copy_dependent_files':True,
                     'create_shared_zip':True,
                     'include_in_shared_zip':True,
                     'optimize':2,

EXE = Executable(script=SCRIPT_NAME,
                 base=BASE,
                 compress=True,
                 targetDir="dist",
                 targetName=EXE_NAME,
                 initScript=None,
                 copyDependentFiles=True,
                 appendScriptToExe=True,
                 appendScriptToLibrary=False,
)

setup(name=PRJ_NAME,
      version=VERSION,
      author=AUTHOR,
      description=DESCRIPTION,
      options="build_exe": BUILD_EXE_OPTIONS,
      executables=[EXE])

还尝试强制 PACKAGES = [], INCLUDES = [] 与 mysql、mysql-connector、mysql.connector 的组合似乎不起作用。

我总是得到:

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in <module>
    exec(code, m.__dict__)
  File "sync_crm2web.py", line 1, in <module>
  File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 2214, in _find_and_load
  File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 2189, in _find_and_load_unlocked
  File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 321, in _call_with_frames_removed
  File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 2214, in _find_and_load
  File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 2201, in _find_and_load_unlocked
ImportError: No module named 'mysql'

有人可以帮助我吗?完整的 cx_freeze 日志在这里http://pastebin.com/S3TMzAnB

【问题讨论】:

当您说它不起作用时,请描述问题所在,例如你看到了什么错误。此外,冻结的日志可能包含有用的信息 - 您可以将其放入粘贴箱并提供链接吗? 谢谢,我已经添加了必要的信息 【参考方案1】:

使用来自Cx-Freeze Error - Python 34 - www.lfd.uci.edu/~gohlke/pythonlibs 的输入解决了问题,所以我从这个安装程序而不是 PIP 安装了 cx_Freeze。

之后我注释了所需的 _ssl 模块并将所有库('build_exe' 属性)放在可执行文件的同一文件夹中。

'''script per il setup'''
import sys
from cx_Freeze import setup, Executable

EXCLUDES = [#'_ssl',  # !!!! COMMENTED !!!!
            'pyreadline', 'difflib', 'doctest', 'locale',
            'optparse', 'pickle', 'calendar']  # Exclude standard library
PACKAGES = []
INCLUDES = []
SCRIPT_NAME = "sync_crm2web.py"
EXE_NAME = "sync_crm2web.exe"
PRJ_NAME = "sync_crm2web"
VERSION = 1.0
AUTHOR = "Antonio"
DESCRIPTION = "Sincronizzazione crm sito internet"
BASE = "Console" #"Win32GUI"

#------------------------------------------------------------------------------
BUILD_EXE_OPTIONS = "packages": PACKAGES,
                     "excludes": EXCLUDES,
                     "includes": INCLUDES,
                     "path": sys.path,
                     'append_script_to_exe':False,
                     'build_exe':"dist", # !!!! BEFORE WAS dist/bin !!!!
                     'compressed':True,
                     'copy_dependent_files':True,
                     'create_shared_zip':True,
                     'include_in_shared_zip':True,
                     'optimize':2,

EXE = Executable(script=SCRIPT_NAME,
                 base=BASE,
                 compress=True,
                 targetDir="dist",
                 targetName=EXE_NAME,
                 initScript=None,
                 copyDependentFiles=True,
                 appendScriptToExe=True,
                 appendScriptToLibrary=False,
)

setup(name=PRJ_NAME,
      version=VERSION,
      author=AUTHOR,
      description=DESCRIPTION,
      options="build_exe": BUILD_EXE_OPTIONS,
      executables=[EXE])

【讨论】:

以上是关于将 cx_freeze 与 mysql-connector 一起使用的主要内容,如果未能解决你的问题,请参考以下文章

将 CX_Freeze 与 Scipy 一起使用:scipy.special._ufuncs.py

如何将 cx_Freeze 可执行文件的 .pyd 和子文件夹与可执行文件分开放在一个文件夹中

cx_freeze 问题与 mac 上的相对路径

cx_Freeze:找不到文件/目录 icon.gif

cx_freeze 无法捆绑 django.utils.six.moves

CX_freeze 可执行文件将不起作用。 _tkinter DLL 加载失败