Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
978 views
in Technique[技术] by (71.8m points)

python 3.x - ImportError: DLL load failed: The specified module could not be found -- IBM DB2

I know there have been many people with this same issue, but here is my situation which I have not been able to find the exact same problem. I am building an executable with pyinstaller and I keep getting the importError. I am using ibm_db package to connect to a IBM DB2 database and do inserts into a table using pandas to_sql method. I used pyinstaller on my program before I added the SQL code so I'm pretty sure it has something to do with my trying to connect to DB2, but for the life of me I cannot figure this out.

I get lots of warnings and info messages when I"m running pyinstaller but no errors that I see. I only get the error once I try to execute the executable file that pyinstaller built.

I have tried to run it in a virtual environment to try to isolate the issue but I am not that familiar with virtual environments, so I stop trying to use that.

Traceback (most recent call last):
  File "rebate_gui_sql.py", line 9, in <module>
  File "c:usersdt24358libsite-packagesPyInstallerloaderpyimod03_importers.py", line 627, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packagesibm_db.py", line 10, in <module>
  File "site-packagesibm_db.py", line 9, in __bootstrap__
  File "imp.py", line 342, in load_dynamic
ImportError: DLL load failed: The specified module could not be found.
[11020] Failed to execute script rebate_gui_sql

Update: 5/1/2019 from comment below, here is my simple program

import pandas as pd
from tkinter import *
from tkinter import ttk
import ibm_db
import ibm_db_dbi as db
from sqlalchemy import create_engine

class Application(Frame):

    def __init__(self, master):
        ttk.Frame.__init__(self, master)  
        self.master = master
        self.run_process()

    def run_process(self):
        engine = create_engine("db2+ibm_db://userid:password@url:port/database")
        conn = engine.connect()
        print("Connected to " + str(engine))

        sql = '''
            Select *
            from rebteam.pd5
            fetch first row only
            '''

        df = pd.read_sql(sql, conn)
        print(df)

        df.to_csv(r'c:usersdt24358scriptspricing toolGUI_SQLest.csv', index=False)
        self.result_label = Label(root, text="Select of PD5 Successful", bg="light green", width=80, justify=LEFT)
        self.result_label.grid(row=0,columnspan=2)

root=Tk()
root.title("Rebate Bid Data Upload")
root.configure(background="light green")
app = Application(root)
root.mainloop()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This answer is relevant for these versions:

python - up to 3.7 (but not higher)  
pyinstaller 3.4  
setuptools 41.0.1 
ibm_db 3.0.1
ibm_db_sa 0.3.4
sqlalchemy 1.3.3

There are separate issues here.

The immediate issue (the ImportError) is the failure to load ibm_db.dll

The ImportError happens because pyinstaller does not copy external (non python) libraries into the bundle unless you explicitly request that to happen.

pyinstaller will also not copy a Db2-client into the bundle unless you explicitly tell it to do that, which means that if your target hostname to which you deploy your built-executable does not already have a preconfigured preinstalled Db2-client then you will also experience the failure to load ibm_db module.

The pyinstaller option --add-binary gives a workaround for some kinds of ImportError , see example below. If you are not using SQLAlchemy just skip those parts of this answer.

The pyinstaller option --add-data gives a workaround for adding directories (for example the clidriver directory for adding a Db2-driver) when your target environment lacks a Db2-driver.

Note that this answer does not require you to use SQLAlchemy, the answer is also relevant if you are only using ibm_db (or ibm_db_dbi), in which case just skip the SQLAlchemy parts.

If your python script uses SQLAlchemy to access Db2, then you may see a second symptom at run time after building with pyinstaller. The run time symptom is either:

"sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:ibm_db_sa"

or

"sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:db2.ibm_db"

(depending on the prefix for the url given to create_engine())

This symptom sqlalchemy.exe.NoSuchModuleError is not specific to Db2 but can impact other databases when used via SQLAlchemy with an external dialect ( Db2, teradata, snowflake, presto,...). Databases that use SQLAlchemy internal dialects may just work out of the box.

Here is one workaround for SQLAlchemy, other workarounds are possible.

