- Python程序设计:编程基础、Web开发及数据分析
- 李辉 刘洋编著
- 930字
- 2025-02-15 22:57:27
1.3 安装Python集成开发环境
1.3.1 Linux环境下安装Python开发环境
1.查看Python安装版本
默认情况下,Linux会自带安装好的Python开发环境,可以通过python--version命令查看Python的安装版本,如图1-41所示。可以看到,Linux中已经自带了Python 2.7.5。

图1-41 Linux环境下查看Python版本
再次运行Python命令后就可以使用Python命令窗口了(〈Ctrl+D〉退出Python命令窗口)。
2.查看Linux默认安装的Python位置
通过ls命令查看Linux默认的Python安装位置,如图1-42所示。

图1-42 查看Linux默认安装的Python位置
可以看到/usr/bin/python和/usr/bin/python2都是软链接,/usr/bin/python指向/usr/bin/python2,而/usr/bin/python2最终又指向/usr/bin/python2.7。所以运行python/python2/python2.7是一样的。
3.安装Python 3
由于Linux自带的Python版本较老,所以需要自行安装Python 3。
(1)下载安装包
打开https://www.python.org/downloads/source/,找到对应版本(以Python 3.6.5为例)的安装包并下载(下载文件为:Python-3.6.5.tgz),如图1-43所示。

图1-43 Python下载页面
(2)解压安装包文件
执行tar-zxvf Python-3.6.5.tgz命令,将安装包文件解压到当前目录,如图1-44所示。

图1-44 Python安装包管理
(3)准备编译环境
执行如下命令安装Python需要的依赖。

(4)编译安装
执行cd Python-3.6.5命令进入解压后的Python-3.6.5目录下,依次执行如下三个命令。

其中--prefix是Python的安装目录,同时安装了setuptools和pip工具。
(5)创建软链接
Linux已经安装了Python 2.7.5,而且不能将它删除,如果删除,系统可能会出现问题。接下来只需要按照与Python 2.7.5相同的方式为Python 3.6.5创建一个软链接即可,可以把软链接放到/usr/local/bin目录下,如图1-45所示。

图1-45 Python创建一个软链接
此时,在命令窗口运行python3,如图1-46所示。Python3即安装成功。当然此时还是可以使用Python 2.7.5版本(运行python/python2/python2.7即可)。

图1-46 Python3命令行运行
(6)配置环境变量
执行vi~/.bash_profile,打开配置文件,添加如下配置。

保存退出(:wq),执行source~/.bash_profile命令使配置生效。
至此,Python 3开发环境安装完成。
1.3.2 Mac OS下安装Python开发环境
Mac OS操作系统环境下安装Python开发环境的步骤如下。
1)通过命令行终端查看Mac自带的Python,可以看到是2.7.10的版本。具体命令为:python。
2)查看Python 3的安装包。具体命令为:brew search python3。
3)安装Python 3。具体命令为:brew install python3。可以看到Python 3的实际安装目录是/usr/local/Cellar/python3/3.7.2。
4)打开配置文件并写入Python的外部环境变量。具体命令为:open~/.bash_profile export PATH=${PATH}:/usr/local/Cellar/python3/3.7.2/bin。
5)把Python系统环境变量重命名为:python。具体命令为:alias python=”/usr/local/Cellar/python3/3.7.2/bin/python3.7”。
6)让配置文件生效。具体命令为:source~/.bash_profile。
7)使用python命令查看Python开发环境是否成功。具体命令为:bin python。
8)若Python开发环境未设置成功,使用which python命令查看此时Python的位置是否正确,若不正确重复步骤3)、4)、5)、6)。