
1.3 下载和安装Python
1.3.1 下载Python
Python支持多平台,不同平台的安装和配置大致相同。本书基于Windows 10和Python 3.7构建Python开发平台。
【例1.1】 下载Python安装程序。
(1)打开Python官网Windows环境下载页面。在浏览器地址栏中输入“https://www.python.org/downloads/windows/”,按Enter键,如图1-1所示。

图1-1 下载Python
(2)下载Python安装程序。单击图1-1中的Windows x86-64 executable installer超链接,下载目前最新版本Python 3.7.0(64位)的安装程序python-3.7.0.exe(25MB)。
1.3.2 安装Python
Python的安装过程与其他Windows安装程序类似。
【例1.2】 安装Python应用程序。
(1)运行Python安装程序。双击下载的Windows格式的安装文件python-3.7.0-amd64.exe,打开安装程序向导。
(2)设定安装选项。根据安装向导安装Python,在定制Python的对话框中注意选中“Add Python 3.7 to PATH”复选框,如图1-2所示。

图1-2 设定Python安装选项
(3)安装程序。单击Install Now超链接,安装Python程序。
1.3.3 安装和管理Python扩展包
Python 3.4以后的版本包含pip和setuptools库。pip用于安装和管理Python扩展包;setuptools用于发布Python包。
在使用pip和setuptools前建议先更新到其最新版本。
pip的典型应用是从PyPI(Python Package Index)上安装Python第三方包。其命令行的基本语法如下。
(1)安装包的最新版本(例如SomeProject的最新版本)。

(2)安装包的某个版本。

(3)安装包的某个范围的版本(例如SomeProject的大于等于1小于2的版本)。

(4)安装包的某个兼容版本(例如SomeProject的兼容1.4.2的版本)。

(5)更新安装包(例如更新SomeProject到最新版本)。

说明:
(1)在Python的安装目录“Python37\Scripts”中还包含pip.exe、pip3.exe、pip3.7.exe,它们与上述基于pip模块安装包等价。例如可以使用命令行安装包:

(2)如果安装包时Python产生错误“[WinError 5]拒绝访问”,可以使用管理员权限打开命令提示符窗口进行安装,或使用-user安装到个人目录中。
【例1.3】 更新pip和setuptools包。
在Windows命令提示符窗口中输入命令行命令“python-m pip install-U pip setuptools”,以更新pip和setuptools包,如图1-3所示。

图1-3 更新pip和setuptools包
【例1.4】 安装NumPy包。Python扩展模块NumPy提供了数组和矩阵处理,以及傅立叶变换等高效的数值处理功能。
在Windows命令提示符窗口中输入命令行命令“python-m pip install NumPy”,以安装NumPy包,如图1-4所示。

图1-4 安装NumPy包
【例1.5】 安装Matplotlib包。Matplotlib是Python最著名的绘图库之一,提供了一整套和MATLAB相似的命令API,既适合交互式地进行制图,也可以作为绘图控件方便地嵌入GUI应用程序中。Matplotlib的具体应用将在本书第13章中详细介绍。
在Windows命令提示符窗口中输入命令行命令“python-m pip install Matplotlib”,以安装Matplotlib包,如图1-5所示。

图1-5 安装Matplotlib包