I am trying to build an application to parse netCDF4 files in c++.
What I have done:
Succeeded in parsing the files in python scripts.
What I need help with:
Including my python script as a module. When I run my C++ program it complains that it can't access numPy.
What I Know:
I fixed the same issue with the netCDF4 by copying the netCDF4.pyd file where my c++ executable was located, but cannot find the numPy. equivalent.
Thanks for any suggestions.
Welcome to the community! I hope I understand the question correctly - is it importing a python module into c++? If that is so, try the following tutorial: Embedding Python in C/C++: Part I.
Basically, to paraphrase, DON'T use code conversion or translation - it is unnecessairly complex. Just make the python code communicate with the C code as follows. Kudos to website above for the code, comments by me.
#include <Python.h> //get the scripts to interact
int main(int argc, char *argv[])
{
PyObject *pName, *pModule, *pDict, *pFunc, *pValue; //Arguments that are required.
if (argc < 3)
{
printf("Usage: exe_name python_source function_name\n");
return 1;
}
// Allow your program to understand python
Py_Initialize();
// Initialize Name
pName = PyString_FromString(argv[1]);
// Import the module from python
pModule = PyImport_Import(pName);
// pDict is a borrowed reference
pDict = PyModule_GetDict(pModule);
// same situiation as above
pFunc = PyDict_GetItemString(pDict, argv[2]);
if (PyCallable_Check(pFunc))
{
PyObject_CallObject(pFunc, NULL);
} else
{
PyErr_Print();
}
// Tidy Everything Up.
Py_DECREF(pModule);
Py_DECREF(pName);
// End interpreter
Py_Finalize();
return 0;
}
I suggest you read the above tutorial, though.
Hope this helps!
Related
For a project of mine, I need to embed the Python interpreter in a wider C application, using the interpreter of an Anaconda environment. How can I do so ? I've looked online, but nothing mentions it. I'm guessing I should change something near Py_Initialize(), but what ?
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#ifndef PY_SSIZE_T_CLEAN
#define PY_SSIZE_T_CLEAN
#endif
#include <Python.h>
int main()
{
Py_Initialize(); // Here ?
// Maybe something like Py_InitializeInterpreter("whatever/path") ?
PyObject* pName = PyUnicode_DecodeFSDefault("script");
PyObject* pModule = PyImport_Import(pName);
Py_DecRef(pName);
bool running = false;
while(running)
{
//whatever I need to do
}
Py_DecRef(pModule);
Py_Finalize();
return EXIT_SUCCESS;
}
EDIT : I'd just like to point out one thing : I'm not a Python dev by any means, I just need to call Python functions from my C application. The Python itself is coded by someone else, so I don't have much knowledge at all about how it works.
I have been trying to work out how to make a .pyd (Python Extension Module) file from a C script (without swig or anything else except MinGW) and have successfully built it into a .pyd.
The problem however occurs when I try and import the module.
If I run it the module runs successfully (as far as I can see) and then an error appears saying Python Has Stopped Working and it closes without executing the rest of the program.
Here is my C script (test.c):
#include <python.h>
int main()
{
PyInit_test();
return 0;
}
int PyInit_test()
{
printf("hello world");
}
And Python Script (file.py):
import test
print('Run From Python Extension')
I compiled the script with:
gcc -c file.py
gcc -shared -o test.pyd test.c
I can't find any errors when compiling in command prompt and am using python 3.6 (running on Windows 10).
I can't find much on the subject and would prefer to keep away from Cython (I already know C) and Swig.
Any help to tell me what is wrong would be fantastic.
Creating a Python extension is completely different than writing regular C code. What you have done is simply creating a valid C program but that doesn't make sense for Python.
That's how your program should look like (it's just a skeleton, not the proper, working code):
#include <Python.h>
#include <stdlib.h>
static PyObject* test(PyObject* self, PyObject* args)
{
printf("hello world");
return NULL;
}
static PyMethodDef test_methods[] = {
{"test", test, METH_VARARGS, "My test method."},
{NULL, NULL, 0, NULL} /* Sentinel */
};
PyMODINIT_FUNC init_test_methods() {
Py_InitModule("test", test_methods);
}
int main(int argc, char** argv)
{
/* Pass argv[0] to the Python interpreter */
Py_SetProgramName(argv[0]);
/* Initialize the Python interpreter. Required. */
Py_Initialize();
/* Add a static module */
init_test_methods();
}
I recommend you read more about this at the following link: http://dan.iel.fm/posts/python-c-extensions/ as well as in the official docs.
I'm trying to translate some simple cython to c++ :
cdef public int G = 1
and then use it in my c++ code :
#include <Python.h>
#include "test.h" // The file generated by the cython command
#include <iostream>
int main(int argc, char **argv) {
std::cout << "G : " << G << std::endl;
}
Output is :
G : 0
I've looked into the test.cpp cython-generated file, and on line 897, I have
G = 1;
So why is G evaluated to 0 in the main ?
Here are the commands used to compile :
cython.exe test.pyx -3 --cplus
g++ test.cpp test_main.cpp -IC:\Python36-32\include -LC:\Python36-32\libs -lpython36
What you generate when you use cython is a python extension module. You cannot link it directly into an executable, as it needs to be dynamically imported and linked to libpython. In that process, your extension's initialization function is run, which will cause your G to be set to 1.
So you should:
Build a python extension from your cython (using -shared and outputting a DLL).
Load python interpreter in your main. In your program, you don't even initialize python at the moment.
Import it in your main using PyImport_ImportModule("mymodule").
Untested, but your main should look like this:
#include <Python.h>
int main(int argc, char * argv[])
{
Py_Initialize();
PyObject * test_module = PyImport_ImportModule("test");
// do something with test_module
Py_Finalize();
return 0;
}
You can get G from python using PyObject_GetAttrString(), or since you declared it as cdef, you can access it directly using your OS's symbol resolution tools, such as GetProcAddress() for windows.
It may be possible to link dynamically at load time, yet still use importmodule to let python do its initialization magic, but I have no idea how to do that or if it's even possible.
My question is when you embed python in c++ after you compile the program you get an exe file and that's it right?
I have another question (and this is the reason I signed up is to ask this one) if someone opened my program in a hex editor and I had some python code like "def add(x,y):return(x+y)" would the python code show up in the hex editor as plain english?
There is an example of a C program with embedded Python code at https://docs.python.org/3.5/extending/embedding.html:
#include <Python.h>
int
main(int argc, char *argv[])
{
wchar_t *program = Py_DecodeLocale(argv[0], NULL);
if (program == NULL) {
fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
exit(1);
}
Py_SetProgramName(program); /* optional but recommended */
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print('Today is', ctime(time()))\n");
Py_Finalize();
PyMem_RawFree(program);
return 0;
}
As you can see, the Python code is present in a plain C static char array, so when you open the executable in editor or hex viewer, it will be visible as is.
But you can encrypt or compress the code, put that compressed version into your source code and decrypt/decompress it in runtime just before you pass it to PyRun_SimpleString. That way it would be obfuscated and not easily visible in hex editor. But someone who can use a debugger could still dig the Python code out.
I'm making an Qt Quick GUI application(for windows), which uses OpenGL and C++ for some computationally intensive stuff. I want to embed python code into the app, for doing some stuff which is comparatively easier in python.
Basically, I just want the c++ code to call a function in a python script and let the script do the job, then store the returned data in a variable(string, or float etc.) for further use. I'm using Qt creator, and I got python3 lib for MinGW compiler. I tried some code, but its looks like python lib is not quite compatible with Qt creator. IS using pyqt for this will be a good idea? What will be the best and easiest way to do this ?
EDIT: This is the basic code I tried, first it gave me an error saying, cannot find pyconfig.h. Then I added an INCUDEPATH to my python34 include directory.
#include "mainwindow.h"
#include <QApplication>
#include <boost/python.hpp>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
using namespace boost::python;
PyObject *pName, *pModule, *pDict, *pFunc, *pValue;
Py_Initialize();
pName = PyString_FromString(argv[1]);
pModule = PyImport_Import(pName);
pDict = PyModule_GetDict(pModule);
pFunc = PyDict_GetItemString(pDict, argv[2]);
if (PyCallable_Check(pFunc))
{
PyObject_CallObject(pFunc, NULL);
} else
{
PyErr_Print();
}
// Clean up
Py_DECREF(pModule);
Py_DECREF(pName);
Py_Finalize();
return a.exec();
}
My .pro file:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = TestWidgetApp
TEMPLATE = app
INCLUDEPATH += C:/boost_1_57_0
INCLUDEPATH += C:/Python34/include
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
OTHER_FILES +=
Then the following errors:
C:\Python34\include\object.h:435: error: C2059: syntax error : ';'
C:\Python34\include\object.h:435: error: C2238: unexpected token(s) preceding ';'
C:\Users\Amol\Desktop\TestWidgetApp\main.cpp:19: error: C3861: 'PyString_FromString': identifier not found
The problem here is that Python 3.4 has a struct member called "slots", (file object.h, in the typedef for PyType_Spec), which Qt defines out from under you so that you can say things like:
public slots:
in your code. The solution is to add:
#undef slots
just before you include Python.h, and to redefine it before you include anything that uses "slots" in the way that Qt does:
#undef slots
#include <Python.h>
#define slots
#include "myinclude.h"
#include <QString>
A bit of a hack (because you're depending on a particular definition of slots in Qt), but it should get you going.
I have removed all the Qt code from your example and then I tried to compile it (Qt has nothing to do with your compile error). And it compiles for me. The difference was I used the include files from Python 2.7.
So I did a little search for the string PyString_FromString in the folders: C:\Python33\includes (I noted you use python 3.4 and not 3.3 but I suspect this is a 3.x thing) and C:\Python27\includes.
Results:
Python 3.3
Python 2.7
So, apparently, Python 3.4 is not supported by your BoostPython version.
Python3 has no PyString_FromString function. Python3 str type internally is unicode objects with complex structure.
Use PyUnicode_FromString or PyUnicode_FromStringAndSize for constructing str object from UTF-8 encoded C string (char*).
Move your
#include "boost/python.hpp"
...to be before your other includes and it will resolve your problem.
The actual issue is as Scott Deerwester described in his answer.