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 · 19 comments
Open

ImportError: DLL load failed while importing ibm_db: #887

ZephyrusMB opened this issue Sep 6, 2023 · 19 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.

@ditarnnt
Copy link

ditarnnt commented Jan 8, 2024

this is really helpfull, thanks

@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?

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