Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError: DLL load failed while importing ibm_db: #887

Open
ZephyrusMB opened this issue Sep 6, 2023 · 30 comments
Open

ImportError: DLL load failed while importing ibm_db: #887

ZephyrusMB opened this issue Sep 6, 2023 · 30 comments

Comments

@ZephyrusMB
Copy link

"ImportError: DLL load failed while importing ibm_db" raised while executing script. Working with ibm_db version 3.1.4.
Dlls are not present with 3.2.0 version.

  • Operating System Name: Microsoft Windows 11 10.0.22621

  • Python Version: 3.11.4 and 3.11.5

  • ibm_db version: 3.2.0

  • Test script to reproduce the problem
    Failed at line 1 import ibm_db

import ibm_db
import ibm_db_dbi
ibm_db_conn= ibm_db.connect(connectionString, "", "")
conn = ibm_db_dbi.Connection(ibm_db_conn)

Steps to Reproduce:

  1. install module via pip install --upgrade ibm_db
  2. execute script python shared in the post
@Earammak
Copy link
Collaborator

Earammak commented Sep 7, 2023

@ZephyrusMB, Can you please run as below, before import ibm_db.
import os
os.add_dll_directory('path to clidriver installation until bin')
import ibm_db

@dverweij-dev
Copy link

Also upgraded from ibm-db version 3.1.4 to 3.2.0 and ran into the error "ImportError: DLL load failed while importing ibm_db".
OS = Windows 10 Enterprise and version = 10.0.19044.
And yes, when adding os.add_dll_directory('path to clidriver installation until bin') the problem is fixed.
Before changing things, can I assume this is a bug/omission which wil be fixed in the next version of this package?

@Earammak
Copy link
Collaborator

Earammak commented Sep 11, 2023

@ZephyrusMB This we need to add manually for windows only. And the same is documented in README.

@dverweij-dev
Copy link

I studied the README before posting this comment, so it was clear to me what needed to be done. The issue I want to address is that this package is not downwards compatible and platform agnostic anymore and we need to change several scripts.

Using ibm-db==3.1.4 we can run the same scripts on Windows, Linux and in Containers.

If I want to use ibm-db==3.2.0 I have to alter these scripts to check if os.name == 'nt' (or os.name != 'posix') and then execute os.add_dll_directory(os.getenv('IBM_DB_HOME') + '/bin').

I'm using this driver mainly in combination with RobotFramework (this is a testautomation framework) third party libraries (e.g. robotframework-databaselibrary). I don't want to change these third party libraries, so need to use IF and Evaluate construction to make it platform independent in my testscripts again:

Connect To DB2 Database
    # Fix DLL issue with package ibm-db version 3.2.0
    IF    (os.name == 'nt')
        Evaluate    os.add_dll_directory(os.getenv('IBM_DB_HOME') + '/bin')    modules=os
    END
    ...
    Connect To Database    ibm_db_dbi    ${DB2_DATABASE}    ${username}    ${password}    ${DB2_HOST}    ${DB2_PORT}
    ...
    ...

The question is, can't you add this construction:

if os.name == 'nt':
    os.add_dll_directory(os.getenv('IBM_DB_HOME') + '/bin')

or other construction to the ibm-db module to make it platform agnostic again and downwards compatible with ibm-db==3.1.4?

If there is good reason to not do this, please shed some light on this as to why. Is it conflicting somewhere else?

If it cannot be changed, then we will stick to ibm-db==3.1.4 for now an change our scripts later on to work with ibm-db=3.2.0 or later version.

@Rishi-spec-art
Copy link

@Earammak I'm getting this error:
OSError: [WinError 87] The parameter is incorrect: 'path to clidriver installation until bin'

@ZephyrusMB
Copy link
Author

Yes please as described by @dverweij-dev the expected behavior is to be platform agnostic as in version 3.1.4.
Is there any specific reason to introduce this breaking change into version 3.2.0? Would you solve this point in the future?

Kind regards

@bimalkjha
Copy link
Member

@frbelotto You need to run below command as documented here as Note:

import os
os.add_dll_directory('path to clidriver installation until bin')
import ibm_db

Also, this solution is already given in this comment: #887 (comment)
Probably, you have not read the full thread. Try it. Thanks.

@readytheory
Copy link

@Earammak I'm getting this error: OSError: [WinError 87] The parameter is incorrect: 'path to clidriver installation until bin'

@Rishi-spec-art they mean something like:

os.add_dll_directory('/venv/cashapp/Lib/site-packages/clidriver/bin')

