且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Linux安装Python3

更新时间:2022-06-09 01:00:33

1、查看Python版本
[root@93 ~]# python -V  #注意,是大写的V
Python 2.7.5

2、安装Python可能需要依赖的库


 yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel 


3、下载Python3的压缩包


到Python官网下载 https://www.python.org

wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0b5.tgz

4、解压缩Python压缩包


[root@Zabbix93 ~]# tar -zxf Python-3.7.0b5.tgz 
[root@Zabbix93 ~]# cd Python-3.7.0b5
[root@Zabbix93 Python-3.7.0b5]# ls
aclocal.m4    configure     Grammar     Lib      Mac              Modules  PC        pyconfig.h.in  setup.py
config.guess  configure.ac  Include     LICENSE  Makefile.pre.in  Objects  PCbuild   Python         Tools
config.sub    Doc           install-sh  m4       Misc             Parser   Programs  README.rst


5、编译安装Python3.7


./configure --prefix=/usr/local/ //安装到/usr/local目录

我这里提示报错,原因:缺少编译器。解决方法:安装gcc


[root@Zabbix93 Python-3.7.0b5]# ./configure --prefix=/usr/local/
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for python3.7... no
checking for python3... no
checking for python... python
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... checking for --without-gcc... no
checking for --with-icc... no
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/root/Python-3.7.0b5':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details




[root@Zabbix93 Python-3.7.0b5]# yum install -y gcc
[root@Zabbix93 Python-3.7.0b5]# ./configure --prefix=/usr/local/
[root@Zabbix93 Python-3.7.0b5]# make
[root@Zabbix93 Python-3.7.0b5]# make altinstall //此处不能用install安装,因为install不区分版本,会出现多版本混乱的问题



6、更改Python默认链接
 python3.7程序的执行文件:/usr/local/bin/python3.7
  python3.7应用程序目录:/usr/local/lib/python3.7
  pip7的执行文件:/usr/local/bin/pip3.7
  pyenv3的执行文件:/usr/local/bin/pyenv-3.7


更改Python默认链接:

# cd/usr/bin
# mv  python python.backup
# ln -s /usr/local/bin/python3.7 /usr/bin/python
# ln -s /usr/local/bin/python3.7 /usr/bin/python3