Skip to content

mssql-python not working from Pyinstaller generated exe #733

Description

@jariml

I'm trying to create a Windows executable with PyInstaller, but I'm getting the following error even though I've included all the necessary DLL, RLL, and PYD files in the package. Do you have any suggestions on how to fix this?

Error message when trying to run dist\hello\hello.exe:
File "mssql_python\connection.py", line 127, in _raise_connection_error
mssql_python.exceptions.OperationalError: Driver Error: Connection operation failed; DDBC Error: Unable to cast Python instance of type <class 'NoneType'> to C++ type '?' (#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)
[PYI-86436:ERROR] Failed to execute script 'hello' due to unhandled exception!

Minimal reproducible example:

pyproject.toml

[project]
name = "mssql-python-pyinstaller"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
    "mssql-python>=1.14.0",
]

[dependency-groups]
dev = [
    "pyinstaller>=6.22.2",
]

hello.spec

# -*- mode: python ; coding: utf-8 -*-

from PyInstaller.utils.hooks import collect_dynamic_libs    
binaries_to_include = collect_dynamic_libs('mssql_python', search_patterns=['*.dll', '*.pyd'])
binaries_to_include.extend(collect_dynamic_libs('mssql_python_odbc', search_patterns=['*.dll', '*.pyd', '*.rll']))

a = Analysis(
    ['hello.py'],
    pathex=[],
    binaries=binaries_to_include,
    datas=[],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    noarchive=False,
    optimize=0,
)
pyz = PYZ(a.pure)

exe = EXE(
    pyz,
    a.scripts,
    [],
    exclude_binaries=True,
    name='hello',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    console=True,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)
coll = COLLECT(
    exe,
    a.binaries,
    a.datas,
    strip=False,
    upx=True,
    upx_exclude=[],
    name='hello',
)

hello.py

import mssql_python

DSN = "Server=localhost,1433;Database=DB;UID=uid;PWD=pwd;Trusted_Connection=no;Encrypt=no;Authentication=SqlPassword"


def main():
    with mssql_python.connect(DSN) as conn:
        conn.execute("select 1")


if __name__ == "__main__":
    main()

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    triage neededFor new issues, not triaged yet.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions