C++(vs2017)调用python

版权声明:王小溪原创专属 https://blog.csdn.net/sinat_41196089/article/details/82658169

VS2017添加python

本环境添加vc++项目:

VS2017中创建一个空的Windows控制台应用程序

在项目中右键弹出的对话框中选择属性

1)修改C/C++下的常规

2)修改链接器下的常规

3)修改链接器下的输入添加python36.lib;(这个没有,是手动写的)Debug版本用的是pythonxx_d.lib

代码实例:

#include "stdafx.h"
#include <Python.h>
int main()
{
    Py_Initialize();
    PyRun_SimpleString("print('Hello Python!')\n");
    Py_Finalize();
    return 0;
}

Python解释器发布

  1. python36.dll
    放在exe同级目录
  2. dlls/pyd
    1) Python36\DLLs里的pyd拷贝到exe同级目录

2) pyd放入exe同级目录下的dlls下,并在cpp中添加如下语句:

PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('dlls/')");
  1. python36.zip
  • 针对默认库将需要的Python36\Lib里的py/pyc和文件夹拷贝进去即可

  • 针对第三方库,将Python36-32\Lib\site-packages下的内容复制到zip

猜你喜欢

转载自blog.csdn.net/sinat_41196089/article/details/82658169
今日推荐