Skip to content
Advertisement

Why DLL is not imported in the python script as the other libraries? [closed]

I know that DLL is a library that has methods, classes,… As far as I saw to use its contents, it is loaded in python but not imported as other libraries like numpy for example. Does that mean it cannot be imported and must be loaded? if this is the case, why?

Advertisement

Answer

A DLL is something inherently different from a python module (such as numpy). A python module is just a file containing python code, that can be used if the corresponding module is imported. If you type

import numpy

then a python file somewhere in your python installation directory is executed and the defined functions, classes and objects can be used in the calling python progam.

A DLL is a shared library containing compiled functions and objects. DLLs are often created by compiling C or C++ code into a shared library, but can also be built from other programming languages. A python interpreter cannot just read and execute them, because there is no pyhton code in there. Thats the reason why you can’t import DLLs with the import statement.

That does however not mean, that importing DLL functions is impossible in python. The python standard library provides the ctypes module, which allows you to load a DLL at runtime and call functions in it. However, because the DLL does know nothing about python objects in general, there are a lot of type conversions in the ctypes module, that you must use to convert the python variables to C data types if you want to pass them to functions in the DLL. More information about this can be found in the python standard library documentation.

Note that for example the python module numpy also probably loads a DLL under the hood. So just becaus you import a python module, that does not mean that there are no DLLs loaded. In fact it is common for libraries written in C or C++ to be compiled into a DLL and then also provide a thin layer of python code (that can be imported as python module) to load that DLL, do the type conversion and call the functions in it. The methods and classes that you mentioned in the question are most likely from such an interface module.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement