Linux下默认系统自带Python2.X的版本,这个版本被系统很多程序所依赖,所以不建议删除,如果使用最新的Python3那么我们知道编译安装源码包和系统默认包之间是没有任何影响的,所以可以安装Python3和Python2共存。
1、下载Linux平台的Python3.x的安装包(本文测试安装下载的是Python 3.7版本)
[linuxidc@localhost Linux公社 ]$ wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz
2、解压python3.6安装包
[linuxidc@localhost Linux公社 ]$ tar xf Python-3.7.2.tgz
[linuxidc@localhost Linux公社 ]$ ll
总用量 22368
drwxrwxr-x 2 linuxidc linuxidc 30 2月 23 17:24 Linux公社
drwxr-xr-x 18 linuxidc linuxidc 4096 12月 24 11:41 Python-3.7.2
-rwxrw-rw- 1 linuxidc linuxidc 22897802 2月 23 19:04 Python-3.7.2.tgz
3、安装Python3.7,默认安装路径为/usr/local
--prefix=PREFIX install architecture-independent files in PREFIX
[/usr/local]
#安装报错,缺失依赖库
[linuxidc@localhost Python-3.7.2]$ ./configure
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... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /usr/bin/grep
checking for a sed that does not truncate output... /usr/bin/sed
checking for --with-cxx-main=<compiler>... no
checking for g++... no
.......
如果您想要一个具有稳定优化的发布版本(PGO等),
请运行./configure --enable-optimizations
#如果有问题请安装依赖库
[linuxidc@localhost Python-3.7.2]$ yum install gcc
#安装python3.7
[linuxidc@localhost Python-3.7.2]$ make && make install
#make && make install 报错
zipimport.ZipImportError: can't decompress data; zlib not available
make: *** [install] Error 1
#安装zlib-devel
[root@localhost Python-3.7.2]# yum install zlib-devel
如果出现:
ModuleNotFoundError: No module named '_ctypes'
make: *** [install] 错误 1
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
#yum install libffi-devel -y 《-安装这个就能解决
#重新安装
[root@localhost Python-3.7.2]# make && make install
4、Python3.7已安装成功,在/usr/local/bin下生成命令python3
[linuxidc@localhost linuxidc.com]$ ll /usr/local/bin/
总用量 27376
lrwxrwxrwx 1 root root 8 2月 23 19:25 2to3 -> 2to3-3.7
-rwxr-xr-x 1 root root 101 2月 23 19:25 2to3-3.7
lrwxrwxrwx 1 root root 8 2月 23 19:25 pydoc3 -> pydoc3.7
-rwxr-xr-x 1 root root 84 2月 23 19:25 pydoc3.7
lrwxrwxrwx 1 root root 9 2月 23 19:25 python3 -> python3.7
-rwxr-xr-x 2 root root 13979432 2月 23 19:23 python3.7
lrwxrwxrwx 1 root root 17 2月 23 19:25 python3.7-config -> python3.7m-config
-rwxr-xr-x 2 root root 13979432 2月 23 19:23 python3.7m
-rwxr-xr-x 1 root root 3097 2月 23 19:25 python3.7m-config
lrwxrwxrwx 1 root root 16 2月 23 19:25 python3-config -> python3.7-config
lrwxrwxrwx 1 root root 10 2月 23 19:25 pyvenv -> pyvenv-3.7
-rwxr-xr-x 1 root root 441 2月 23 19:25 pyvenv-3.7
lrwxrwxrwx 1 root root 6 2月 22 19:13 stream -> magick
5、使用Python3命令查看版本