教程描述
方法1:Windows 7
+Microsoft Visual Studio community 2015
+python2.7+opencv_python-3.4.0
与
方法2:Windows 7
+Microsoft Visual Studio community 2015
+Anaconda3-5.0.0(python3.6)+opencv_python-3.4.0
一、工具下载/安装
visual studio 2015 下载安装汉化
参考
Anaconda3安装(自带python3.6)与opencv_python-3.4.0导入
二、方法/步骤
1. VS2015添加Python环境
Visual Studio集成了Python Tools for Visual Studio插件。我们要在自定义安装的时候点选安装

如果你已经安装过了,进入控制面板-程序与功能-卸载程序,选择VS2015更改






点击确定

到这里分为两种方法
2. 方法1:使用VS本身功能
直接点击运行,就会出现下面这个界面,然后点击下载Python2.7
我没有下最新版,点击Python homepage

点击Downloads



点击保存,确定保存位置

要等待一会,完成后,点击运行


一般选择所有人都能使用,点击Next

选择安装位置,点击Next

点击Next


完成,点击Finish
这时在VS2015的解决方案资源管理器中,Python Environments下有了Python2.7

右键点击Python Environments,选择Add/Remove Python Environments...

勾选Python2.7,点击OK


输入
print "Hello world!"
a=1/2
print a
b=1.0/2.0
print b
运行成功

Python环境能使用
补充:VS2017 安装的时候就已经安装了python,版本3.6,默认路径在C:\ProgramFiles (x86)\Microsoft Visual Studio\Shared\Python36_64\,所以没有再单独安装python
看解决方案资源管理器中,Python Environments下的Python2.7的包最开始是只有pip和setuptools

在解决方案资源管理器中,右键点击Python Environments,选择**View All Python Environments...进入

把Overview选项改为pip

在Search PypI and installed packages中输入需要安装的package,共三个: opencv-python,numpy, scipy,然后基本都选择第一个结果“pip install ……”







在源文件中输入
import cv2
import numpy as np
print(cv2.__version__)
image = cv2.imread("pic.jpg")
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("Over theClouds", image)
cv2.imshow("Over theClouds - gray", gray_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
将图片pic.jpg复制到工程目录下面,与源码位于同一目录
运行,结果如下



结束
3. 方法2:使用第三方的Anaconda管理Python解释器与包
按照前文链接教程操作后,在VS2015的解决方案资源管理器中,右键点击Python Environments,选择View All Python Environments...


点击+Custom...,查看overview选项,如果之前没有创建环境,下面就空空如也

查看Configure选项,依次填入环境名称,Anaconda下Python解释器路径前缀,Python.exe路径,Pythonw.exe路径及库的路径,选择Python3.5(我实际上是Python3.6版本,但可以使用),64位。之后点击Apply

你也可以在VS中点击Tools--->Python Tools--->Python Environments会弹出上面的菜单栏
添加环境

点击OK,发现环境已经加入

在源文件中输入
import cv2
import numpy as np
print ("Hello world!")
a=1/2
print (a)
b=1.0/2.0
print (b)
print(cv2.__version__)
image = cv2.imread("pic.jpg")
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("Over theClouds", image)
cv2.imshow("Over theClouds - gray", gray_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
运行,结果如下


如果你要在多环境中切换,右键点击项目名-点击属性,也可以在工具栏上点击项目-属性修改


visual studio 2015 搭建python开发环境,python入门到精通[三] - 请叫我头头哥 - 博客园
Python 入门到精通(3):VS 2015 搭建开发环境 - Python - 伯乐在线
Python | 如何安装VS2015 Python插件(PTVS)_百度经验
Python | 开发环境IDE VS2015配置_百度经验
在 Visual Studio 中管理 Python 环境 | Microsoft Docs
网友评论