Skip to main content

Load a shared library (and access a 32-bit library from 64-bit Python)

Project description

Documentation Status pypi travis appveyor

This package loads a shared library in Python. It is basically just a thin wrapper around ctypes (for libraries that use the __cdecl or __stdcall calling convention), Python for .NET (for libraries that use Microsoft’s .NET Framework, CLR), Py4J (for Java .jar or .class files) and comtypes (for libraries that use the Component Object Model).

However, the primary advantage is that it is possible to communicate with a 32-bit library from 64-bit Python.

MSL-LoadLib is a pure-python package, but, Python for .NET depends on the .NET Common Language Runtime (CLR) on Windows and Mono Runtime on Linux and macOS and Py4J depends on having a Java Virtual Machine installed.

Install

To install MSL-LoadLib run:

pip install msl-loadlib

Alternatively, using the MSL Package Manager run:

msl install loadlib

Optional dependencies:

To set up your environment on Linux, please follow the instructions on the prerequisites section of the documentation.

Examples

If you are loading a 64-bit library in 64-bit Python (or a 32-bit library in 32-bit Python), then you can directly load the library using LoadLibrary.

The following examples load a 64-bit library in a 64-bit Python interpreter. If you are using a 32-bit Python interpreter then replace the 64 with 32 in the filename.

Import the LoadLibrary class and the directory where the example libraries are located

>>> from msl.loadlib import LoadLibrary
>>> from msl.examples.loadlib import EXAMPLES_DIR

If the file extension is not included then a default extension, .dll (Windows) or .so (Linux), is used.

Load a C++ library and call the add function

>>> cpp = LoadLibrary(EXAMPLES_DIR + '/cpp_lib64')
>>> cpp.lib.add(1, 2)
3

Load a FORTRAN library and call the factorial function

>>> fortran = LoadLibrary(EXAMPLES_DIR + '/fortran_lib64')

With a FORTRAN library you must pass values by reference using ctypes, and, since the returned value is not of type int we must configure ctypes for a value of type double to be returned

>>> from ctypes import byref, c_int, c_double
>>> fortran.lib.factorial.restype = c_double
>>> fortran.lib.factorial(byref(c_int(37)))
1.3763753091226343e+43

Load a .NET library and call the reverse_string function, we must specify that the library type is a .NET library by passing in the 'net' argument

>>> net = LoadLibrary(EXAMPLES_DIR + '/dotnet_lib64.dll', 'net')
>>> net.lib.StringManipulation().reverse_string('abcdefghijklmnopqrstuvwxyz')
'zyxwvutsrqponmlkjihgfedcba'

Load Java byte code and call the cos function

>>> java = LoadLibrary(EXAMPLES_DIR + '/Trig.class')
>>> java.lib.Trig.cos(1.234)
0.33046510807172985

Python interacts with the Java Virtual Machine via a local network socket and therefore the connection needs to be closed when you are done using the Java library

>>> java.gateway.shutdown()

To load a Component Object Model (COM) library pass in the library’s Program ID. NOTE: This example will only work on Windows.

Here we load the FileSystemObject library and include the 'com' argument to indicate that it is a COM library.

>>> com = LoadLibrary('Scripting.FileSystemObject', 'com')

If creating the com object in the previous command raises

OSError: [WinError -2147417850] Cannot change thread mode after it is set

then perform the following and try again

>>> import sys
>>> sys.coinit_flags = 0

We then use the library to create, edit and close a text file

>>> fp = com.lib.CreateTextFile('a_new_file.txt')
>>> fp.WriteLine('This is a test')
0
>>> fp.Close()
0

Inter-process communication is used to access a 32-bit shared library from a module that is running within a 64-bit Python interpreter. The procedure uses a client-server protocol where the client is a subclass of msl.loadlib.Client64 and the server is a subclass of msl.loadlib.Server32. See the tutorials for examples on how to implement inter-process communication.

Documentation

The documentation for MSL-LoadLib can be found here.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

msl-loadlib-0.8.0.tar.gz (1.1 MB view details)

Uploaded Source

Built Distributions

