将cx_freeze与mysql-connector一起使用

时间:2014-06-30 07:34:36

标签: python mysql cx-freeze mysql-connector-python

我试图用功能齐全的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 个答案:

答案 0 :(得分:1)

使用Cx-Freeze Error - Python 34 - www.lfd.uci.edu/~gohlke/pythonlibs的输入解决,因此我从此安装程序安装了cx_Freeze,而不是从PIP安装。

之后,我评论了所需的 _ssl 模块,并将所有库(&#39; build_exe&#39;属性)放在可执行文件的同一文件夹中。

'''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])