Skip to main content

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

Project description

Documentation Status pypi

This package is used to load 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) and Py4J (for Java .jar or .class files). However, the primary advantage is that it is possible to communicate with a 32-bit shared library from 64-bit Python.

Tested in Python 2.7, 3.4+. The examples provided are currently only supported in Windows and Linux.

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 OSX 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()

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.4.1.tar.gz (893.9 kB view details)

Uploaded Source

Built Distributions

msl_loadlib-0.4.1-py2.py3-none-win_amd64.whl (8.2 MB view details)

Uploaded Python 2 Python 3 Windows x86-64

msl_loadlib-0.4.1-py2.py3-none-manylinux1_x86_64.whl (11.9 MB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: msl-loadlib-0.4.1.tar.gz
  • Upload date:
  • Size: 893.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.5.5

File hashes

Hashes for msl-loadlib-0.4.1.tar.gz
Algorithm Hash digest
SHA256 eabaaa66874b8ba52a289f919162708a664a13581a7c5bef00808be88a255fa3
MD5 d4a7fa0dbff238dafac35eabdd150fcf
BLAKE2b-256 15571b4472ce14c9439866562a2cba4b6f3bb00c521953ab1b7016104dc6da93

See more details on using hashes here.

File details

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

File metadata

  • Download URL: msl_loadlib-0.4.1-py2.py3-none-win_amd64.whl
  • Upload date:
  • Size: 8.2 MB
  • Tags: Python 2, Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.5.5

File hashes

Hashes for msl_loadlib-0.4.1-py2.py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 2b6c77d7100e79f335ffa2dbbbc9d77fbe81f75099bbc92796edf4ec355c38fe
MD5 42fb4ef73525ecaf8206cd950a21e12a
BLAKE2b-256 ce9317e416368f69bcd9112e32981f888f5fd15102d9cd26fe0fd11013e26c43

See more details on using hashes here.

File details

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

File metadata

  • Download URL: msl_loadlib-0.4.1-py2.py3-none-manylinux1_x86_64.whl
  • Upload date:
  • Size: 11.9 MB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.5

File hashes

Hashes for msl_loadlib-0.4.1-py2.py3-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 1894e4f5523074a7d1e3e569fcf3877046dc86de46b5688e4615688d7ac12420
MD5 6ef21426394489fd60692dd8a044656b
BLAKE2b-256 214408c8a1d263f5a1cdc30e9c5f0a38a79a128fe717c1f6bd790bd8a92c2e72

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