SQLAlchemy external dialects use pkg_resources entry_points to allow SQLAlchemy to exploit them, but pyinstaller cannot yet handle these, without some assistance from you. Such entry point information is a kind of meta data about the module.

This workaround uses pyinstaller hooks to collect the metadata of the relevant modules , and tells pyinstaller the directory (or directories) that contain these hook files. For Db2 with SQLAlchemy, three hook files are needed, hook-ibm_db.py, hook-ibm_db_sa.py, hook-sqlalchemy.py. I choose to put these hook files in the same directory as my source file python script.

The contents of each of these files is trivial two lines, and the contents differ only by the module name contained within. Here is an example of one of the files hook-sqlalchemy.py (for the other 2 required files, just replace the module name appropriately):

from PyInstaller.utils.hooks  import  copy_metadata

datas = copy_metadata('sqlalchemy')

To add ibm_db.dll via the --add-binary method, you can either use a command line option to pyinstaller or edit the spec file.

For handling load failures of ibm_db.dll alone, just use the --add-binary additional option like this:

pyinstaller -y --add-binary  %LOCALAPPDATA%ProgramsPythonPython37Libsite-packagesibm_db_dllsibm_db.dll;.ibm_db_dlls your_script.py

If you want to include clidriver in your bundle, first find the fully qualified pathname to its location via:

pip show ibm_db

and in the output of that command see the Location: line which has the first part of the fully qualified pathname , so you append CLIDRIVER to that path and use it in the --add-data additional option like this:

--add-data="c:pathoclidriver;.clidriver"

If you do include clidriver in your bundle, there are additional considerations, see the notes section below.

For apps that also use SQLAlchemy, you need additional steps.

Suppose that the ibm_db.dll lives in this directory:

%LOCALAPPDATA%programspythonpython37libsite-packagesibm_db_dlls

and you make a variable in a CMD.EXE shell to point to that location:

> set ibm_db_path=%LOCALAPPDATA%programspythonpython37libsite-packagesibm_db_dlls

For an MS-Windows batch file (using ^ as line continuation character), the pyinstaller command line example to handle both of the workarounds mentioned above is:

pyinstaller -y ^
--additional-hooks-dir=. ^
--hidden-import ibm_db_sa.ibm_db ^
--hidden-import ibm_db_dbi ^
--hidden-import ibm_db ^
--add-binary  %LOCALAPPDATA%ProgramsPythonPython37Libsite-packagesibm_db_dllsibm_db.dll;.ibm_db_dlls ^
 your_script.py

Notes:

  • If your python script explicitly imports the SQLAlchemy modules then you do not need to specify them via --hidden-import options (buy you still need the hooks for SQLAlchemy to operate after bundling).

  • For ibm_db versions up to 3.0.2, the ibm_db.dll needs to be in subdirectory ibm_db_dlls in your bundle, which is the reason for specifying that destination on the --add-binary option.

  • If you are building for Linux/Unix, instead of ^. use as the line continuation character as usual.

  • If you intend to copy your built executable to a new hostname, and that new hostname does not already have a pre-installed Db2-client , and you do not wish to install a separate Db2-client on the target, then you can bundle clidriver with the pyinstaller output with the --add-data option shown above.

  • If you bundle clidriver be aware that you may also need to bundle its configuration files (such as db2dsdriver.cfg and db2cli.ini) if they are in non-default locations, depending on whether your code uses externally configured DSNs or long connection-strings. If you do not bundle such configuration files (implicitly or explicitly) and you deploy your built environment to a different hostname than the build environment then you will need to reconfigure those files at the target hostname. The default location for these files is in the clidrivercfg directory which will get included via --add-data as mentioned earlier.

  • If you bundle clidriver, and if you are using encrypted connections to Db2 via TLS/SSL, be aware you may also need to bundle additional files such as certificates, keystore/stash files etc when you run the pyinstaller build.

  • If you bundle clidriver, be aware that IBM refreshes this component a couple of times per year with bug fixes and security fixes and new functions, so you may need to refresh your executables periodically to prevent them from becoming security holes by being frozen in time with old versions.

  • if you bundle clidriver and if you need to use odbcad32 on the target hostname for configuring Db2 DSNs, then following deployment on the target hostname remember to run the clidriverindb2cli install -setup command on the target hostname.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...