【问题标题】:pymssql throws ImportError: No module named _mssql when build app with py2exepymssql 抛出 ImportError: No module named _mssql when build app with py2exe
【发布时间】:2011-10-19 17:34:43
【问题描述】:

我有应该作为 Windows 可执行文件启动的 python 应用程序。我正在使用 py2exe 和 pymssql 1.9.908。

我使用下一个构建脚本来生成应用程序:

from distutils.core import setup
import MySQLdb
import fnmatch
import os
import pymssql
import shutil
import py2exe
import glob

##############

name = 'BGAgent'
old_version = '0.1'
ver = '0.1'

distDir = 'Dist' + name + ver
shutil.rmtree(distDir, True)
shutil.rmtree('Dist' + name + old_version, True)

os.mkdir(distDir)

##############

class Target(object):
    """ A simple class that holds information on our executable file. """
    def __init__(self, **kw):
        """ Default class constructor. Update as you need. """
        self.__dict__.update(kw)

# MySQLdb
#dst = os.path.join(distDir, "MySQLdb")
#copy_tree(MySQLdb.__path__[0], dst )
# pymssql
site_packages_dir = os.path.dirname(pymssql.__file__)
pymssql_files = []#'pymssql.py', 'pymssql.pyc', 'pymssql.pyo', '_mssql.pyd']
for eggInfo in glob.glob(os.path.join(site_packages_dir, '*mssql*')) :
    pymssql_files.append(os.path.basename(eggInfo))

for fname in pymssql_files :
    src = os.path.join(site_packages_dir, fname)
    dst = os.path.join(distDir, fname)
    if(os.path.isfile(src)) :
        shutil.copy(src, dst)
    else :
        shutil.copytree(src, dst)

includes = ['MySQLdb', 'pymssql', 'OpenSSL']
excludes = ['run_w.exe'] #['_gtkagg', '_tkagg', 'bsddb', 'curses', 'email', 'pywin.debugger', 'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl', 'Tkconstants', 'Tkinter']

packages = ['MySQLdb', 'pymssql', 'OpenSSL']

dll_excludes = []#['libgdk-win32-2.0-0.dll', 'libgobject-2.0-0.dll', 'tcl84.dll', 'tk84.dll']

data_files = ['server.pem', 
              'config.ini', 
              'run.bat',
              #os.path.join(os.path.split(pymssql.__file__)[0], 'ntwdblib.dll'),
              ]
icon_resources = []
bitmap_resources = []
other_resources = []


MyApp_Target = Target(
    # what to build
    script = "run.py",
    icon_resources = icon_resources,
    bitmap_resources = bitmap_resources,
    other_resources = other_resources,
    dest_base = name,    
    version = ver,
    company_name = "",
    copyright = "",
    name = name,
    )


setup(
    data_files = data_files,
    options = {"py2exe": {"compressed": 0, 
                          "optimize": 1,
                          "includes": includes,
                          "excludes": excludes,
                          "packages": packages,
                          "dll_excludes": dll_excludes,
                          "bundle_files": 3,
                          "dist_dir": distDir,
                          "xref": False,
                          "skip_archive": False,
                          "ascii": False,
                          "custom_boot_script": '',
                         }
              },
    zipfile = r'library.zip',
    console = [],
    windows = [MyApp_Target],
    service = [],
    com_server = [],
    ctypes_com_server = []
    )

构建工作,但我尝试启动应用程序时出错:

  File "pymssql.pyo", line 12, in <module>
  File "pymssql.pyo", line 10, in __load
  File "_mssql.pxd", line 10, in init pymssql (pymssql.c:7370)
ImportError: No module named _mssql

_mssql.pyd 和 pymssql.pyd 文件在可执行目录中。

操作系统版本 Windows 7 Enterprice SP 1。

【问题讨论】:

    标签: python py2exe pymssql


    【解决方案1】:

    在您尝试导入的程序中(例如,在 A.py 中的 A.exe 中),也为 _mssql 指定导入语句。您可能还需要导入几个其他模块(十进制和 uuid)才能使 exe 正常工作

    【讨论】:

    • 这是 pymssql 中的错误。如果我在套接字模块隐式地导入 _mssql 一切正常。我找到了解决方法,但还是谢谢。
    【解决方案2】:

    只需在您的文件中添加声明import _mssql。接下来,运行您的程序。当你得到同样的东西时,只需在你的代码中导入那个模块。这种方法对我很有效。

    【讨论】:

      【解决方案3】:
      from distutils.core import setup
      
      import py2exe, os, pymssql
      import decimal
      
      data_files = []
      data_files.append(os.path.join(os.path.split(pymssql.__file__)[0], 'ntwdblib.dll'))
      py2exe_options = {"py2exe":{"includes": ['decimal'],
                      "dll_excludes":["mswsock.dll",
                      "powrprof.dll",
                      "user32.dll",
                      "shell32.dll",
                      "wsock32.dll",
                      "advapi32.dll",
                      "kernel32.dll",
                      "ntwdblib.dll",
                      "ws2_32.dll",
                      "oleaut32.dll",
                      "ole32.dll",
                                  ],
      }}
      
      setup(console=["jobs_pcc_main.py"], options= py2exe_options, data_files=data_files)
      

      【讨论】:

      • 嗨,我发布了我的 setup.py,我遇到了同样的问题,请参考这个 setup.py,我认为你的代码会好的。
      • 感谢您的提示。为什么将 ntwdblib.dll 包含为数据文件并排除为 DLL?诀窍是什么?我的问题是套接字模块。它看起来像 pymssql 猴子修补它,我在 pymssql 之前导入了它。
      【解决方案4】:

      对谁有帮助 我在尝试使用 Pyinstaller 和 我终于做对了,所以在这里发帖可能会对某人有所帮助 使用 pip install pymssql 安装 pymssql 后

      在 pyinstaller 中使用此参数将其包含在内

      --onefile --paths=.\venv\ --hidden-import='pymssql' --hidden-import='uuid' --collect-all='pymssql' --collect-all='uuid'
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-10-25
        • 2013-01-31
        • 1970-01-01
        • 1970-01-01
        • 2016-08-01
        • 2017-08-20
        • 2011-07-26
        相关资源
        最近更新 更多