msl_loadlib-0.8.0-py2.py3-none-win_amd64.whl (7.7 MB view details)

Uploaded Python 2 Python 3 Windows x86-64

msl_loadlib-0.8.0-py2.py3-none-win32.whl (7.7 MB view details)

Uploaded Python 2 Python 3 Windows x86

msl_loadlib-0.8.0-py2.py3-none-manylinux1_x86_64.whl (15.9 MB view details)

Uploaded Python 2 Python 3

msl_loadlib-0.8.0-py2.py3-none-manylinux1_i686.whl (16.0 MB view details)

Uploaded Python 2 Python 3

File details

Details for the file msl-loadlib-0.8.0.tar.gz.

File metadata

  • Download URL: msl-loadlib-0.8.0.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.5

File hashes

Hashes for msl-loadlib-0.8.0.tar.gz
Algorithm Hash digest
SHA256 d8da144578813ed34ccee2dfc63293a9c27bff456c7646fc789524d4fd1154f7
MD5 42f4065e33cb0271adac6225e54a1a2f
BLAKE2b-256 817d03eae7e047345b330badf29bc5e1bfec9fcb38baa5d97ccf7025bf48f533

See more details on using hashes here.

File details

Details for the file msl_loadlib-0.8.0-py2.py3-none-win_amd64.whl.

File metadata

  • Download URL: msl_loadlib-0.8.0-py2.py3-none-win_amd64.whl
  • Upload date:
  • Size: 7.7 MB
  • Tags: Python 2, Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.5

File hashes

Hashes for msl_loadlib-0.8.0-py2.py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 db918de97cd47040149223eed0b6c050705021e0735ab74ab0bb419e9acc0042
MD5 3b8eb4975d0994bb0bb02fa42e56ae1b
BLAKE2b-256 6277e3c24c2bba5767cfeb5ee09b7bb1dba87098e8ca78a797d6dfd09e63d813

See more details on using hashes here.

File details

Details for the file msl_loadlib-0.8.0-py2.py3-none-win32.whl.

File metadata

  • Download URL: msl_loadlib-0.8.0-py2.py3-none-win32.whl
  • Upload date:
  • Size: 7.7 MB
  • Tags: Python 2, Python 3, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.5

File hashes

Hashes for msl_loadlib-0.8.0-py2.py3-none-win32.whl
Algorithm Hash digest
SHA256 898265a4d73d58593e79a379123cd1bf972633530fcc09c9b27a5f74a8508489
MD5 d593f54866157b8aba93dd023c87b340
BLAKE2b-256 a067672e122b4ba875e9c0219b88dcd050b713853965a4cbe1f39a276f636217

See more details on using hashes here.

File details

Details for the file msl_loadlib-0.8.0-py2.py3-none-manylinux1_x86_64.whl.

File metadata

  • Download URL: msl_loadlib-0.8.0-py2.py3-none-manylinux1_x86_64.whl
  • Upload date:
  • Size: 15.9 MB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.5

File hashes

Hashes for msl_loadlib-0.8.0-py2.py3-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 14cdd65e2682715d8d51b8fd9870340834a646065775885f3f4dedd909c2c6c9
MD5 87f2dbce902493bb084920d2751aee1c
BLAKE2b-256 334987747205d376b0c5064a230a5a140278c1c52af5da62f00b45b197de03c9

See more details on using hashes here.

File details

Details for the file msl_loadlib-0.8.0-py2.py3-none-manylinux1_i686.whl.

File metadata

  • Download URL: msl_loadlib-0.8.0-py2.py3-none-manylinux1_i686.whl
  • Upload date:
  • Size: 16.0 MB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.5

File hashes

Hashes for msl_loadlib-0.8.0-py2.py3-none-manylinux1_i686.whl
Algorithm Hash digest
SHA256 8d0a050b4a11ecfc7ee997fc4ee787e3991c239cd38973eb9a2d21b73e3be3e3
MD5 f83089815492a8b439e0e25c2c0bc193
BLAKE2b-256 1cf9ac2af796887bc0ec995bfc5b929bb229fb74ec7c3a20346cf5217e1db603

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page