
1.2 Python开发环境
集成开发环境(Integrated Development Environment,IDE)是提供程序开发环境的应用程序,一般包括代码编辑、程序编译、程序调试和图形交互界面等工具,是集成了代码编辑功能、语法分析功能、编译功能、调试功能等一体化的开发软件服务套。
Python IDE是Python编程必须使用的开发工具,IDE的选择对Python编程有很大的影响,因此选择合适的Python IDE对于程序开发十分重要,目前常用的Python IDE有10多种,其中使用较多的有以下几种。(1)Python官网下载的安装包自带的IDE;(2)Python专业集成开发环境PyCharm,有两个版本,一个是免费的社区版本,另一个是面向企业开发者的专业版本;(3)Python的人工智能、科学计算环境Anaconda,其中包含了大量的科学包,提供包管理与环境管理的功能。
1.2.1 PyCharm的下载和安装
PyCharm是专业的面向Python的全功能集成开发环境。PyCharm主页:http://www.jetbrains. com/pycharm/,如图1-7所示。

图1-7 PyCharm主页
(1)在主页单击DOWNLOAD NOW按钮,弹出下载界面,如图1-8所示。

图1-8 PyCharm下载界面
(2)PyCharm目前有两个版本:付费的专业版和免费的社区版,这里选择免费的社区版。下载的文件名为“pycharm-community-2019.3.3.exe”,双击文件开始安装,如图1-9所示。

图1-9 安装PyCharm
(3)安装完成PyCharm以后,在程序开始菜单出现PyCharm启动项“JetBrains>JetBrains PyCharm Community”,单击JetBrains PyCharm Community选项即可启动,如图1-10所示。

图1-10 启动PyCharm
(4)启动PyCharm,在弹出的窗口中进行设置,这是第一次启动,选择Do not import settings选项,单击OK按钮,如图1-11所示。

图1-11 设置选项
(5)PyCharm启动后的主界面如图1-12所示。

图1-12 PyCharm主界面
1.2.2 PyCharm的使用
在PyCharm中新建Python程序的步骤如下。
(1)在PyCharm的主界面中选择“File>New Project”菜单命令以新建项目,如图1-13所示。

图1-13 新建项目
(2)选中新建的项目,单击鼠标右键,在弹出菜单中选择“New>Python File”菜单命令,新建Python文件,如图1-14所示。

图1-14 新建Python文件
(3)在新建的Python文件中输入代码,单击鼠标右键,在弹出的菜单中选择菜单“Run‘t1’”菜单命令,执行Python代码,如图1-15所示。

图1-15 运行Python代码
在PyCharm中执行调试Python程序有两种方式:运行文件方式(上面介绍的方法)和交互方式。在PyCharm的主界面中选择窗口Python Console,在此窗口中可以交互执行Python源代码,如图1-16所示。

图1-16 Python Console窗口
PyCharm可以进行个性化的设置,选择“File>Settings”菜单命令,打开Settings(设置)窗口,如图1-17所示。在左侧栏中选择“Project>Project Interpreter”选项,在右侧即可设置Python的项目解释器Project Interpreter。

图1-17 PyCharm设置窗口
(4)在Python开发的过程中,经常用到第三方模块,可以在Settings窗口中进行操作。方法是在Settings窗口右侧栏中单击按钮,弹出如图1-18所示的窗口,在左侧选择安装的模块,例如numpy模块,然后单击下方的Install Package按钮开始安装。
安装完成以后,在Settings窗口中即可看到已经安装的模块numpy,如图1-19所示。

图1-18 安装Python第三方模块

图1-19 安装的第三方模块
1.2.3 树莓派Python IDLE的使用
Python语言由于具有很强的可读性、可移植性,且易于维护,以及包含丰富多样的第三方扩展组件(例如数据库、数学计算、数据爬取、数据挖掘、机器视觉、人工智能、深度学习框架等方面的Python组件),因此,随着嵌入式处理系统的功能逐渐增强,Python也广泛地应用到了嵌入式系统中。
树莓派(Raspberry Pi)是为学生学习计算机编程教育而设计的,鼓励计算机爱好者动手开发相关的软硬件应用,且只有信用卡大小的微型电脑。虽然树莓派的体积小,但是它的应用潜力无限,受众多计算机发烧友和创客的追捧,基于树莓派可以设计搭建出多种DIY产品,例如机器人、智能音箱、控制系统、云服务器、仿生系统、人工智能系统等,如图1-20所示。

图1-20 树莓派
树莓派主要是用Python语言开发设计各种应用。Python允许树莓派实现各种丰富的应用,有了树莓派和Python,唯一受到限制的就是想象力了。可以用Python实现树莓派控制实时监控系统、自动驾驶机器人、实时感知机器人、人脸识别系统等。
树莓派主页:https://www.raspberrypi.org/,如图1-21所示,可以从主页中下载树莓派的操作系统。

图1-21 树莓派主页
完成树莓派操作系统的下载以后,还要下载树莓派系统烧录软件Win32 Disk Imager,下载网址:https://sourceforge.net/projects/win32diskimager/files/latest/download,如图1-22所示。

图1-22 Win32 Disk Imager下载页面
下载完成后,运行文件“win32diskimager-1.0.0-install.exe”进行安装,将映像文件“win32diskimager-1.0.0-install 2020-02-13-raspbian-buster-full.img”烧写到启动树莓派的Micro SD卡中。方法是将插有Micro SD卡的USB读卡器插入PC的USB接口,运行Win32 Disk Imager系统烧录软件,如图1-23所示。可以看到插入的Micro SD卡盘符位于“设备”下面,单击“写入”按钮开始烧入。

图1-23 Win32DiskImager运行界面
烧写完成后弹出对话框,单击OK按钮,如图1-24所示。

图1-24 烧写完成
树莓派的官方操作系统是一款基于Linux的操作系统,将烧写完成操作系统的Micro SD卡插入树莓派卡槽,加电启动树莓派系统,启动后的界面如图1-25所示。可以看到已经集成了两个Python的编程环境:Python 3(IDLE)和Thonny Python IDE。

图1-25 树莓派启动后的界面
在使用树莓派访问GPIO硬件之前,还需要安装Python的GPIO库。在安装GPIO库之前,要确保系统安装了Python-dev包,安装方法是在树莓派主界面启动一个控制台终端,执行命令:sudo apt-get install python3-dev。Python-dev包安装完成以后,即可安装树莓派Python的GPIO库,执行命令:sudo apt-get install python3-rpi.gpio,如图1-26所示。

图1-26 安装树莓派Python的GPIO库
树莓派安装其他Python库的方法类似,命令格式为“sudo apt-get install Python库”。