ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

封装Python和调用C++模块的坑(使用pyinstaller和pybind11)

2021-02-14 12:32:19  阅读:596  来源: 互联网

标签:install Python C++ 虚拟环境 SingleKinect pybind11 pip include


Python 部分

依赖库的 pip 安装

创建虚拟环境并进入虚拟环境:

conda create --name bundle python=3.7
conda activate bundle

在虚拟环境下:

pip install torch===1.3.1 torchvision==0.2.2.post3 -f https://download.pytorch.org/whl/torch_stable.html
pip install opencv-python==4.2.0.32 dlib==19.19.0 pyinstaller==3.6

Pillow 的版本不能过高,需要降级:

pip install --upgrade pillow==6.0.0

setuptools 的版本不能过高,需要降级:

pip install --upgrade setuptools==40.8.0


C++ 部分

pybind11

头文件

  • 在 include 中包含 pybind11 头文件。
  • 把 Python 虚拟环境头文件路径装进来:D:\develop\Anaconda3\envs\bundle\include。

库文件

把 Python 虚拟环境(注意,一定要是 pyinstaller 所在的那个虚拟环境)的库文件目录装进来:D:\develop\Anaconda3\envs\bundle\libs。

加入链接库文件:

_tkinter.lib
python3.lib
python37.lib

暴露接口

 1 #include <pybind11/pybind11.h>
 2 namespace py = pybind11;
 3 
 4 #include <iostream>
 5 #include "SingleKinect.h"
 6 
 7 
 8 PYBIND11_MODULE(main, m) {
 9 // shorthand
10 using namespace pybind11::literals;
11 m.doc() = "pybind11 example plugin";
12 
13 // Creating bindings for a custom type
14 py::class_<ws_tech::SingleKinect>(m, "SingleKinect")
15 .def(py::init<py::function, int>())
16 .def("Open", &ws_tech::SingleKinect::Open)
17 .def("Running", &ws_tech::SingleKinect::Running)
18 .def("Close", &ws_tech::SingleKinect::Close);
19 }

更改输出类型:动态库、后缀为 pyc。

打包过程问题

pyinstall 打包

pyinstaller -F .\runner.py

必要的运行时库

  • vcruntime140_1.dll

 

参考

标签:install,Python,C++,虚拟环境,SingleKinect,pybind11,pip,include
来源: https://www.cnblogs.com/noluye/p/12367748.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有