先将终端所在路径切换到python脚本文件的目录下
然后给脚本文件运行权限,一般755就OK,如果完全是自己的私人电脑,也不做服务器什么的,给777的权限问题也不大(具体权限含义参考chmod指令的介绍,就不赘述了):
chmod 755 ./*.py
然后执行。
在linux命令行模式中运行python,进入python交互式环境,写程序后直接输出结果。
B. linux C语言调用Python脚本
比如什么变量呢?
可以用命令行参数啊
system("python xxx.py arg1 arg2 ...")
如果让python接收参数自己查一下
C. linux下用qt需要用到python在Py_Initialize就报错
你在链接的时候必须提供正确的链接参数 (需要把 libpython 链接到你的可执行程序才能正确找到python C API)
具体可以用
pythonX.Y-config--cflags
得到编译参数。
用
pythonX.Y-config--ldflags
得到链接参数
比如在我的系统上python2.7得到的参数是
$python2.7-config--cflags
-I/usr/include/python2.7-I/usr/include/python2.7-fno-strict-aliasing-march=x86-64-mtune=generic-O2-pipe-fstack-protector-strong--param=ssp-buffer-size=4-DNDEBUG-march=x86-64-mtune=generic-O2-pipe-fstack-protector-strong--param=ssp-buffer-size=4
$python2.7-config--ldflags
-lpython2.7-lpthread-ldl-lutil-lm-Xlinker-export-dynami
那么就在编译和链接的时候分别加入上面列出的参数,就可以了 (注意,不要加我例子中打印出来的内容,要加你自己执行pythonX.Y-config 后得到的内容)。
D. 如何在QT中调用python 我有一个QT的程序,想要调用python的一些脚本,该怎么做呢
C语言中怎么调就怎么调,python文档中有简单的例子,自己看着办就行了。
E. qt中c++调用python是含类的模块导入怎么处理呢
#include <QCoreApplication>
#include
<Python.h>
#include
<iostream>
using
namespace
std;
int
main(
int
argc,
char
argv[]) { QCoreApplication a(argc, argv);
//
初始化python模块
Py_Initialize();
if
( !
Py_IsInitialized() ) {
return
; }
//
导入test.py模块
PyObject* pMole = PyImport_ImportMole(
test
);
if
(!
pMole) { printf(
Cant open python file!\n
);
return
; }
//
获取test模块中的hello函数
PyObject* pFunhello= PyObject_GetAttrString(pMole,
hello
);
//
注释掉的这部分是另一种获得test模块中的hello函数的方法
//
PyObject* pDict = PyMole_GetDict(pMole);
//
if (!pDict) {
//
printf("Cant find dictionary.\n");
//
return -1;
//
//
PyObject* pFunhello = PyDict_GetItemString(pDict, "hello");
if
(!
pFunhello){ cout
<<
Get function hello failed
<<
endl;
return
; }
//
调用hello函数
PyObject_CallFunction(pFunhello,NULL);
//
结束,释放python
Py_Finalize();
return
a.exec(); }
F. 想用在linux下用Qt写c++能够调用Python的程序,应该如何配置参数
QT用的是C++吧,C++可以和Python混合编程啊。
http://www.udpwork.com/item/10422.html
G. linux下Qt中我想添加python.h但是不知道libs和include路径,怎么办
可以的,前提是,Qt必须安装在linux,中,如果是Qt creator 你可以写好了再到linux下编译。否则报错。
H. QT中调用Python
如果你是嵌入的话,应该没问题。python的库会暴露c api的,你的qt程序照着python文档中的embedded章节就好。 看样子,你的是linux下的,不过一般头文件就是include/python.h,怎么会是include/python2.7呢?你写错了吧。库文件应该是在lib下面。
I. 怎样用QtCreator编辑运行python脚本
不知道你是在Windows还是在linux系统上
在windows上面先 Win+R 然后 进入CMD 命令行-> 输入:cd py文件的目录
进入你py文件所在的目录后 输入:python 脚本文件名.py 就可以运行了
在linux下,模拟终端输入:python 然后把你的脚本拖进命令行就可以运行了
J. 在linux下用qt编程时,请教怎样在程序中调用另一个程序,例如我写好了个聊天程序,想添加个按钮,
使用QProcess,看下Qt的帮助文档有关QProcess这块的你就懂了。