I'm writing a Python-based [web] application that needs to be able to read and write EXIF data.
libexif seems to have all the right ingredients, but I can't work out how (or if) I could access it access it by using Python's ctypes library? I'm new to C, suppose I need see a .so for this to work?
You need to be running on an os that you can obtain the required library, to download the .h files, (usually the -dev package gives you these).
Then you need to work your way through the ctypes tutorial found here which explains all the steps you need to take.
Related
Some important background upfront, I am using a computer that does not give me access to pip. In fact I do not have access to the command prompt. This make is it impossible for me to install additional libraries unfortunately (at least the standard way).
My question is whether I can run a python library without formally installing it. Could I download the library, and then store it the same directory as my main script, and then import it like I would with a multi .py script project with functions being defined in other files, almost as if I had written the script natively on my computer?
Specifically, I would like to use pdfminer.six. Apparently it is written completely in python, however, I realize that may not mean what I think it does. It may be similar to numpy which I understand has C++ code associated with it.
You can import any script or lib from your current folder (example). You can find any lib you want by googling 'lib_name github'. Download the zip and unpack it in your folder, it should work.
You can also go to your python Lib folder on another computer and copy libs from there (By default: C:\Users\User\AppData\Local\Programs\Python\Python310\Lib)
Maybe you can use a web-based-interpreter solution like Google Colab and work in your browser.
https://colab.research.google.com
So I have a dynamic library file named "libxlearn_api.dylib". I can load this library from within python code by
lib = ctypes.cdll.LoadLibrary(/path/to/lib)
I can also see what functions are defined in the above library by dir(lib).
But, this just gives the list of names of the functions in it.
I need to view the description/definition of these functions(how they are written/implemented, i.e the actual code.)
How can I do this?
I need to view the description/definition of these functions(how they are written/implemented, i.e the actual code.)
In general, you cannot, because the dynamic library might be proprietary (this is the case for many GUI related libraries on MacOSX, read about Aqua).
If the library is free software, or if by some (legal) means you've got its source code (perhaps after signing some NDA and having paid some money), you might study its source code. Then you'll better build that library from source code (to be sure that the binary library and the source code matches), otherwise you need to trust the provider of the binary library that it corresponds exactly to the source code you've got.
BTW, xlearn is free software. Perhaps it is the same as your binary dynamic library (but you need to check). I recommend to be able to build it from its source code (in a terminal, using commands). See its installation guide, section: installing from source code.
I'm writing some sort of Python C extension. It uses my own *.so library and headers from another project (let's say they're in /usr/local/lib/otherproject.so and /usr/local/include/otherproject.h).
I don't know which strategy to follow. I came up with two:
As a pure Python extension
Write a Python C extension just as described in the official docs. The problem here is that I don't know how to link with my own library and headers; to compile, I write a setup.py file and run python3.4 setup.py build. I don't know if I can include some option to the former command, or if I can write something in setup.py to include my headers and binaries (if so, I will also have to worry about making this distributable?).
With ctypes
Write a C library (with my other project's build system). Include Python by passing '/usr/include/python2.7' to find headers and the python2.7 binary. Then use ctypes to wrap around that library and get the functions, types, objects, etc. The inconvenience here is that I need to manually wrap around every single function/type/variable from ctypes; I don't think I can use PyModule_AddObject since I'm not creating the module in C but in the Python wrapper (with ctypes).
Also, I tried the second approach, but I could not successfully get my custom PyTypeObject from ctypes. If the second approach sounds good to any more expert brain here on SO, I would post the code to get any help =).
The second approach also yields problems with distribution. And if you create a Python object in C you should do it in the context of a module. For scenarios where distribution is problematic, you could link this module statically instead.
For your issue with linking you'll find more information about Library options in the documentation. Since your library resides in a directory which should be in the standard library search path, you'd only need to define your library with the libraries option of the Extension class:
mymodule_ext = Extension('mymodule', ['mymodule.c'], libraries=['otherproject'])
If you're not using the standard lib* prefix you'd need to use libraries=[':otherproject.so'] instead.
I have an executable (face recognition) written by one of our programmers who's no longer with us. It takes a commandline argument and prints the result to STDOUT like this.
I need to be able to use this in Python but performance is important so I'm not sure if I should just use subprocess. I need to capture the output of the program so is it a better idea to modify the code to make it a C++ library instead and write a wrapper for it in Python? How would that impact performance compared to using subprocess?
I have looked into the Python docs for running C code and found this but the answer here is different. The second method does not even use Python.h so I'm a little confused. What's the best way to do what I need when performance is important?
At first I have to tell you I think subprocess method and running external application and reading output from STD output would be slow and unreliable. I won't recommend that method.
Instead, here is some suggestions:
You can create a Python extension in C or C++, you can find a lot of resources online about it. So you can convert your current code to Python extension format and just use Python extension skeleton template, integrate your functions and code in it and you'll have a python extension, simply you'll be able to call it's functions from Python code. See: http://docs.python.org/2/extending/index.html
Convert your current C++ code directly into a DLL and exports functions of your code you want to call from python if you are in Windows or simply convert it to a shared library if you are using Linux. It will be compiled to .so . Then you can call functions as an external library in your Python code. Example with explanation: http://www.linuxforu.com/2010/05/extending-python-via-shared-libraries/
I've never tried them, but it seems these projects can help you with what you want:
http://sourceforge.net/projects/pygccxml/?source=dlp
https://code.google.com/p/pybindgen/
http://www.cython.org/
My Delphi program uses Python4Delphi, and the Python script uses some standard libs. When deploying my program I don't want an entire Python installation, and it must work with python27.dll. What are the minimal set of necessary files? The document in Python4Delphi is dated and it's not clear to me...
Thanks for your help.
When I did this, I made the list myself, of what I needed for my embedded python application to work.
I remember this worked with python15.dll:
PythonXX.dll should work, without any other external files other than the Visual C++ Runtime DLLs, which require a side-by-side manifest (see the p4d wiki page) to work.
If you want to IMPORT something, then you need to ship it and anything it depends on. That means, either you pick part of the python standard libraries you want, or you pick all of it. There is no way you need all of Python's standard libraries. But I wouldn't want to live without OS and a a few other key ones. BUt the decision is yours.