VS2010下,C++调用python的环境,基于Python3.6.8-win32位

VS2010下,C++调用python的环境,基于Python3.6.8-win32位

前言

配置Theano + Scipy + Python + VS2010 C++ 开发环境。
由于单位开发环境较老,使用的是VS2010的Win32平台开发,因此考虑到32位与64位不能兼容,使c++能正确调用Python,踩遍CSDN上的坑,若开发环境为64位基本操作差不多,不过时刻牢记Python位数与C++环境位数应该对应相同才能互相调用。

总结如下工作配置环境流程:

  • Miniconda的安装
  • Miniconda虚拟环境配置
  • VS2010环境配置(基于Win32平台)

Miniconda的安装

之所以选择Miniconda而不是Anaconda,不外乎是32位的考虑,安装32位miniconda对应python版本即为32位,不用过多考虑其他问题,链接:Miniconda官网

下载安装时不推荐添加到环境变量,这样可以使conda的环境与现有Python环境分开,虽然不方便使用,但是便于管理。安装完成会有对应Anaconda Prompt(miniconda)的命令行快捷方式生成。

Miniconda虚拟环境配置

打开Prompt命令行,输入命令创建新的python环境:
conda create -n your_env_name python=3.6.8
不是所有Python都有32位版本,华为开源Python镜像可以查询对应版本是否有32位(结尾是Win32即为32位)。

如果觉得安装慢,需要添加源,这里选择Anaconda清华源

  1. 搜索电脑里有没有 .condarc 文件,有则点开编辑;
  2. 无则命令行输入conda config --set show_channel_urls yes即可生成对应文件;
  3. 文件打开粘贴如下内容即可。
channels:
  - defaults
show_channel_urls: true
channel_alias: https://mirrors.tuna.tsinghua.edu.cn/anaconda
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

安装会加速很多很多。创建完成后安装想要的包即可,这里为了神经网络安装:
激活环境:conda activate your_env_name
安装Scipy:conda install scipy
安装机器学习库Theano:conda install theano

VS2010环境配置(基于Win32平台)

本机Minicodna安装在E:\miniconda下,虚拟环境则在该目录的env目录下,假设创建的虚拟环境为py36,那么对应虚拟环境路径为 E:\miniconda\envs\py36

公司环境为VS2010,Win32,创建项目选对应的:
Win32控制台应用程序
创建成功后上方应显示 Debug 和 Win32。
在这里插入图片描述
右键项目,点击最下方属性,弹出属性页
① 添加附加包目录:E:\miniconda\envs\py36\include;
在这里插入图片描述
②添加附加库目录: E:\miniconda\envs\py36\libs;
在这里插入图片描述
③添加Python36_d.lib
debug模式下需要python36.lib的debug版本python36_d.lib,复制粘贴重命名为python36_d.lib即可。
在这里插入图片描述
④复制Python36.dll
python36.lib链接着python36.dll,经常会因为找到了lib文件找不到dll文件报错,复制python环境目录下的(既E:\miniconda\envs\py36)python36.dll ,粘贴到项目的debug目录下即可。
⑤再不放心
现在在CPP中#include "Python.h""已经不会找不到库,为保险起见,在程序中再设置个PythonHome,本机中数据类型不一样导致不能直接传Char[ ]进Py_SetPythonHome() , 因此定义个wchar_t[]才能正确传递地址进去,不会报错。

#include "stdafx.h"
#include "Python.h"

int main()
{
	wchar_t wszStr[] = L"E:\\miniconda\\envs\\py36";

	Py_SetPythonHome(wszStr);
	Py_Initialize();

	PyRun_SimpleString("import os");
	PyRun_SimpleString("import sys");
	PyRun_SimpleString("sys.path.append('.')");

	PyRun_SimpleString("from hello import *");
	PyRun_SimpleString("print_hello()");

	Py_Finalize();

	system("pause");
	return 0;
}

再有其他错误基本上也是python少个包什么的,直接安装就好,debug弹出的窗口会告诉问题出在哪里。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值