views:

77

answers:

5

Hi, is it possible to import modules from .lib library to Python program (as simple as .dll)?

A: 

Unfortunately, no. Dynamic Link Libraries are required for runtime loading.

Rakis
A: 

Look into the Python ctypes modules which allows you to load dll when you need them and access functions.

Look here for a tutorial

Martin Thomas
Why was this answer voted down? The OP asked about DLLs and the answer is relevant. Please do not down-vote without explanation.
Martin Thomas
I didn't down-vote but I asked for .lib - not .dll
Carolus89
A: 

Do you have access to the source code? Or at least a header file? If you do, then you could either create a shared library or a Python extension which links to the library. Since you mentioned DLLs, I'll assume you're working on Windows. This tutorial may be useful.

Michael Mior
+1  A: 

In theory, yes; in practice, probably not -- and certainly not as simply as a DLL. Static libraries are essentially just collections of object files, and need a full linker to correctly resolve all relocation references they may contain. It might be possible to take your static library and simply link its contents to form a shared library, but that would require that the static library had been built as position independent code (PIC), which is not guaranteed. In theory there's no reason the work a full linker would do to link the library couldn't be done at runtime, but in practice there's no off-the-shelf code for doing so. Your best real option is probably to track down the source or a shared version of the library.

llasram
A: 

Do you have a static library or do you have a .lib file and are assuming that it is a static library? On Windows, a .lib library can be an import library or a static library. An import library is created alongside the dll of the same name (eg kernel32.dll and kernel32.lib). It is used at link time to populate the import address table of the executable. A static library contains code that will be copied into the executable at link time.

If you have access to a compiler, another option may be to create an extension module that makes use of the static library. For more details see the Python docs

Martin Thomas