Search code examples
pythonsqlalchemypython-3.4psycopg2cx-freeze

cx_Freeze exe results in sqlalchemy.exc.NoSuchmoduleError with psycopg2 at run time


Edit: What tools can I use to see what packages/file the executable is trying to find when it tries to access the psycopg2 package? Perhaps that can help profile where things are going wrong.

I have a python script that runs perfectly fine when run using the interpreter yet when I freeze it, I am getting the error:

sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgresql.psycopg2

Because it runs fine through the interpreter and fails when frozen, I suspect that something is wrong with my setup.py file.

#-*- coding: 'utf-8' -*-

from cx_Freeze import setup, Executable
import sys

# Dependencies are automatically detected, but it might need
# fine tuning.

# execute this file with the command: python setup.py build

buildOptions = dict(packages = ['ht_cg.ht_cg_objects'],
                    includes = ['ht_cg.ht_cg_objects'],
                    excludes = ['tkinter', 'PyQt4', 'matplotlib', 'tcl', 'scipy'],
                    include_files = ['./cg_source_prep/readme.txt', "./ht_cg_objects/ht_db_config.cfg"],
                    build_exe = 'build/source_density_exe')

sys.path.append('./')
sys.path.append('../')
executables = [
    Executable(script = "cg_source_save.py",
                initScript = None,
                base='Console',
                targetName = 'source_density_save.exe',
                copyDependentFiles = True,
                compress = True,
                appendScriptToExe = True,
                appendScriptToLibrary = True,
                shortcutName="CG Source Density",
                shortcutDir='DesktopFolder',
                icon = "./cg_source_prep/archimedes.ico"
            )
                ]

setup(name='Source_Density',
      version = '1.0',
      description = 'Source Density Uploader',
      author = 'zeppelin_d',
      author_email = 'zeppelin_d@email',
      options = dict(build_exe = buildOptions),
      executables = executables)
  1. I've tried adding 'psycopg2' to the includes list and the packages list.
  2. I've installed the psycopg2 binary installer.
  3. I've made sure 'psycopg2' is not in the includes or packages list.
  4. I've got the MS Visual C++ 2008 redistributable x64 and x86 packages installed.
  5. Mike Bayer says a similar error is due to the connection string not being what I think it is but I am printing my conn string right before I create the engine and it is correct. It's only broken when frozen and the same connection string works fine otherwise.
  6. I have another frozen script that runs without error. These two scripts use the same method to create the engine and metadata objects from sqlalchemy. I've tried copying the psycopg2 files from the working executable folder to the broken one with no results. I've tried copying over random dlls and no luck.

I'm reading the connection string from the ht_db_config.cfg file that is base64 encoded but I print out the string just before attempting sqlalchemy.create_engine() and it's correct. I also added a string literal as the argument to the sqlalchemy.create_engine method and the frozen executable fails. The actual output from the script that fails is:

postgresql+psycopg2://user_name:[email protected]:5432/ht_cg_prod

I've replaced the username and the password.

I've been trying to get this fixed for a couple of days. I'd be grateful for any help. I'm running python 3.4, sqlalchemy 1.0.8, cx_freeze 4.3.4 and psycopg2 2.6 as determined by 'conda list' on windows 8.1. Thanks.


Solution

  • I finally found an answer to this. On the cx_freeze mailing list someone else had the same problem. The solution was to add 'sqlalchemy.dialects.postgresql' to my list of packages in the cx_freeze build options.