vc中调用python代码
运行一句python命令
对vc设置路径
include:D:\PYTHON31\INCLUDE
lib:D:\PYTHON31\LIBS
#include "stdafx.h"
#include "python.h"
int main(int argc, char* argv[])
{
Py_Initialize() ;
PyRun_SimpleString("print('Hello')");
//PyRun_SimpleString("print(dir())");
Py_Finalize();
return 0;
}
编译、连接
拷贝D:\Python31\python31.dll到exe文件目录
运行,输出到控制台
可以运行多行命令
Py_Initialize() ;
PyRun_SimpleString("x=100");
PyRun_SimpleString("print(x)");
PyRun_SimpleString("a=x*x\nprint(a)\n");
Py_Finalize();
相当于在python下运行下列代码
x=100
print(X)
a=x*x
print(a)
控制台输入
以下代码解释控制台输入
#include "stdafx.h"
#include "python.h"
int main(int argc, char* argv[])
{
Py_Initialize() ;
PyRun_AnyFile(stdin,NULL);
Py_Finalize();
return 0;
}
读取python变量值
将python运行结果读取到vc中
char *cstr;
PyObject *pstr;
PyObject *main_dict;
Py_Initialize() ;
PyObject* main_module = PyImport_AddModule("__main__");
main_dict = PyModule_GetDict(main_module);
PyRun_SimpleString("x='abc'");
pstr = PyRun_String("x", Py_eval_input, main_dict, main_dict);
PyArg_Parse(pstr,"s",&cstr); //转换
printf(cstr);
Py_Finalize();
相关文档:
以下"#"开头是Ubuntu终端命令
1。首先安装Ubuntu10.04
参考 http://wiki.ubuntu.org.cn/
2。修改root用户密码
3。使用root登陆系统
4。Ubuntu默认已经安装python2.6.5
5。下载stackless
查看网址 http://zope.stackless.com/download/sdocument_view
# cd /usr/src
# wget http://www.sta ......
初学Python,这么做好像有点2,凑合能用:
class MyClass():
def __init__(self, n = 10):
self._Field = n
def __getitem__(self, range):
return MyClass(self._Field)
obj1 = MyClass()
obj2 = obj1
obj3 = obj1[:]
obj1._Field = 100
obj4 = MyClass(123)
print obj1._Field, obj2. ......
上一篇中我们已经了解如何在Python程序和C模块之间进行值的相互传递,现在我们来进入实作阶段,看看如何将一个C语言开发的开源mp3编解码库LAME包装为一个Python下可以使用的扩展模块。首先去http://lame.sourceforge.net/download.php下载LAME的源代码,然后切换到root用户编译源代码,./configure
make
make instal ......
exec语句用来执行储存在字符串或文件中的Python语句。例如,我们可以在运行时生成一个包含Python代码的字符串,然后使用exec语句执行这些语句。下面是一个简单的例子。
>>> exec 'print "Hello World"'
Hello World
eval语句用来计算存储在字符串中的有效Python表达式。下面是一个简单的例子。
>>> ......
eval(str [,globals [,locals ]])函数将字符串str当成有效Python表达式来求值,并返回计算结果。
同样地, exec语句将字符串str当成有效Python代码来执行.提供给exec的代码的名称空间和exec语句的名称空间相同.
最后,execfile(filename [,globals [,locals ]])函数可以用来执行一个文件,看下面的例子:
>>> ev ......