當前位置:首頁 » 編程語言 » c移植到python

c移植到python

發布時間: 2023-08-30 07:13:01

『壹』 求助 關於c程序中嵌入python的問題

嵌入
與python的擴展相對,嵌入是把Python解釋器包裝到C的程序中。這樣做可以給大型的,單一的,要求嚴格的,私有的並且(或者)極其重要的應用程序內嵌Python解釋器的能力。一旦內嵌了Python,世界完全不一樣了。

C調用python中的函數:
hw.py:

#coding=utf8

def hw_hs(canshu):
return canshu

if __name__ == "__main__":
ccss = "I am hw"
print hw_hs(ccss)

helloWorld.py:

#coding=utf8
import hw

def hello():
ccss = "I am helloWorld"
return hw.hw_hs(ccss)

if __name__ == "__main__":
print hello()

testcpypy.c:

//#include "testcpypy.h"
#include <Python.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
//初始化Python
Py_Initialize();
if (!Py_IsInitialized()) {
printf("Py_Initialize");
getchar();
return -1;
}

//執行python語句
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");

PyObject *pMole = NULL;
PyObject *pFunc = NULL;
PyObject *reslt =NULL;

//載入python模塊
if(!(pMole = PyImport_ImportMole("helloWorld"))) {
printf("PyImport_ImportMole");
getchar();
return -1;
}

//查找函數
pFunc = PyObject_GetAttrString(pMole, "hello");
if ( !pFunc || !PyCallable_Check(pFunc) )
{
printf("can't find function [hello]");
getchar();
return -1;
}

//調用python中的函數
reslt = (PyObject*)PyEval_CallObject(pFunc, NULL);
//printf("function return value : %d\r\n", PyInt_AsLong(reslt));

//將python返回的對象轉換為C的字元串
char *resltc=NULL;
int res;
res = PyArg_Parse(reslt, "s", &resltc);
if (!res) {
printf("PyArg_Parse");
getchar();
return -1;
}
printf("resltc is %s", resltc);
getchar();

//釋放內存
Py_DECREF(reslt);
Py_DECREF(pFunc);
Py_DECREF(pMole);

//關閉python
Py_Finalize();

return 0;
}

編譯
gcc -o testcpypy testcpypy.c -IC:\Python27\include -LC:\Python27\libs -lpython27 ---C:\Python27為python安裝目錄
或:
gcc -c testcpypy.c -IC:\Python27\include
gcc -o testcpypy.exe testcpypy.o -LC:\Python27\libs -lpython27
執行結果:

帶參數的情況:

#include "callpydll.h"
#include "Python.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

int callhello(char *instr, char *outstr)
{

PyObject *pMole = NULL;
PyObject *pFunc = NULL;
PyObject *reslt = NULL;
PyObject *pParm = NULL;

char *resltc = NULL;
int resltn;
int res;

char *helloWorld = "TestIM_ProtocBuf";

char *im_account = "aaaa";
char *auth_code = "aaaa";
char *im_uid = "aaaa";
char *proxy_topic = "";

//初始化Python
Py_Initialize();
if (!Py_IsInitialized()) {
printf("Py_Initialize");
getchar();
return -1;
}

//執行python語句
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");

//載入python模塊
if(!(pMole = PyImport_ImportMole(helloWorld))) {
printf("PyImport_ImportMole");
getchar();
return -2;
}

//查找函數
pFunc = PyObject_GetAttrString(pMole, "login_proxy_body_serialize");
if ( !pFunc || !PyCallable_Check(pFunc) )
{
printf("can't find function [hello]");
getchar();
return -3;
}

//參數轉換C --> python, 參數必須是元組(一個參數也是,否則會失敗!!!坑啊)
pParm = Py_BuildValue("(ssss)", im_account, auth_code, im_uid, proxy_topic);

//調用python中的函數
reslt = (PyObject*)PyEval_CallObject(pFunc, pParm);

//將python返回的對象轉換為C的字元串
res = PyArg_ParseTuple(reslt, "si", &resltc, &resltn);
if (!res) {
printf("PyArg_Parse");
getchar();
return -4;
}

printf("resltn is %d", resltn);
memcpy(outstr, resltc, strlen(resltc)+1);

//釋放內存
Py_DECREF(reslt);
Py_DECREF(pFunc);
Py_DECREF(pMole);
Py_DECREF(pParm);

//關閉python
Py_Finalize();

return 0;
}

int main() {
int i;
char *dais = "iammain";
char res[10240];
memset(res,'\0',sizeof(res));

i = callhello(dais, res);
if(0 != i) {
printf("Notify:error");
getchar();
return -1;
}
printf("result is %s", res);
getchar();
return 0;
}

『貳』 C語言怎麼轉化成python

要將C語言轉化為python,前提:
1
對C語言語法即python編程均熟練掌握;
2
演算法相關有一定了解;
3
至少可以看懂要轉換的C語言程序。
由於C語言的語句,與python沒有對應關系,所以只能按照如下方式轉換:
1
讀懂C語言代碼實現功能,可以以函數為單位;
2
按照功能,依照python方式實現相同功能。

『叄』 怎麼把這個C語言轉換成python

C語言不能轉化為python,它們之間沒有之間聯系,只能說演算法是可以轉化實現的。

『肆』 c可以調用python嗎

可以的。

C中內嵌Python
新建立一個工程,首先需要將工作目錄設置到Python-3.1.1PCbuild中,以獲取到動態庫,至於靜態庫的包含,Include目錄的指定,那自然也是少不了的。文件中需要包含Python.h文件,這也是必須的。
介面中
Py_Initialize();
Py_Finalize();

其他的根據需求,再引入相應的python builder 即可

『伍』 c如何調用python程序

C語言如何調用python,相關步驟如下:

首先,C語言中調用python,要使用頭文件Python.h。

2、接著,定義一個調用python的函數。

相關推薦:《Python教程》

3、函數中,設置python庫的路徑。

4、然後,初始化python。

5、運行一個python代碼,輸出How are you。

6、最後,釋放python。

熱點內容
r7000p2021買哪個配置 發布:2025-02-04 06:40:17 瀏覽:965
如何消除微信小程序緩存 發布:2025-02-04 06:34:24 瀏覽:633
python27mysqldb 發布:2025-02-04 06:28:44 瀏覽:768
svn文件夾許可權 發布:2025-02-04 06:23:47 瀏覽:901
師編程 發布:2025-02-04 06:22:51 瀏覽:169
加密類型wpa 發布:2025-02-04 06:21:27 瀏覽:178
互聯網與雲伺服器 發布:2025-02-04 06:15:56 瀏覽:254
硬碟挖礦源碼 發布:2025-02-04 06:15:45 瀏覽:76
寶馬3系哪個配置合適 發布:2025-02-04 06:03:10 瀏覽:328
磁碟存儲器的管理課後答案 發布:2025-02-04 05:58:58 瀏覽:600