Ctype - Could not find DLL dependencies - python

I am trying to use already existing C library "IMSApiCLib.dll" to communicate with external program.
Problem is that this library is using some Java sub-libraries. Before using this library I need to set "Java" path to correct (actual) folder (tested with Tester.exe provided with DLL)
So I have BAT file which prepare it and run Python script itself
cd C:\API\imsapi-clib-9.10.00-7-windows64\
set PATH=.;.\java\jre\bin;.\java\jre\bin\server;%PATH%
python C:\API\imsapi-clib-9.10.00-7-windows64\Connection.py
Connection.py:
DllPath = r"C:\API\imsapi-clib-9.10.00-7-windows64\IMSApiCLib.dll"
imsapi_clib= ctypes.windll.LoadLibrary(DllPath)
FileNotFoundError: Could not find module
'C:\API\imsapi-clib-9.10.00-7-windows64\IMSApiCLib.dll' (or one of
its dependencies). Try using the full path with constructor syntax.
It seems that added new JAVA path is not used when using ctypes.windll.LoadLibrary()
Any tips?

Related

Can't import .so using ctypes in Python

Problem
I am trying to link C++ with python, so I compiled a .so file via g++ (the MinGW version, so I believe it should be able to run on Windows) and then in python:
import ctypes
lib = ctypes.cdll.LoadLibrary("support.so")
I get the Error:
FileNotFoundError: Could not find module 'support.so' (or one of its dependencies). Try using the full path with constructor syntax.
I tried using the full path and using os.add_dll_directory(), but didn't help.
Environment
Python 3.8 / 3.9 both not working
The .so file is created by myself, it is compiled via g++.
I encourtered this problem first on Windows 11, and solved it by changing the winmode in ctypes.__init__() by the help of this answer https://stackoverflow.com/a/64472088/9249608.
However, when I copy this program and the .so file to another computer with Window 10, I cannot solve this problem by any of the above methods. I even tried adding the folder to the PATH or copying this .so file to the python folder, but it still produces the error.
Is there any way that I can know exactly which paths are being searched? I see that there are many searching modes in ctypes.__init__() but I don't know how to output the corresponding searching paths.

Azure ML Python with Script Bundle cannot import module

