Windows下QT中用C++调用Python之四 - 第三方扩展的使用

462 阅读1分钟

如需转载请标明出处

QQ技术交流群:129518033

Windows下QT中用C++调用Python之一 - 基础调用

Windows下QT中用C++调用Python之二 - 编译BUG处理

Windows下QT中用C++调用Python之三 - 基础参数的传入和传出

环境: Python35: python-3.5.4   32位
QT: 5.6.2  32位\

编译器:Visual Studio 2013

操作系统:windows 7 64Bit SP1

扩展:numpy‑1.13.3+mkl‑cp35‑cp35m‑win32.whl

在完成了基本的Python操作之后,我们需要引用第三方扩展进行更强大的处理。

这里我们以numpy为例做简单的说明

目标:引入numpy扩展,输出numpy的版本号

hello.py文件

# -*- coding: utf-8 -*-
import numpy as np

def hellonumpy():
	print("numpy version :",np.__version__)

def hello():
    print("hello python!")

def test_add(a, b):
	c = a + b
	print( a, "+", b," = ",c)
	return c

1.用Python安装numpy的whl

我们要使用numpy就需要先用pip命令对numpy进行安装。

安装过程这里就不说了。

如果不安装,C++调用Python会提示加载*.py失败,实际上语法有错误。

2.numpy的调用

    //获取hello模块中的hellonumpy函数
    PyObject* pFunhellonumpy= PyObject_GetAttrString(pModule,"hellonumpy");
    if(!pFunhellonumpy){
        infoData = "Get function pFunhellonumpy failed";
        qDebug()<< infoData;
    }

    //调用hellonumpy函数
    PyObject_CallFunction(pFunhellonumpy,NULL);

3.运行结果