Load a library (and access a 32-bit library from 64-bit Python)
Project description
MSL-LoadLib
This package loads a library in Python. It is basically just a thin wrapper around ctypes (for libraries that use the __cdecl
or __stdcall
calling convention), Python.NET (for libraries that use Microsoft .NET, CLR
), Py4J (for Java .jar
or .class
files) and comtypes (for libraries that use the Component Object Model or ActiveX).
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.NET depends on the .NET Common Language Runtime (CLR) on Windows and Mono Runtime on Linux/macOS, and Py4J depends on having a Java Virtual Machine installed.
Install
msl-loadlib
is available for installation via the Python Package Index
pip install msl-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 replace 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), .so
(Linux) or .dylib
(macOS), is used.
Load the example C++ library and call the add
function
>>> cpp = LoadLibrary(EXAMPLES_DIR / "cpp_lib64")
>>> cpp.lib.add(1, 2)
3
Load the example 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 c_int
we must configure ctypes for a value of type c_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 the example 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 must be closed when you are done using the Java library
>>> java.gateway.shutdown()
Load the example .NET library and call the reverse_string
function, we must specify that the library type is a .NET library by including the "net"
argument
>>> net = LoadLibrary(EXAMPLES_DIR / "dotnet_lib64.dll", "net")
>>> net.lib.StringManipulation().reverse_string("abcdefghijklmnopqrstuvwxyz")
'zyxwvutsrqponmlkjihgfedcba'
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")
We then use the library to create, edit and close a text file
>>> f = com.lib.CreateTextFile("a_new_file.txt")
>>> f.WriteLine("This is a test")
0
>>> f.Close()
0
Inter-process communication is used to access a 32-bit 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 examples for examples on how to implement Inter-process communication.
Documentation
The documentation for msl-loadlib
can be found here.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file msl_loadlib-1.0.0.tar.gz
.
File metadata
- Download URL: msl_loadlib-1.0.0.tar.gz
- Upload date:
- Size: 39.6 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
7874e5a1aa5f894c4d44c26735e991d3e96d86bc5bf00bb83afa13beee2a2d96
|
|
MD5 |
d4a513d675f99b8a36b43fecd3595d63
|
|
BLAKE2b-256 |
95cad9c472576fee4a51e978b89ac1471bf63625c7cba59483a848094d435c3c
|
File details
Details for the file msl_loadlib-1.0.0-py3-none-win_amd64.whl
.
File metadata
- Download URL: msl_loadlib-1.0.0-py3-none-win_amd64.whl
- Upload date:
- Size: 12.7 MB
- Tags: Python 3, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
dc19e8b34b3816e24ee6b3a62f38a938c495ce1f48f2ea7fd475b9a77e1cbcfe
|
|
MD5 |
89c60bfd7a8c6395df170c3946074e7a
|
|
BLAKE2b-256 |
becc8059768f00ae67344e1190517ae48a369e8b163764c20fbb93801b704e25
|
File details
Details for the file msl_loadlib-1.0.0-py3-none-win32.whl
.
File metadata
- Download URL: msl_loadlib-1.0.0-py3-none-win32.whl
- Upload date:
- Size: 12.7 MB
- Tags: Python 3, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
a6244eab6e5e12943aa8eb80ce746274220d80f7688ac165fadeed6a39efeaed
|
|
MD5 |
0a2465ecbd1ef30bbb9845bc0a1e791a
|
|
BLAKE2b-256 |
19d2da53bf011349923f0d68e5b2eb592961ed6f9f61f3467065fbdc2f048238
|
File details
Details for the file msl_loadlib-1.0.0-py3-none-musllinux_1_1_x86_64.whl
.
File metadata
- Download URL: msl_loadlib-1.0.0-py3-none-musllinux_1_1_x86_64.whl
- Upload date:
- Size: 76.0 kB
- Tags: Python 3, musllinux: musl 1.1+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
7561da866e4aedb87bc9a093dc585cb47ad0db2f2e5bb19a6d3a047e8e307bb3
|
|
MD5 |
14735fa8e67b6bdab3868f0d15b749ed
|
|
BLAKE2b-256 |
dae700de424d1ac966b4f8b22c539aa0b215c383cd25288935f507318f099672
|
File details
Details for the file msl_loadlib-1.0.0-py3-none-musllinux_1_1_aarch64.whl
.
File metadata
- Download URL: msl_loadlib-1.0.0-py3-none-musllinux_1_1_aarch64.whl
- Upload date:
- Size: 76.0 kB
- Tags: Python 3, musllinux: musl 1.1+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
e2dfd7f21ca7eb09a4e5367ac5bab90dab8f42600b159a26843e77af8c0dafb0
|
|
MD5 |
999f9760a3547af14b44644f1112d696
|
|
BLAKE2b-256 |
e631500d873efc1b93effc73c51cc0ac7c49f3775dce2f57318a3169383a7bbf
|
File details
Details for the file msl_loadlib-1.0.0-py3-none-manylinux1_x86_64.whl
.
File metadata
- Download URL: msl_loadlib-1.0.0-py3-none-manylinux1_x86_64.whl
- Upload date:
- Size: 26.8 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
4b32bbf6591fc2e6e8a16cb8fd02b9885fa65720e3ec6035cee0a9dab2f39e27
|
|
MD5 |
90f625d513fd806e28ee45fc95cdc1e0
|
|
BLAKE2b-256 |
7de8531ab979238eef67fee37ffc17937ce28c60680ec490a42c936c5f8926dc
|
File details
Details for the file msl_loadlib-1.0.0-py3-none-manylinux1_i686.whl
.
File metadata
- Download URL: msl_loadlib-1.0.0-py3-none-manylinux1_i686.whl
- Upload date:
- Size: 26.8 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
8b634e96de917d026266266158cfc0224979aa29c4105384488586d5dd7931d4
|
|
MD5 |
777af760dab60502746f457d83f51106
|
|
BLAKE2b-256 |
ee268606fa371cb40ccc1d0625e64ed56dc42a7c194e4923f162a7a94ebc5137
|
File details
Details for the file msl_loadlib-1.0.0-py3-none-macosx_11_0_arm64.whl
.
File metadata
- Download URL: msl_loadlib-1.0.0-py3-none-macosx_11_0_arm64.whl
- Upload date:
- Size: 81.3 kB
- Tags: Python 3, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
8dc4492b9e7d0d03af2931d748cf49c6bdcbb9517e9223f7d6798b9c8f581cc2
|
|
MD5 |
61224c0cd67f0c4375cf5581e9f11b1c
|
|
BLAKE2b-256 |
1eba4e52c41c921adaa2f25dcd9073f4a858914c78b3f107bb6befc37d7e3cee
|
File details
Details for the file msl_loadlib-1.0.0-py3-none-macosx_10_6_x86_64.whl
.
File metadata
- Download URL: msl_loadlib-1.0.0-py3-none-macosx_10_6_x86_64.whl
- Upload date:
- Size: 272.8 kB
- Tags: Python 3, macOS 10.6+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
42c9f48dc2fd2345cda1c42563915bfd4c6eec7cd4f3d6da5f211e0e9436ef1f
|
|
MD5 |
2eaca2236d2c123c9c757b7903bf65be
|
|
BLAKE2b-256 |
5b4ca672095da239516d985b7a3f6f22c84b95e6280251ef541facfcc8b42227
|