In Azure ML, I'm trying to execute a Python module that needs to import the module pyxdameraulevenshtein (https://pypi.python.org/pypi/pyxDamerauLevenshtein).
I followed the usual way, which is to create a zip file and then import it; however for this specific module, it seems to never be able to find it. The error message is as usual:
ImportError: No module named 'pyxdameraulevenshtein'
Has anyone included this pyxdameraulevenshtein module in Azure ML with success ?
(I took the package from https://pypi.python.org/pypi/pyxDamerauLevenshtein.)
Thanks for any help you can provide,
PH
I viewed the pyxdameraulevenshtein module page, there are two packages you can download which include a wheel file for MacOS and a source code tar file. I don't think you can directly use the both on Azure ML, because the MacOS one is just a share library .so file for darwin which is not compatible with Azure ML, and the other you need to first compile it.
So my suggestion is as below for using pyxdameraulevenshtein.
First, compile the source code of pyxdameraulevenshtein to a DLL file on Windows, please refer to the document for Python 2/3 or search for doing this.
Write a Python script using the DLL you compiled to implement your needs, please refer to the SO thread How can I use a DLL file from Python? for how to use DLL from Python and refer to the Azure offical tutorial to write your Python script
Package your Python script and DLL file as a zip file, then to upload the zip file to use it in Execute Python script model of Azure ML.
Hope it helps.
Adding the path to pyxdameraulevenshtein to your system path should alleviate this issue. The script checks the system path that the python script is running on and doesn't know where else to look for anything other than the default packages. If your python script is in the same directory as the pyxdameraulevenshtein package in your ZIP file, this should do the trick. Because you are running this within Azure ML and can't be sure of the exact location of your script each time you run it, this solution should account for that.
import os
import sys
sys.path.append(os.path.join(os.getcwd(), 'pyxdameraulevenshtein'))
import pyxdameraulevenshtein

Using libarchive in python on Windows

I'm trying to get libarchive module working in python 3.4 on windows.
I've installed libarchive-c with pip and all went ok but whenever I try to import it into my code or even to run it alone I'me getting error:
OSError: [WinError 126] The specified module could not be found
This is coming from ffi.py from the code below:
libarchive_path = os.environ.get('LIBARCHIVE') or find_library('archive')
libarchive = ctypes.cdll.LoadLibrary(libarchive_path)
I've never used ctypes before but if I understand correctly it is looking for external DLL. So found and installed http://gnuwin32.sourceforge.net/packages/libarchive.htm also I've added C:\Program Files (x86)\GnuWin32\bin to my %PATH% in environmental variables but it still cannot load the module. As it does not give me the name I'm not sure what module it is looking for.
What am I missing?
(disclaimer) I contribute to https://github.com/Changaco/python-libarchive-c and I maintain https://github.com/nexB/scancode-toolkit
Both contain a ctypes binding for libarchive, though ScanCode is for extraction only.
My answer here is for python-libarchive-c, but ScanCode contains some of the DLL you are looking for so I am lacing in a bit of both.
To get python-libarchive-c going on Windows you need a libarchive DLL and its deps that can then be loaded.
There are no pre-built DLLs bundled in python-libarchive-c but I have prebuilt Windows binaries for another project here:
https://github.com/nexB/scancode-toolkit/tree/develop/src/extractcode/bin/win-32/bin
The corresponding source code is there: https://github.com/nexB/scancode-thirdparty-src
And you have MinGW32 build instructions there if you want to rebuild from source yourself: https://github.com/nexB/scancode-thirdparty-src/blob/master/libarchive/build.sh#L47
In general to load a DLL from a path -- assuming that the var libarchive contains the full path to that DLL -- use this:
lib = ctypes.CDLL(libarchive)
Now this is for Scancode. For python-libarchive-c, you could try to set the LIBARCHIVE variable to point the path of your DLL with:
set LIBARCHIVE="C:\.....\libarchive.dll"
Then start Python, import the library and use it.
NB: I did not test this (yet) , but this should work. IF not please file a bug.
I did not run any test on Python 3.4 either. I use primarily Python 2.7.
But the DLL and the code is not Python 2.7-specific at all.
FWIW, the way scancode loads the library is a tad more engaged since it can from the same code load DLLs Win/Linux/Mac for specific 32 or 64 bits archs using conventional locations. You can see the code in action there:
https://github.com/nexB/scancode-toolkit/blob/develop/src/extractcode/libarchive2.py#L64
ScanCode is NOT using python-libarchive-c ATM yet but a different/custom ctypes binding focused on a more specific use case of extraction only. At least it gives you access to a Win DLL and its deps (or instruction a build them) and an example on how to load it correctly.
/HTH

Linking library in python

I want to use the Cantera library in python. I have been using it for C++ and I am linking my adding these couple lines to my makefile:
CANT_LIB = $HOME/usr/local/Cantera201/lib/
CANT_INC = $HOME/usr/local/Cantera201/include/ -I $HOME/usr/local/Cantera201/include/cantera \
with CANT_LIB and CANT_INC being called when compiling.
I have very limited experience with python. Is there an equivalent to linking libraries in python? I have tried adding the cantera path to PYTHONPATH but it did not work. I am working on a Linux server on which I do not have access to super user and python 2.6.6.
You need to install Cantera's Python module to use it, the raw C/C++ libraries aren't enough. If you install using the directions on their website it should be installed to the appropriate Python site-packages directory automatically, and available for use with just import cantera.

How to use cutom python library?

I have generated a custom library for python using Swig and i want to use that library somewhere else (with out the source files) , Should i copy the .so file to that place ? or is there any other way.
Using Swig it has generated one so file(say _example.so) now if i want to use that library in that particular folder i need to do import example but if i am trying the same in any other folder it is throwing error saying 'Import Error: no module named example'.
Normally you should have generated _example.so and example.py. You need to distribute both. If you are concerned about exposing the sources - do not worry, example.py contains only assets translating python code to calls to the shared library.
Try this before running your command:
export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:/path/to/directory/with/so/file"

Categories

Resources