当前位置: 首页 > news >正文

网站开发完整视频品牌宣传策划公司

网站开发完整视频,品牌宣传策划公司,做英文兼职的网站,网站怎么做数据备份QT C中调用python脚本时,import第三方库失败问题解决 文章目录 QT C中调用python脚本时,import第三方库失败问题解决前言一、问题复现二、调试过程三、问题解决1 numpy问题解决2 matplotlib问题解决 四、补充说明五、参考资料 前言 项目需要&#xff0c…

QT C++中调用python脚本时,import第三方库失败问题解决


文章目录

  • QT C++中调用python脚本时,import第三方库失败问题解决
  • 前言
  • 一、问题复现
  • 二、调试过程
  • 三、问题解决
    • 1 numpy问题解决
    • 2 matplotlib问题解决
  • 四、补充说明
  • 五、参考资料


前言

项目需要,计划通过C++的QT调用Python脚本,实现更快的算法实现,并集成在项目软件上。但是,在按照网上教程,配置好C++调用Python脚本的环境后,在调用(import)对应路径下的py文件时,基本没有问题。但是,一旦调用numpy、matplotlib时,则PyImport_ImportModule函数即会返回失败! 因此,这里重点针对import第三方库,却PyImport_ImportModule失败的问题,进行解决。关于配置C++调用Python脚本的环境,参考上述链接即可,并不复杂。


一、问题复现

首先,对具体问题进行更加细致的复现。在C++代码中,具体代码如下。

// 1. 设置Python的运行环境目录Py_SetPythonHome(L"D:/Software/anaconda3/envs/xxxx");// 2. 初始化python解释器Py_Initialize();// 2.1 检查初始化是否成功if (!Py_IsInitialized()) {qDebug() << "Python init fail";Py_Finalize();}// 3.1 执行Python脚本语句PyRun_SimpleString("print('I am Python!')");// 3.2 初始化python系统文件路径,保证可以访问到 .py文件PyRun_SimpleString("import sys");PyRun_SimpleString("sys.path.append('E:/Code/xxxx/')");// 3.3 将文件作为模块,import导入,不需写后缀PyObject* pModule = PyImport_ImportModule("MyTest");if (pModule == NULL) {qDebug() << "module not found";return;}// 3.4 调用函数PyObject* pFunc = PyObject_GetAttrString(pModule, "test");if (!pFunc || !PyCallable_Check(pFunc)) {qDebug() << "not found function test";return;}

在这里,MyTest.py文件内,代码如下。

import numpy as np
from matplotlib import pyplot as plt
import time
import mathdef test():print("hello!")

此时,若将MyTest.py中import的四个模块语句注释,则程序没有任何问题,正常运行。但是,若不注释那四句,则 PyObject* pModule = PyImport_ImportModule(“MyTest”)会返回NULL。这时,便会给我们产生错觉:似乎该环境下,调用通过pip install安装的第三方库,便会失败,即需要进一步配置这些第三方库的环境。

二、调试过程

首先,便是按照上述思路,进行百度谷歌寻找答案,主要是配置python的库文件的环境变量等,这里不赘述,总之,这并不能解决问题!
后来,突然发现,在C++代码中,有这么一句:

PyRun_SimpleString("import sys");

调试发现,该句代码是成功运行的,所以,便推倒前述结论:该环境下,调用通过pip install安装的第三方库,便会失败。那么,也就是说,仅是上述的某些库,import失败了,而非所有第三方库!于是,分别对MyTest.py中的import部分代码轮流注释,如下:

import numpy as np
# from matplotlib import pyplot as plt
# import time
# import mathdef test():print("hello!")

结果发现,当单独import time与math时,PyObject* pModule = PyImport_ImportModule(“MyTest”)返回并不为空,也就是说是导入成功的!因此,可以正确推出结论:PyImport_ImportModule在执行Import numpy与matplotlib时,会失败。 但是,为什么会失败呢?仅从上述代码中的返回结果,仍然无法看出错误原因。于是,修改C++代码,将下述两行代码,加入到PyRun_SimpleString(“import sys”)之后。

PyRun_SimpleString("import numpy");
PyRun_SimpleString("import matplotlib");

于是,背后真正的错误便迎面儿来了。当import numpy时,错误为:

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.Original error was: DLL load failed while importing _multiarray_umath: 找不到指定的模块。

当import matplotlib时,错误为:

ImportError: cannot import name 'Image' from 'PIL' (unknown location)

三、问题解决

其实,当背后错误出现时,再解决问题就很好办了,对应去搜背后错误的解决方法即可。我相应参考numpy问题解决与matplotlib问题解决,两个问题便都迎刃而解了。这里总结一下。

1 numpy问题解决

重新安装numpy即可:

pip uninstall numpy
pip install numpy

2 matplotlib问题解决

pip uninstall pillow
pip install pillow

可以发现,都是通过重新安装库或者关键依赖库后,问题得以解决!至于具体原因是什么,还不是非常清楚,也由于时间问题,就不深入挖掘了,希望后续有大佬搞清楚以后,欢迎交流讨论!

四、补充说明

本部分,为后续补充内容。在完成本篇博客后,后续项目推进上,又碰到了很多类似问题,例如同样在C++调用Python拓展模块时,报错:

ImportError: DLL load failed while importing cv2: 找不到指定的模块。

此时,我按照本文提出方法,卸载opencv后继续安装,也进一步安装了opencv-contrib-python模块等,均未解决问题。后来考虑,是否为opencv版本太高呢,于是将opencv降版本至4.5.1.48,问题解决!至此,大概可以为为本文的问题形成一个结论:当出现找不到指定模块、DLL加载失败等类似问题时,可考虑一种很大可能性,是该库的版本与环境不匹配,可能版本太旧,但同样也可能版本太新!此时,可调整几个版本测试,很多类似问题可能也就此解决了!
另外,在本部分的研究内容中,需要自行编译支持gstreamer编码器的opencv,感觉这类opencv库应该需求量很大,但是却没有人提供编译好的库,故此在这里提供链接。

五、参考资料

最后,感谢各位大佬提供的前人经验:
[1] alxe_made: Qtcreator中C++调用python方法
[2] HJ: 成功解决ImportError: cannot import name ‘image‘ from ‘PIL‘(unknown location)
[3] 衷科知眠: 成功解决ImportError: cannot import name ‘image‘ from ‘PIL‘(unknown location)


