How to use cutom python library? - python

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"

Related

How to make protoc generated python stub file to include package name in import

I use the protoc tool, from grpc-tools package (ver. 1.43.0) with python 3.8.10 to generate the data types and client and server stubs for my proto3 service. The generated file containing the stubs (ending in '_grpc.py') imports the generated file containing the message data types using an import statement with no package name: 'import <my_service_name>_pb2.py'. If I understand correctly, in order for this import to work properly in python 3 when the generated code is being included in a package, this import needs to include the name of the package containing it: 'from <my_containing_package> import <my_service_name>_pb2.py'. Is there a way to make protoc generate this import with the package name I require?
P.S. I'm currently using sed to post edit the generated file to add the package name, so only interested, here, in a way to make protoc generate the correct form for python 3 in the first place.
TL; DR; use https://github.com/cpcloud/protoletariat.
From that tool, you can follow the discussion on the protocolbuffers issue,
which basically clarifies that the intended use is to pump all your stubs
in a flat source directory.

Ctype - Could not find DLL dependencies

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?

Issue with Linking Python C Extension to Another Library

I am in the process of developing a Python extension to allow a script running on a Raspberry Pi to control a sensor. The sensor manufacturer provided my organization with the source code to their C API, and I am trying to create a set of bindings to make the sensor accessible in Python scrips.
The makefile that came with the API source created a set of object files that I then linked together into a library (libvl53l1.a) using the command:
ar -cvq libvl53l1.a *.o
I then added this library to the setup.py script o my extension by adding this flag:
extra_compile_args=["-l:libvl53l1.a"]
The code, library, and setup.py script are currently in the same directory for convenience. Installing the library into Python using the command (python3 setup.py build_ext --inplace) runs without errors, however, when I try to import my library in a Python interpreter, the import fails because of an undefined symbol "VL53L1_WaitDeviceBooted" in the extension's .so file. Listing the symbols in libvl54l1.a:
nm libvl53l1.a | grep "VL53L1_WaitDeviceBooted"
shows that the library does expose a symbol of this name. Therefore, I believe that the linker is failing to link the extension with this static library. Is there a step I am missing that is causing this? I have also tried removing the .a extension as recommended in the Python documentation, to no avail.
Thank you
extra_compile_args=["-l:libvl53l1.a"]
This setting adds -l:... to the compilation command, but the compiler ignores that option, because it's a linking option, and no linking is done by the compiler.
You want: extra_link_args=["-lvl53l1"], which would add -lvl53l1 to the link command (the linker wouldn't ignore that option while performing the linking).

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

Error on connecting to python libraries

I have a project that has code which will communicate with a python script and call python functions. In the proj file, i've added the includePath for the python header files, and added external python library to the project (python27.a). However the qt compiler gave me an error:
No rule to make target
/home/pgeng/work/OpenModelica-rev.9876.CSA/OMEdit/OMEditGUI/../../../../../../usr/lib/python2.7/libpython2.a
, needed by ../bin/OMEdit. Stop
What does this mean? How can i fix it? this is anything related to PyQt?
It seems like libpython2 is missing.
You will have to :
Find out what package provides this library.
You can Google for that.
Or search a repo for the lib.

Categories

Resources