VScode中配置环境
首先是在VScode中为C++调用python接口配置环境
1、配置step1
打开vscode的拓展商店安装python
2、配置step2
然后编辑c_cpp_properties.json文件,在文件中的includePath项添加自己的python include路径
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"E:\\Anaconda3\\include"#这里的路径即为我们要添加的自己的python路径
],
"defines": ["_DEBUG", "UNICODE", "_UNICODE"],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "E:\\C++\\mingw64\\bin\\g++.exe", /*修改成自己bin目录下的g++.exe,这里的路径和电脑里复制的文件目录有一点不一样,这里是两个反斜杠\\*/
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "${default}"
}
],
"version": 4
}
3、配置step3
编辑jasks.json文件,
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "task g++",
"command": "E:\\C++\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-L","E:\\Anaconda3\\libs\\*",#这里改为自己python库文件夹路径
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I",
"E:\\Anaconda3\\include",#这里改为自己python的include路径
"-std=c++17"
],
"options": {
"cwd": "E:\\C++\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
},
{
"type": "cppbuild",
"label": "C/C++: cpp.exe 生成活动文件",
"command": "E:\\C++\\mingw64\\bin\\cpp.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
]
}
调用python
python测试代码
test.py
def hello():
print("hello")
main.cpp
#include "E:\\Anaconda3\\include\\Python.h"
//#include "Python.h"
#include <iostream>
#include <stdio.h>
#include <windows.h>
using namespace std;
int main()
{
Py_SetPythonHome(L"E:\\Anaconda3");
Py_Initialize(); //初始化python解释器,告诉编译器要用的python编译器
PyRun_SimpleString("import test"); //调用python文件
PyRun_SimpleString("hell.hello()"); //调用上述文件中的函数
Py_Finalize(); //结束python解释器,释放资源
system("Pause");
return 0;
}
结果
参考文章
在VS2019和VScode中配置C++调用python接口 Vscode 使用 C++ 调用python VsCode安装和配置c/c++环境(超完整,小白专用)
|