1、介绍##
Linux
下默认系统自带python2.6
的版本,这个版本被系统很多程序所依赖,所以不建议删除,如果使用最新的Python3
那么我们知道编译安装源码包和系统默认包之间是没有任何影响的,所以可以安装python3
和python2
共存。
2、准备##
首先去 python
官网下载 python3
的源码包。
网址:https://www.python.org/
进去之后点击导航栏的 Downloads
,也可以鼠标放到 Downloads
上弹出菜单选择 Source code
,就是源码包的意思,这里选择最新版本3.5.1,当然下面也有很多其他历史版本,点进去之后页面下方可以看到下载链接,包括源码包、Mac OSX 安装包、Windows 的安装包。
3、安装##
tar -xvzf Python-3.5.3.tgz #解压文件
cd Python-3.5.3/ #进入目录
./configure --prefix=/usr/local/python-3.7/ --enable-optimizations #添加配置
make #编译源码(看下边重点)
make install #执行安装
重点
:执行完 make
命令以后 如果有以下内容,代表有部分模块安装失败 openssl
必须的模块,需要重新找到原因。
Python build finished successfully!
The necessary bits to build these optional modules were not found:
_hashlib _ssl _uuid
To find the necessary bits, look in setup.py in detect_modules() for the module's name.
The following modules found by detect_modules() in setup.py, have been
built by the Makefile instead, as configured by the Setup files:
_abc atexit pwd
time
Could not build the ssl module!
Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host().
LibreSSL 2.6.4 and earlier do not provide the necessary APIs, https://github.com/libressl-portable/portable/issues/381
解决方案:
https://www.cnblogs.com/minglee/p/9232673.html
# 安装 openssl-devel
sudo yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
make clean
make && make install
整个过程大约 5-10 分钟,安装成功之后,安装目录就在 /usr/local/python
系统中原来的 python
在 /usr/bin/python
,通过 ls -l
可以看到,python
是一个软链接,链接到本目录下的python2.6
我们可以把这个删除,也可以新建一个 python3
的软链接,只不过执行时 python
要改成 python3
,或者python
脚本头部声明要改为 #!/usr/bin/python3
这里为了方便建议先重命名一下,然后建立个软链接就可以了,之前的程序头部也不用更改:
ln -s /usr/local/python/bin/python3 /usr/bin/python3 #建立软链接
ln -s /usr/local/python/bin/pip3 /usr/bin/pip3 # 建立 pip3 命令
这样就建立好了,就相当于调用 python3
,实际上 python3
也是个软链接,链接到 python3.5.1
,这个多次链接其实不影响,主要是为了版本升级更加方便,不用改版本号了
python3
新版本的安装就是这些,不用卸载旧版本,新版本同样正常使用。