源码安装 python3

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了源码安装 python3相关的知识,希望对你有一定的参考价值。

Linux下默认系统自带python2.6的版本,这个版本被系统很多程序所依赖,所以不建议删除,如果使用最新的Python3那么我们知道编译安装源码包和系统默认包之间是没有任何影响的,所以可以安装python3和python2共存

首先去python官网下载python3的源码包,网址:https://www.python.org/

进去之后点击导航栏的Downloads,也可以鼠标放到Downloads上弹出菜单选择Source code,就是源码包的意思,点进去之后页面下方可以看到下载链接,包括源码包、Mac OSX安装包、Windows的安装包

1 安装依赖包

[[email protected] ~]# yum -y install wget gcc automake zlib-devel openssl-devel xz
[[email protected] ~]# yum install readline-devel.x86_64    #解决python3退格功能

2 编译安装

[[email protected] ~]# wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz

[[email protected] ~]# tar xf Python-3.6.5.tar.xz  && cd Python-3.6.5

[[email protected] ~]# ./configure 
[[email protected] ~]# make && make install

3 把python3作为默认python

[[email protected] ~]# python --version
Python 2.6.6

[[email protected] ~]# which python
/usr/bin/python

[[email protected] ~]# which python3
/usr/local/bin/python3

[[email protected] ~]# ll /usr/bin/python*
-rwxr-xr-x 2 root root 9728 8月  18 2016 /usr/bin/python
lrwxrwxrwx 1 root root    6 12月 11 14:14 /usr/bin/python2 -> python
-rwxr-xr-x 2 root root 9728 8月  18 2016 /usr/bin/python2.6

[[email protected] ~]# rm -f /usr/bin/python /usr/bin/python2
[[email protected] ~]# ln -s /usr/local/bin/python3 /usr/bin/python
[[email protected] ~]# ln -s /usr/bin/python2.6 /usr/bin/python2

验证:
[[email protected] ~]# python --version
Python 3.6.5

4 指定yum调用的python版本

4.1 修改/usr/bin/yum

[[email protected] ~]# vim ?/usr/bin/yum
1 #!/usr/bin/python
  改为
1 #!/usr/bin/python2.6          # 改为python默认版本,CentOS 7.*为python2.7

或:

[[email protected] ~]# ansible all -m shell -a "sed -i ‘1s#python$#python2.6#g‘ /usr/bin/yum“
[[email protected] ~]# ansible all -m shell -a "head -1 /usr/bin/yum”

修改软链接

[[email protected] ~]# ll /usr/bin/python*
lrwxrwxrwx 1 root root   22 1月   4 09:41 /usr/bin/python -> /usr/local/bin/python3
lrwxrwxrwx 1 root root   18 1月   4 09:41 /usr/bin/python2 -> /usr/bin/python2.6
-rwxr-xr-x 1 root root 9728 8月  18 2016 /usr/bin/python2.6
[[email protected] ~]#

4.2 CentOS 7.*

需要修改 /usr/libexec/urlgrabber-ext-down文件

[[email protected] ~]# ansible all -m shell -a "sed -i ‘1s#python$#python2.7#g‘ /usr/libexec/urlgrabber-ext-down"
[[email protected] ~]# ansible all -m shell -a "head -1 /usr/libexec/urlgrabber-ext-down"

还需要修改/usr/bin/yum-config-manager

[[email protected] ~]# ansible all -m shell -a "sed -i ‘1s#python$#python2.7#g‘ /usr/bin/yum-config-manager"
[[email protected] ~]# ansible all -m shell -a "head -1 /usr/bin/yum-config-manager"

附:

mv /usr/local/lib/{,bak_}libisl.so.10.1.1-gdb.py

提示:Ignoring ensurepip failure: pip 7.1.2 requires SSL/TLS
这是原因没有安装或升级openssl:

END

以上是关于源码安装 python3的主要内容,如果未能解决你的问题,请参考以下文章

scrapy主动退出爬虫的代码片段(python3)

scrapy按顺序启动多个爬虫代码片段(python3)

Ubuntu 下安装最新 Python3.10 教程 (源码安装)

Ubuntu 下安装最新 Python3.10 教程 (源码安装)

源码安装 python3

python3.6.5源码安装