使用带有sqlachemy的cxfreeze exe会产生错误:sqlalchemy.exc.NoSuchModuleError:无法加载插件:sqlalchemy.dialects:mysql

时间:2018-05-10 12:43:51

标签: python python-3.x sqlalchemy cx-freeze

我有一个带有以下功能的python脚本:

#! /usr/bin/env python3
# generate_settings.py
import sqlalchemy
from typing import List

def get_settings_from_db(table_name: str, **db_args: str) -> List[Setting]:
    """
    Gets the settings from the specified database, and returns them as an
    ordered list of database objects.
    """
    server_address = 'mysql://{user}:{pwd}@{ip}:{port}/{db_name}'.format(**db_args)

    engine = sqlalchemy.create_engine(server_address)

    # Setting is just a namedtuple
    return [Setting(s) for s
            in engine.execute(f'SELECT * FROM {table_name}').fetchall()]

当作为python脚本正常运行时(如python generate_settings.py none),一切正常。但是,当我使用cx_freeze进行编译(没有问题)并运行可执行文件时,我收到以下错误:

$ ./dist/generate_settings.exe none
Traceback (most recent call last):
  File "C:\Programs\Anaconda3\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 14, in run
    module.run()
  File "C:\Programs\Anaconda3\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26, in run
    exec(code, m.__dict__)
  File "generate_settings.py", line 269, in <module>
    print(build_file(args.package, **db_args))
  File "generate_settings.py", line 124, in build_file
    methods = build_methods(get_settings_from_db(**db_args))
  File "generate_settings.py", line 99, in get_settings_from_db
    engine = sqlalchemy.create_engine(server_address)
  File "C:\Programs\Anaconda3\lib\site-packages\sqlalchemy\engine\__init__.py", line 419, in create_engine
    return strategy.create(*args, **kwargs)
  File "C:\Programs\Anaconda3\lib\site-packages\sqlalchemy\engine\strategies.py", line 56, in create
    entrypoint = u._get_entrypoint()
  File "C:\Programs\Anaconda3\lib\site-packages\sqlalchemy\engine\url.py", line 150, in _get_entrypoint
    cls = registry.load(name)
  File "C:\Programs\Anaconda3\lib\site-packages\sqlalchemy\util\langhelpers.py", line 221, in load
    (self.group, name))
sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:mysql

我尝试过制作一个setup.py脚本并运行python setup.py build,但在运行.exe时仍会遇到同样的错误:

#! /usr/bin/env python3
# setup.py

import sys
from cx_Freeze import setup, Executable
import os.path

# needed for some reason https://stackoverflow.com/a/43034479/4819449
PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')

build_exe_options  = {
    "packages": [
        "argparse",
        "re",
        "getpass",
        "attr",
        "sqlalchemy",
        "warnings",
        "pathlib",
        "datetime",
        "typing"
    ],
    "include_files": [
        os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
        os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'),
    ]
}

setup(  name = "generate_settings",
        version = "0.1",
        options = {"build_exe": build_exe_options},
        executables = [Executable("generate_settings.py")])

这是在Windows 10上使用python 3.6.1,cxfreeze 5.1.1和sqlalchemy 1.2.1

0 个答案:

没有答案