My friend has an application written in C that comes with a GUI made using GTK under Linux. Now we want to rewrite the GUI in python (wxpython or PyQT).
I don't have experience with Python and don't know how to make Python communicate with C. I'd like to know if this is possible and if yes, how should I go about implementing it?
Yes its possible to call 'C' functions from Python.
Please look into SWIG(deprecated) also Python provides its own Extensibility API. You might want to look into that.
Also google CTypes.
LINKS:
Python Extension
A simple example:
I used Cygwin on Windows for this. My python version on this machine is 2.6.8 - tested it with test.py loading the module called "myext.dll" - it works fine. You might want to modify the Makefile to make it work on your machine.
original.h
#ifndef _ORIGINAL_H_
#define _ORIGINAL_H_
int _original_print(const char *data);
#endif /*_ORIGINAL_H_*/
original.c
#include <stdio.h>
#include "original.h"
int _original_print(const char *data)
{
return printf("o: %s",data);
}
stub.c
#include <Python.h>
#include "original.h"
static PyObject *myext_print(PyObject *, PyObject *);
static PyMethodDef Methods[] = {
{"printx", myext_print, METH_VARARGS,"Print"},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC initmyext(void)
{
PyObject *m;
m = Py_InitModule("myext",Methods);
}
static PyObject *myext_print(PyObject *self, PyObject *args)
{
const char *data;
int no_chars_printed;
if(!PyArg_ParseTuple(args, "s", &data)){
return NULL;
}
no_chars_printed = _original_print(data);
return Py_BuildValue("i",no_chars_printed);
}
Makefile
PYTHON_INCLUDE = -I/usr/include/python2.6
PYTHON_LIB = -lpython2.6
USER_LIBRARY = -L/usr/lib
GCC = gcc -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -fPIC -DMAJOR_VERSION=1 -DMINOR_VERSION=0 -I/usr/include -I/usr/include/python2.6
win32 : myext.o
- gcc -shared myext.o $(USER_LIBRARY) $(PYTHON_LIB) -o myext.dll
linux : myext.o
- gcc -shared myext.o $(USER_LIBRARY) $(PYTHON_LIB) -o myext.so
myext.o: stub.o original.o
- ld -r stub.o original.o -o myext.o
stub.o: stub.c
- $(GCC) -c stub.c -o stub.o
original.o: original.c
- $(GCC) -c original.c -o original.o
clean: myext.o
- rm stub.o original.o stub.c~ original.c~ Makefile~
test.py
import myext
myext.printx('hello world')
OUTPUT
o: hello world
Sorry but i don't have python experience so don't know how to make Python communicate with C program.
Yes, that's exactly how you do it. Turn your C code into a Python module, and then you can write the entire GUI in Python. See Extending and Embedding the Python Interpreter.
If you have an option between C or C#(Sharp) then go with C# and use visual studio, you can build the GUI by dragging and dropping components easy. If you want to do something in python look up wxPython. Java has a built in GUI builder known as swing. You'll need some tutorials, but unless this program doesn't need to be portable just go with C# and build it in 10 minutes .
Also, you can write your code in C and export it as a python module which you can load from python. It`s not very complicated to set up some C functions and have a python GUI which calls them. To achieve this you can use SWIG, Pyrex, BOOST.
Related
I am trying to compile some C++ Code with OpenCv and Pybind with this header:
https://github.com/patrikhuber/eos/blob/v0.12.2/python/pybind11_opencv.hpp
This has worked for me before, so I don't think the header file is the Problem.
I can compile the code without problems, but when i try to import the created file to Python I get the following error:
ImportError: /usr/lib/libgtk-3.so.0: undefined symbol: g_mount_operation_set_is_tcrypt_hidden_volume
Here is the C++ Code:
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <pybind11/pybind11.h>
#include "pybind11_opencv.hpp"
using namespace std;
namespace py = pybind11;
cv::Mat func(cv::Mat Image1,cv::Mat Image2)
{
return Image1;
}
PYBIND11_MODULE(pybind_module, m)
{
m.doc() = "Text";
m.def("func", &func, "Function",
py::arg("Image1"),
py::arg("Image2"));
}
I am guessing it's a problem with my setup (arch linux) since I got something similar working before and not even this minimal example is working.
I was able to solve the problem myself using the following compiler settings.
c++ -msse4 -O3 -Wall -shared -std=c++11 -fPIC -lopencv_imgcodecs `python3 -m pybind11 --includes` main.cpp -o executable`python3-config --extension-suffix` /usr/local/include/ -L/usr/local/lib/ -march=native
I guess there was a mistake in the previous compilation.
I was looking at here to see how to expose c++ to Python. I have built Python deep learning code which uses boost-python to connect c++ and python and it is running ok, so my system has things for boost-python alread setup.
Here is my hello.cpp code (where I used WorldC and WorldP to clearly show the C++ and Python class name usage in the declaration. I don't know why the original web page is using the same class name World to cause confusion to beginners.)
#include <boost/python.hpp>
using namespace boost::python;
struct WorldC
{
void set(std::string msg) { this->msg = msg; }
std::string greet() { return msg; }
std::string msg;
};
BOOST_PYTHON_MODULE(hello)
{
class_<WorldC>("WorldP")
.def("greet", &WorldC::greet)
.def("set", &WorldC::set)
;
}
and this is how I made hello.so
g++ -shared -c -o hello.so -fPIC hello.cpp -lboostpython -lpython2.7 -I/usr/local/include/python2.7
When I run import hello in python, it gives me this error.
>>> import hello
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: ./hello.so: only ET_DYN and ET_EXEC can be loaded
Can anybody tell me what's wrong?
(I'm using anaconda2 under my home directory for python environment, but since my deep learning code builds ok with boost-python, there should be no problem including boost/python.hpp in my system directory)
I've long forgotten about this problem and got to revisit this issue today.
I found two problems. The first problem was that I gave -c option which made the compiler only compile the source and not link. The second problem was that the library name was wrong(I search /usr/lib64 and there was libboost_python.so, so it should be -lboost_python instead of -lboostpython). So the correct way to build it is :
g++ -shared -o hello.so -fPIC hello.cpp -lboost_python -lpython2.7 -I/usr/local/include/python2.7
I found it runs ok in python. :)
I am having trouble embedding Python in C++. I am using Mingw w64 gcc and 64 bit Python 2.7.11.
#include <Python.h>
int main(int argc, char *argv[]) {
Py_Initialize();
PyObject* pName = PyString_FromString("test");
Py_DECREF(pName);
Py_Finalize();
return 0;
}
Calls to compiler:
g++ "-IC:\\Python27\\include" -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -o "src\\main.o" "..\\src\\main.cpp"
g++ "-LC:\\Python27\\libs" -std=c++11 -o pytest.exe "src\\main.o" -lpython27
The problem is that it segfaults in Py_DECREF. I have tried expanding the macros, and have traced segfault to the following statement:
((*(((PyObject*) ((PyObject *) (pName)))->ob_type)->tp_dealloc)((PyObject *) ((PyObject *) (pName))));
Turns out, tp_dealloc points to 0x1.
The same problem happens in the example code provided in Python docs:
https://docs.python.org/2/extending/embedding.html#pure-embedding
If I remove some of the calls to Py_DECREF(pName) and Py_DECREF(pArgs), code from the docs works as intended. Yet every example I've found on the web (including the one from the Python docs) does call Py_DECREF.
What could be the cause of this error? Could there be some inconsistency in my build environment?
So, apparently, something is wrong with my environment. I tried compiling the same code on another PC and there was no segfault anymore.
I am trying to wrap c++ functions into python using swig. I am using following commands
swig -c++ -python helloworld.i
g++ -O2 -fPIC -c hello.cpp
g++ -O2 -fPIC -c helloworld_wrap.cxx -I//anaconda/include/python2.7
g++ -lpython -dynamclib hello.o helloworld_wrap.o -o _helloworld.so
with hello.cpp being initial file with functions and helloworld.i being file with wrapper. These commands creates the library helloworldbut I can only import it through default python in /usr/bin/python
If I try to import it through python installed through anaconda it gives following error:
Fatal Python error: PyThreadState_Get: no current thread
Abort trap: 6
Can you tell me how can I wrap the codes with python from anaconda?
Found a solution : Python.h not found using swig and Anaconda Python
In above question, the top answer gives an explanation of using disutilsand a set up file in python to build the library. This works wonders :)
The next problem I am having for wrapping simple class:
my class code from [example] (http://web.mit.edu/svn/src/swig-1.3.25/Examples/python/class/index.html)
/* File : example.h */
class Shape {
public:
Shape() {
nshapes++;
}
virtual ~Shape() {
nshapes--;
};
double x, y;
void move(double dx, double dy);
virtual double area() = 0;
virtual double perimeter() = 0;
static int nshapes;
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) { };
virtual double area();
virtual double perimeter();
};
class Square : public Shape {
private:
double width;
public:
Square(double w) : width(w) { };
virtual double area();
virtual double perimeter();
};
My setup.py file :
#setup.py file:
from setuptools import setup, Extension
setup(name='example',
version='0.1',
ext_modules=[Extension('_example', ['example.h', 'example.i'],
swig_opts=['-c++'],
)],
)
Code I am using to wrap :
python setup.py build_ext --inplace
Error message:
running build_ext
building '_example' extension
swigging example.i to example_wrap.cpp
swig -python -c++ -o example_wrap.cpp example.i
error: unknown file type '.h' (from 'example.h')
Can you suggest what is wrong here. I suppose it is not recognizing '.h' file but as it is header file, I thought it could be kept as it is. Also if my setup.py file is correct or not? I am just trying to follow example for simple wrapping, there is no simple tutorial online apparently.
I can also ask this question on other different question but thought just continuing here for now.
Answer by Warren Weckesser in similar question
. I used the setup.py file as suggested in the answer and added the path to the library to sys.path and it work wonders :)
Use the option -I/Users/myuser/anaconda/include/python2.7 in the gcc command. (That's assuming you are using python 2.7. Change the name to match the version of python that you are using.) You can use the command python-config --cflags to get the full set of recommended compilation flags:
$ python-config --cflags
-I/Users/myuser/anaconda/include/python2.7 -I/Users/myuser/anaconda/include/python2.7 -fno-strict-aliasing -I/Users/myuser/anaconda/include -arch x86_64 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes
However, to build the extension module, I recommend using a simple setup script, such as the following setup.py, and let distutils figure out all the compiling and linking options for you.
# setup.py
from distutils.core import setup, Extension
example_module = Extension('_example', sources=['example_wrap.c', 'example.c'])
setup(name='example', ext_modules=[example_module], py_modules=["example"])
Then you can run:
$ swig -python example.i
$ python setup.py build_ext --inplace
(Take a look at the compiler commands that are echoed to the terminal when setup.py is run.)
distutils knows about SWIG, so instead of including example_wrap.c in the list of source files, you can include example.i, and swig will be run automatically by the setup script:
# setup.py
from distutils.core import setup, Extension
example_module = Extension('_example', sources=['example.c', 'example.i'])
setup(name='example', ext_modules=[example_module], py_modules=["example"])
With the above version of setup.py, you can build the extension module with the single command
$ python setup.py build_ext --inplace
Once you've built the extension module, you should be able to use it in python:
>>> import example
>>> example.fact(5)
120
If you'd rather not use the script setup.py, here's a set of commands that worked for me:
$ swig -python example.i
$ gcc -c -I/Users/myuser/anaconda/include/python2.7 example.c example_wrap.c
$ gcc -bundle -undefined dynamic_lookup -L/Users/myuser/anaconda/lib example.o example_wrap.o -o _example.so
Note: I'm using Mac OS X 10.9.4:
$ gcc --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix
I am trying to compile a simple C extension in Mac to use with Python, and all works well in the command line. Code and gcc command that works are presented below.
Now I am trying to build the same extension in Xcode 4.5 (Mac OS10.8), and I tried several target settings for either dylib or static library, but I always get a file that cannot be loaded in Python showing the error:
./myModule.so: unknown file type, first eight bytes: 0x21 0x3C 0x61 0x72 0x63 0x68 0x3E 0x0A
My ultimate target is to create a workspace in XCode with the source code of a C/C++ extension and have python script that calls it in Xcode. So, if I need to debug the C/C++ extension I have XCode debugging capabilities. I am aware that XCode do not debug into Python script, but it can run it, correct ?
gcc -shared -arch i386 -arch x86_64 -L/usr/lib/python2.7 -framework python -I/usr/include/python2.7 -o myModule.so myModule.c -v
#include <Python.h>
/*
* Function to be called from Python
*/
static PyObject* py_myFunction(PyObject* self, PyObject* args)
{
char *s = "Hello from C!";
return Py_BuildValue("s", s);
}
/*
* Another function to be called from Python
*/
static PyObject* py_myOtherFunction(PyObject* self, PyObject* args)
{
double x, y;
PyArg_ParseTuple(args, "dd", &x, &y);
return Py_BuildValue("d", x*y);
}
/*
* Bind Python function names to our C functions
*/
static PyMethodDef myModule_methods[] = {
{"myFunction", py_myFunction, METH_VARARGS},
{"myOtherFunction", py_myOtherFunction, METH_VARARGS},
{NULL, NULL}
};
/*
* Python calls this to let us initialize our module
*/
void initmyModule()
{
(void) Py_InitModule("myModule", myModule_methods);
}
This guy seems to be having the same problem.
I've figured out the problem. Even though I changed the setting in xcode to specify output type "dynamic library" or "bundle", xcode was ignoring the setting. Starting a new BSD dynamic library project solved the issues I was seeing. Thanks for the help!
I've had success debugging unit-tested C extensions in XCode 4.6 using setuptools, virtualenv, unittest and GDB as the debugger.
I use virtualenvwrapper to create a virtualenv for the project and then set ~/.virtualenvs/module_name/bin/python as the executable to debug.
The single argument to pass to the virtualenv python interpreter in the Run configuration is the path to your test.py.
I then set GDB rather than None as the debugger launching it automatically.
The last step is to pass "setup.py install" as the arguments to your build tool (~/.virtualenvs/module_name/bin/python) on your test target's External Build Tool Configuration pane. The virtualenv provides a fairly simple way for you to install the shared object for your C extension into the test script python interpreter's library path without actually installing it into the global site-packages for your host.
With this setup I can call the extension code from a python script (the ultimate aim) and still debug the C code using XCode's GUI debug support.
If I haven't described this clearly please let me know and I'll share an example project.