64,654
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <Python.h>
int main(int argc, char* argv[])
{
PyObject *pModule = NULL;
PyObject *pFunc = NULL;
Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
pModule = PyImport_ImportModule("hello");
pFunc = PyObject_GetAttrString(pModule, "hello");
PyEval_CallObject(pFunc, NULL);
Py_Finalize();
return 0;
}
gcc -I/usr/include/python2.6/ -L/usr/lib/ -lpython2.6 -o main main.c
确保在当前目录下放置hello.py
def hello():
print 'Hello!'
#include <stdio.h>
#include <iostream>
#include <Python.h>
int main(int argc, char* argv[])
{
PyObject *pModule = NULL;
Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
pModule = PyImport_ImportModule("Py_PubUtility");
if(!pModule)
printf("Error");
getchar();
return 0;
}
#include <string>
#include <map>
#include <Python.h>
typedef std::map<std::string,PyObject*> pyModuleMap;
typedef pyModuleMap::iterator pyModuleMapIter;
class PyOperBase
{
public:
static int initPython() //must running at the main thread
{
if(m_isInit == true)
return true;
Py_Initialize();
if(!Py_IsInitialized())
{
return false;
}
//Need to specify the current path path only Linux
#ifdef LINUX
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
#endif
//PyEval_InitThreads();
//PyEval_ReleaseThread(PyThreadState_Get());
m_isInit = true;
return true;
}
static int releasePython()
{
if(!m_isInit) return false;
pyModuleMapIter iter;
PyObject* pModule = NULL;
for(iter = m_ModuleMap.begin(); iter != m_ModuleMap.end(); iter++)
{
pModule = iter->second;
Py_DECREF(pModule);
}
m_ModuleMap.clear();
//PyGILState_Ensure();
Py_Finalize();
m_isInit = false;
return true;
}
static bool getInitStatus()
{
return m_isInit;
}
static int getModleDictByScriptName(std::string& script, PyObject** object)
{
if(m_isInit != true)
return false;
PyObject *pName = NULL, *pModule = NULL;
pyModuleMapIter iter = m_ModuleMap.find(script);
if(iter != m_ModuleMap.end())
{
pModule = iter->second;
if(!getDictByModle(pModule,object))
return false;
return true;
}
pName = PyString_FromString(script.c_str());
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if(!pModule)
{
return false;
}
if(!getDictByModle(pModule,object))
{
Py_DECREF(pModule);
return false;
}
m_ModuleMap.insert(std::pair<std::string,PyObject*>(script,pModule));
return true;
}
private:
static int getDictByModle(PyObject* pModle, PyObject** ppDict)
{
PyObject *pDict = NULL;
pDict = PyModule_GetDict(pModle);
if(!pDict)
{
return false;
}
*ppDict = pDict;
return true;
}
static bool m_isInit;
static pyModuleMap m_ModuleMap;
};
class PyThreadLock
{
public:
PyThreadLock(void)
{
state = PyGILState_Ensure( );
}
~PyThreadLock(void)
{
PyGILState_Release( state );
}
private:
PyGILState_STATE state;
};
.cpp文件如下
#include "XPS2SVG_py.h"
bool PyOperBase::m_isInit = false;
pyModuleMap PyOperBase::m_ModuleMap;
我linux下环境变量配置好了python路径,所以直接#include <Python.h>了
以上代码在windows下跑没问题。
但到linux下,pModule = PyImport_Import(pName);会返回空
应该是没找到pName标识的模块,但是pName标识标识的模块文件就在可执行文件的当前目录下,