0

I have a python script with the following function:

#! /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()]

When run normally as a python script (as python generate_settings.py none) all works fine. However, when I compile with cx_freeze (no issues) and run the executable I get the following error:

$ ./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

I have tried making a setup.py script and running python setup.py build instead but still get the same error when running the .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")])

This is on windows 10 with python 3.6.1, cxfreeze 5.1.1 and sqlalchemy 1.2.1

FHTMitchell
  • 11,793
  • 2
  • 35
  • 47

1 Answers1

0

Found the solution. I needed to include "mySQLdb" in my build_exe_options["packages"] in setup.py.

#! /usr/bin/env python3
# setup.py
"""
This creates the cx_freeze executable in a `build/` directory.
Run using
    python setup.py build
"""

from cx_Freeze import setup, Executable
from generate_settings import __version__

build_exe_options  = {
    "packages": [
        "attr",
        "sqlalchemy",
        "mySQLdb"
    ],
    "excludes": [
        "tkinter",
        "pyqt",
        "scipy",
        "numpy",
        "matplotlib",
        "notebook",
        "distutils",
        "email"
    ]
}

setup(  name = "generate_settings",
        version = __version__,
        options = {"build_exe": build_exe_options},
        executables = [Executable("generate_settings.py")])
FHTMitchell
  • 11,793
  • 2
  • 35
  • 47