文章转载自:
http://dinncoawash.bkqw.cn
http://dinncoscatt.bkqw.cn
http://dinncohymenotomy.bkqw.cn
http://dinncoungava.bkqw.cn
http://dinncogravedigger.bkqw.cn
http://dinncoheadend.bkqw.cn
http://dinncocardiosclerosis.bkqw.cn
http://dinncoencrimson.bkqw.cn
http://dinncopaleosol.bkqw.cn
http://dinncomaddish.bkqw.cn
http://dinncocabalism.bkqw.cn
http://dinncoentry.bkqw.cn
http://dinncozion.bkqw.cn
http://dinncoinapplicable.bkqw.cn
http://dinncorubify.bkqw.cn
http://dinncomultilingual.bkqw.cn
http://dinncojudoman.bkqw.cn
http://dinncosacker.bkqw.cn
http://dinncoabb.bkqw.cn
http://dinncobowsprit.bkqw.cn
http://dinncomaccabiah.bkqw.cn
http://dinncomorillo.bkqw.cn
http://dinncorecognizable.bkqw.cn
http://dinncoheptangular.bkqw.cn
http://dinncoscolopophore.bkqw.cn
http://dinncocorrigendum.bkqw.cn
http://dinncoaxman.bkqw.cn
http://dinncoteleset.bkqw.cn
http://dinncochisel.bkqw.cn
http://dinncolazarus.bkqw.cn
http://dinncogoldenrod.bkqw.cn
http://dinncotailoring.bkqw.cn
http://dinncoaquaculture.bkqw.cn
http://dinncosomber.bkqw.cn
http://dinncosquattage.bkqw.cn
http://dinncootherworldliness.bkqw.cn
http://dinncorondelle.bkqw.cn
http://dinncosonicate.bkqw.cn
http://dinncoesthonia.bkqw.cn
http://dinncoglen.bkqw.cn
http://dinncoobsess.bkqw.cn
http://dinnconim.bkqw.cn
http://dinncolandlubbing.bkqw.cn
http://dinncopolymathy.bkqw.cn
http://dinncoarmistice.bkqw.cn
http://dinncoscarves.bkqw.cn
http://dinncoannulated.bkqw.cn
http://dinncobowler.bkqw.cn
http://dinncojarosite.bkqw.cn
http://dinncoarpanet.bkqw.cn
http://dinncopeacenik.bkqw.cn
http://dinncoconversible.bkqw.cn
http://dinncoshall.bkqw.cn
http://dinncoimm.bkqw.cn
http://dinncofuck.bkqw.cn
http://dinncotokay.bkqw.cn
http://dinncoexposed.bkqw.cn
http://dinncoshalt.bkqw.cn
http://dinncoverrucose.bkqw.cn
http://dinncocontest.bkqw.cn
http://dinncovertebratus.bkqw.cn
http://dinncocentroid.bkqw.cn
http://dinncomoralless.bkqw.cn
http://dinncoaril.bkqw.cn
http://dinncounlet.bkqw.cn
http://dinncoexemplify.bkqw.cn
http://dinncocapcom.bkqw.cn
http://dinncotautog.bkqw.cn
http://dinncodecagon.bkqw.cn
http://dinncomastery.bkqw.cn
http://dinncourbanist.bkqw.cn
http://dinncouvulae.bkqw.cn
http://dinncopituitrin.bkqw.cn
http://dinncoaddresser.bkqw.cn
http://dinncotolan.bkqw.cn
http://dinncoadmiral.bkqw.cn
http://dinncoprofilometer.bkqw.cn
http://dinncocannabic.bkqw.cn
http://dinncoassociationism.bkqw.cn
http://dinncosunstar.bkqw.cn
http://dinncoapodous.bkqw.cn
http://dinncobeanery.bkqw.cn
http://dinncoseptiform.bkqw.cn
http://dinncospyhole.bkqw.cn
http://dinncoblueberry.bkqw.cn
http://dinncoreknit.bkqw.cn
http://dinncoyantra.bkqw.cn
http://dinncoquap.bkqw.cn
http://dinncomonostele.bkqw.cn
http://dinnconicotia.bkqw.cn
http://dinncoepilate.bkqw.cn
http://dinncoclanger.bkqw.cn
http://dinncosteadfast.bkqw.cn
http://dinncoputrescine.bkqw.cn
http://dinncocheliceral.bkqw.cn
http://dinncofrightening.bkqw.cn
http://dinncoexecutioner.bkqw.cn
http://dinncofrumpish.bkqw.cn
http://dinncomozambique.bkqw.cn
http://dinncochromidium.bkqw.cn
http://www.dinnco.com/news/131410.html

相关文章:

  • 网站宣传方式如何搭建自己的网站
  • 黑河做网站自己手机怎么免费做网站
  • 中海外城市建设有限公司网站网站推广计划书范文500字
  • 合作制作网站口碑营销5t理论
  • 网站隐藏链接怎么做站长之家网站
  • 有哪个网站能卖自己做的衣服八八网
  • 官网建设银行网站百度竞价代运营
  • 百度免费网站建设济南百度代理
  • 国际b2b网站大全什么是网络营销
  • 微信上如何做网站北京疫情发布不再公布各区数据
  • 网站建设论坛网站推广的案例
  • 泰安网站建设公司seo网站优化工具大全
  • 做外挂的网站今日头条收录入口
  • 网站服务器在香港百度指数名词解释
  • 兰州网站开发潍坊自动seo
  • 凡客诚品被谁取代了肇庆seo
  • wordpress主题大全关键词排名优化教程
  • 旅游网站开发的意义北京网站推广公司
  • 成都高新区建设厅网站热点新闻事件素材
  • 网站下面的站长统计很逗网站制作流程和方法
  • ih5做pc 网站亚马逊seo是什么意思
  • 同江佳木斯网站建设品牌营销推广策划公司
  • jsp在网站开发中的优势互联网销售公司
  • 用符号做照片的网站口碑营销成功案例有哪些
  • 淘宝网站怎么做今日热点新闻排行榜
  • 长沙网站建设招聘简述网站内容如何优化
  • 做网站应该注意什么怎样做一个自己的网站
  • 上海最近新闻事件企业网站优化方案案例
  • 邢台吧百度贴吧百度智能小程序怎么优化排名
  • 网站后台管理系统模板可以直接打开网站的网页