... whatever the path is to where pip put your clidriver/bin. IBM team, I'd suggest clarifying that wording. "until bin" sounds to me like you don't want it to stop at "clidrivder/"

@frbelotto
Copy link

@frbelotto You need to run below command as documented here as Note:

import os
os.add_dll_directory('path to clidriver installation until bin')
import ibm_db

Also, this solution is already given in this comment: #887 (comment) Probably, you have not read the full thread. Try it. Thanks.

Just to understand. Such installation (ODBC and CLI driver) wasn't needed before? I have been using IBM_DB for a while and started getting this error recently.

@bimalkjha
Copy link
Member

@readytheory Yes, do not stop at clidriver but clidriver/bin as in os.add_dll_directory('/venv/cashapp/Lib/site-packages/clidriver/bin'). Thanks.

@bimalkjha
Copy link
Member

@frbelotto You are right. Actually from ibm_db version 3.2.0, we have discontinued shipping ibm_db.dll file and switched on wheel support for Linux, Mac and Windows to avoid compilation related issues. So, change in behavior. Thanks.

@LinusConradM
Copy link

I got this error despite following the guide

  1. install module via pip install --upgrade ibm_db (this shows "Requirement already satisfied: ibm_db in c:\users\cmuhirwe\appdata\local\anaconda3\lib\site-packages (3.2.0)"

  2. execute script python shared in the post
    import ibm_db
    import ibm_db_dbi
    ibm_db_conn= ibm_db.connect(connectionString, "", "")
    conn = ibm_db_dbi.Connection(ibm_db_conn)

Bellow is the error
ImportError Traceback (most recent call last)
Cell In[18], line 1
----> 1 import ibm_db
2 import ibm_db_dbi
3 ibm_db_conn= ibm_db.connect(connectionString, "", "")

ImportError: DLL load failed while importing ibm_db: The specified module could not be found.

@bimalkjha
Copy link
Member

@LinusConradM Seems you have not went through the comments in this issue. Please check it: #887 (comment) Thanks.

@rredburn
Copy link
Contributor

rredburn commented Nov 3, 2023

add_dll_directory to every script that needs db2 access? What a terrible user experience, I can't believe this is the best answer. Please re-evaluate the strategy.

@frbelotto
Copy link

Does anyone can confirm what is the lastest version of the library that does not request adding the dll manually? I want to set it up on my requests.txt.

@bchoudhary6415
Copy link
Contributor

@frbelotto python-ibm_db v3.1.4 is the latest version of the library that does not request adding the dll manually.

Also from python version
The os.add_dll_directory() function is part of the os module in Python (introduced in Python 3.8), and it is used to add a directory to the DLL (Dynamic Link Library) search path. This function is particularly useful when working with third-party DLLs or shared libraries that your Python application depends on.

Before Python 3.8, the DLL search path was influenced by the PATH environment variable. However, this could lead to issues, especially when Python was embedded in larger applications. The os.add_dll_directory() function was introduced to provide a more controlled and isolated way of managing the DLL search path.

Here's a basic example of how to use os.add_dll_directory():

import os

Add a directory to the DLL search path
dll_directory = r'C:\Path\To\Your\DLLs'
os.add_dll_directory(dll_directory)

Now, when loading a DLL or using ctypes, the specified directory is considered in the search path
This function is particularly useful when dealing with scenarios where the DLLs needed by your Python application are not in the system's default search paths. By using os.add_dll_directory(), you can ensure that the specified directory is considered when resolving DLL dependencies.

It's important to note that this function is available only in Python 3.8 and later versions. If you're working with an earlier version of Python, you might need to consider alternative approaches or upgrade to a newer Python version.

Thanks.

@Xnot
Copy link

Xnot commented Apr 3, 2024

Regardless of whether you end up adding the ddl back or not, could you consider issuing deprecation warnings for breaking changes in the future?

Edit: Especially considering that installs for <3.2 are now also retroactively fucked because it relied on pulling the DDL from master, which is no longer there, so rolling back is not even an option.

@ralberrto
Copy link

ralberrto commented May 28, 2024

I'm running into this issue on redhat, it's failing to create databases but I can query existing ones:

>>> ibm_db.createdb(conn, "NEWDB")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Exception: Error in loading db2cli.dll library file

Am I missing something here?

@kxmatjob
Copy link

kxmatjob commented Jun 18, 2024


I have found a way to copy the clidriver and ibm_db_dlls folders from the ibm_db-3.1.4-py3.8.egg directory, as well as ibm_db.py and ibm_db_dbi.py, in the code directory.
@rredburn @readytheory @bimalkjha @frbelotto @ZephyrusMB


@ksaket1998
Copy link

@ZephyrusMB, Can you please run as below, before import ibm_db. import os os.add_dll_directory('path to clidriver installation until bin') import ibm_db

This works fine, can connect to db2 after running os.add_dll_directory.
But can you help me how to make it work after converting the python script to an exe which would be used in multiple systems ?

@bimalkjha
Copy link
Member

@ksaket1998 In your python script, you need to code to find the correct site_packages path that contains ibm_db.* file using code similar to below one:

import sys
for path in sys.path:
    if 'site-packages' in path:
        print(path)

Then check for clidriver under site_packages or directly check for clidriver instead of ibm_db* file. Then construct full path till clidriver\bin and add it to os.add_dll_directory(). Thanks.

@EliezerRamirezRuiz
Copy link

EliezerRamirezRuiz commented Jul 23, 2024

hey buddy, how are you?
I am recently developing a script, using the ibm_db library and I got the same error as you.

I have found a solution that at least it has helped me and other users, was to uninstall the library after the driver and do the step by step again.

1.- Install the IBM Driver
2.- Place a variable in the system called IBM_DB_HOME.
3.- Reinstall the ibm_db library.
4.- If it doesn't work try the following

from os import add_dll_directory
from os import environ


if "IBM_DB_HOME" not in environ["PATH"]:
    add_dll_directory(r"path ibm drivers")

from ibm_db import connect

p8cmprod_connection = connect("dsn", "user", "password")

I have also seen the solution of mapping to the clidriver that comes with the library but at least it didn't work for me, so any solution that works for you would be good to know.

@bimalkjha
Copy link
Member

bimalkjha commented Aug 26, 2024

@frbelotto To install or update python ibm_db driver, no need to install DB2 Client. It automatically downloads and install clidriver internally. But, if you have requirement to use specific version of Db2 Client or driver and want to avoid automatic download of clidriver during installation of ibm_db, then you need to follow the requirement of installation of Db2 Client.. and yes, most of the website wants registration to know their users.
Also, the wheel package of ibm_db comes with clidriver in-built. Search about IBM_DB_HOME on https://github.com/ibmdb/python-ibmdb page to know more about download of db2client by ibm_db and installation steps using separate db2client install. Thanks.

@frbelotto
Copy link

@frbelotto To install or update python ibm_db driver, no need to install DB2 Client. It automatically downloads and install clidriver internally. But, if you have requirement to use specific version of Db2 Client or driver and want to avoid automatic download of clidriver during installation of ibm_db, then you need to follow the requirement of installation of Db2 Client.. and yes, most of the website wants registration to know their users.

Also, the wheel package of ibm_db comes with clidriver in-built. Search about IBM_DB_HOME on https://github.com/ibmdb/python-ibmdb page to know more about download of db2client by ibm_db and installation steps using separate db2client install. Thanks.

Hello!
The ibm db installation,on windows , just talk about setting the environment path, but it says "if you have db2 cli installed "

I've tried to install ibm db today, but as expected it was complaining about a missing DLL.

I don't need to use any specific version. I am trying to use a newer version so I could start using Python 3.12

@bimalkjha
Copy link
Member

bimalkjha commented Aug 27, 2024

@frbelotto This missing DLL error on windows is a known issue and we have already documented the solution and updated on this comment too. Thing is the path of clidriver/bin directory that you need to pass to add_dll_directory() is from your site_packages path. If you check your site_packages directory of python, you can see clidriver is present there. Just add the full path of clidriver\bin directory to add_dll_directory(). No need to have separate installation of db2client to get this directory. It gets automatically installed by ibm_db during installation of ibm_db. You can use it and fix the issue. Thanks.

@bimalkjha
Copy link
Member

bimalkjha commented Aug 27, 2024

Below screenshot shows how it works without installing separate db2client in the system post pip install:

image

@bimalkjha
Copy link
Member

bimalkjha commented Aug 27, 2024

@frbelotto Below is the complete steps with example that show there is no db2 client installed in my system and how ibm_db works on windows. I have python3.10 installed in my system, so same is used in this example. Same works for python 3.12 too.
I have dsn configured in the db2dsdriver.cfg file, so used it in below example to hide credentials. Same works for any connection string.
Please refer below steps and if you face any issue, let me know.

C:\>python --version
Python 3.10.10

C:\>python -m site
sys.path = [
    'C:\\',
    'C:\\Python310\\python310.zip',
    'C:\\Python310\\DLLs',
    'C:\\Python310\\lib',
    'C:\\Python310',
    'C:\\Python310\\lib\\site-packages',
]
USER_BASE: 'C:\\Users\\bjha\\AppData\\Roaming\\Python' (doesn't exist)
USER_SITE: 'C:\\Users\\bjha\\AppData\\Roaming\\Python\\Python310\\site-packages' (doesn't exist)
ENABLE_USER_SITE: True

C:\>pip list
Package         Version
--------------- -------
build           0.10.0
colorama        0.4.6
packaging       23.1
pip             22.3.1
pyproject_hooks 1.0.0
setuptools      68.0.0
tomli           2.0.1
wheel           0.40.0

C:\>dir C:\Python310\lib\site-packages\clidriver
 Volume in drive C is Windows
 Volume Serial Number is E21C-1A80

 Directory of C:\Python310\lib\site-packages

File Not Found

C:\>db2level
'db2level' is not recognized as an internal or external command,
operable program or batch file.

C:\>pip install ibm_db --no-cache-dir
Collecting ibm_db
  Downloading ibm_db-3.2.3-cp310-cp310-win_amd64.whl (27.8 MB)
     ---------------------------------------- 27.8/27.8 MB 5.2 MB/s eta 0:00:00
Installing collected packages: ibm_db
Successfully installed ibm_db-3.2.3

C:\>dir C:\Python310\lib\site-packages\clidriver
 Volume in drive C is Windows
 Volume Serial Number is E21C-1A80

 Directory of C:\Python310\lib\site-packages\clidriver

08/27/2024  08:45 PM    <DIR>          .
08/27/2024  08:45 PM    <DIR>          ..
08/27/2024  08:45 PM    <DIR>          bin
08/27/2024  08:45 PM    <DIR>          bnd
08/27/2024  08:45 PM    <DIR>          cfg
08/27/2024  08:45 PM    <DIR>          conv
08/27/2024  08:45 PM    <DIR>          db2
08/27/2024  08:45 PM    <DIR>          include
08/27/2024  08:45 PM    <DIR>          lib
08/27/2024  08:45 PM    <DIR>          license
08/27/2024  08:45 PM    <DIR>          msg
08/27/2024  08:45 PM    <DIR>          properties
08/27/2024  08:45 PM    <DIR>          scripts
08/27/2024  08:45 PM    <DIR>          security
08/27/2024  08:45 PM                 0 __init__.py
08/27/2024  08:45 PM    <DIR>          __pycache__
               1 File(s)              0 bytes
              15 Dir(s)  55,122,616,320 bytes free

C:\>copy C:\work\db2dsdriver.cfg C:\Python310\lib\site-packages\clidriver\cfg
        1 file(s) copied.

C:\>python
Python 3.10.10 (tags/v3.10.10:aad5f6a, Feb  7 2023, 17:20:36) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ibm_db
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: DLL load failed while importing ibm_db: The specified module could not be found.
>>> import os
>>> os.add_dll_directory('C:\\Python310\\Lib\\site-packages\\clidriver\\bin')
<AddedDllDirectory('C:\\Python310\\Lib\\site-packages\\clidriver\\bin')>
>>> import ibm_db
>>> connstr='dsn=mybludb;'
>>> conn=ibm_db.connect(connstr, '', '')
>>> cursor = ibm_db.exec_immediate(conn, "select 56, 'xyz' from sysibm.sysdummy1")
>>> cols = ibm_db.fetch_tuple( cursor )
>>> print(cols)
(56, 'xyz')
>>> ibm_db.close(conn)
True
>>> exit()

C:\>

Thanks.

@Gamhigh
Copy link

Gamhigh commented Oct 22, 2024

Screenshot 2024-10-22 081618
i'm setting IBM DB2 in D\ , and i can't connect with the ibm because DLL
import ibm_db
ImportError: DLL load failed while importing ibm_db: The specified module could not be found.

@bchoudhary6415
Copy link
Contributor

bchoudhary6415 commented Oct 22, 2024

@Gamhigh
You have imported ibm_db before importing DLL.
first import DLL then import ibm_db.

import os
os.add_dll_directory('path to clidriver installation until bin')
import ibm_db

Please don't share your userID and Password here.

Thank you

@Gamhigh
Copy link

Gamhigh commented Oct 22, 2024

@bchoudhary6415 thank you very much, I was able to log in. I know it's fake password

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests