CentOS7下安装配置python3

CentOS7下安装配置python3

实验环境

操作系统:CentOS Linux release 7.3.1611 (Core)

IP: 192.168.230.142
  • 1
  • 2
  • 3

下载插件

根据官网提示,在centos下安装python3版本需要提前安装如下插件:
openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel

[root@localhost ~]# yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel  -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.sohu.com
 * extras: mirrors.sohu.com
 * updates: mirrors.sohu.com
Resolving Dependencies
--> Running transaction check
---> Package bzip2-devel.x86_64 0:1.0.6-13.el7 will be installed
---> Package expat-devel.x86_64 0:2.1.0-10.el7_3 will be installed
---> Package gdbm-devel.x86_64 0:1.10-8.el7 will be installed
---> Package openssl-devel.x86_64 1:1.0.2k-8.el7 will be installed
--> Processing Dependency: zlib-devel(x86-64) for package: 1:openssl-devel-1.0.2k-8.el7.x86_64
--> Processing Dependency: krb5-devel(x86-64) for package: 1:openssl-devel-1.0.2k-8.el7.x86_64
---> Package readline-devel.x86_64 0:6.2-10.el7 will be installed
--> Processing Dependency: ncurses-devel for package: readline-devel-6.2-10.el7.x86_64
---> Package sqlite-devel.x86_64 0:3.7.17-8.el7 will be installed
--> Running transaction check
---> Package krb5-devel.x86_64 0:1.15.1-8.el7 will be installed
--> Processing Dependency: libverto-devel for package: krb5-devel-1.15.1-8.el7.x86_64
--> Processing Dependency: libselinux-devel for package: krb5-devel-1.15.1-8.el7.x86_64
--> Processing Dependency: libcom_err-devel for package: krb5-devel-1.15.1-8.el7.x86_64
--> Processing Dependency: keyutils-libs-devel for package: krb5-devel-1.15.1-8.el7.x86_64
---> Package ncurses-devel.x86_64 0:5.9-14.20130511.el7_4 will be installed
---> Package zlib-devel.x86_64 0:1.2.7-17.el7 will be installed
--> Running transaction check
---> Package keyutils-libs-devel.x86_64 0:1.5.8-3.el7 will be installed
---> Package libcom_err-devel.x86_64 0:1.42.9-10.el7 will be installed
---> Package libselinux-devel.x86_64 0:2.5-11.el7 will be installed
--> Processing Dependency: libsepol-devel(x86-64) >= 2.5-6 for package: libselinux-devel-2.5-11.el7.x86_64
--> Processing Dependency: pkgconfig(libsepol) for package: libselinux-devel-2.5-11.el7.x86_64
--> Processing Dependency: pkgconfig(libpcre) for package: libselinux-devel-2.5-11.el7.x86_64
---> Package libverto-devel.x86_64 0:0.2.5-4.el7 will be installed
--> Running transaction check
---> Package libsepol-devel.x86_64 0:2.5-6.el7 will be installed
---> Package pcre-devel.x86_64 0:8.32-17.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===================================================================================================================================
 Package                             Arch                   Version                                  Repository               Size
===================================================================================================================================
Installing:
 bzip2-devel                         x86_64                 1.0.6-13.el7                             base                    218 k
 expat-devel                         x86_64                 2.1.0-10.el7_3                           base                     57 k
 gdbm-devel                          x86_64                 1.10-8.el7                               base                     47 k
 openssl-devel                       x86_64                 1:1.0.2k-8.el7                           base                    1.5 M
 readline-devel                      x86_64                 6.2-10.el7                               base                    138 k
 sqlite-devel                        x86_64                 3.7.17-8.el7                             base                    104 k
Installing for dependencies:
 keyutils-libs-devel                 x86_64                 1.5.8-3.el7                              base                     37 k
 krb5-devel                          x86_64                 1.15.1-8.el7                             base                    266 k
 libcom_err-devel                    x86_64                 1.42.9-10.el7                            base                     31 k
 libselinux-devel                    x86_64                 2.5-11.el7                               base                    186 k
 libsepol-devel                      x86_64                 2.5-6.el7                                base                     74 k
 libverto-devel                      x86_64                 0.2.5-4.el7                              base                     12 k
 ncurses-devel                       x86_64                 5.9-14.20130511.el7_4                    updates                 712 k
 pcre-devel                          x86_64                 8.32-17.el7                              base                    480 k
 zlib-devel                          x86_64                 1.2.7-17.el7                             base                     50 k

Transaction Summary
===================================================================================================================================
Install  6 Packages (+9 Dependent packages)

Total download size: 3.9 M
Installed size: 9.1 M
Downloading packages:
(1/15): expat-devel-2.1.0-10.el7_3.x86_64.rpm                                                               |  57 kB  00:00:00     
(2/15): keyutils-libs-devel-1.5.8-3.el7.x86_64.rpm                                                          |  37 kB  00:00:00     
(3/15): gdbm-devel-1.10-8.el7.x86_64.rpm                                                                    |  47 kB  00:00:00     
(4/15): libcom_err-devel-1.42.9-10.el7.x86_64.rpm                                                           |  31 kB  00:00:00     
(5/15): libverto-devel-0.2.5-4.el7.x86_64.rpm                                                               |  12 kB  00:00:00     
(6/15): bzip2-devel-1.0.6-13.el7.x86_64.rpm                                                                 | 218 kB  00:00:00     
(7/15): libsepol-devel-2.5-6.el7.x86_64.rpm                                                                 |  74 kB  00:00:00     
(8/15): libselinux-devel-2.5-11.el7.x86_64.rpm                                                              | 186 kB  00:00:00     
(9/15): krb5-devel-1.15.1-8.el7.x86_64.rpm                                                                  | 266 kB  00:00:00     
(10/15): ncurses-devel-5.9-14.20130511.el7_4.x86_64.rpm                                                     | 712 kB  00:00:00     
(11/15): readline-devel-6.2-10.el7.x86_64.rpm                                                               | 138 kB  00:00:00     
(12/15): sqlite-devel-3.7.17-8.el7.x86_64.rpm                                                               | 104 kB  00:00:00     
(13/15): zlib-devel-1.2.7-17.el7.x86_64.rpm                                                                 |  50 kB  00:00:00     
(14/15): pcre-devel-8.32-17.el7.x86_64.rpm                                                                  | 480 kB  00:00:00     
(15/15): openssl-devel-1.0.2k-8.el7.x86_64.rpm                                                              | 1.5 MB  00:00:00     
-----------------------------------------------------------------------------------------------------------------------------------
Total                                                                                              2.9 MB/s | 3.9 MB  00:00:01     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
  Installing : libsepol-devel-2.5-6.el7.x86_64                                                                                1/15 
  Installing : ncurses-devel-5.9-14.20130511.el7_4.x86_64                                                                     2/15 
  Installing : pcre-devel-8.32-17.el7.x86_64                                                                                  3/15 
  Installing : libselinux-devel-2.5-11.el7.x86_64                                                                             4/15 
  Installing : libcom_err-devel-1.42.9-10.el7.x86_64                                                                          5/15 
  Installing : libverto-devel-0.2.5-4.el7.x86_64                                                                              6/15 
  Installing : keyutils-libs-devel-1.5.8-3.el7.x86_64                                                                         7/15 
  Installing : krb5-devel-1.15.1-8.el7.x86_64                                                                                 8/15 
  Installing : zlib-devel-1.2.7-17.el7.x86_64                                                                                 9/15 
  Installing : 1:openssl-devel-1.0.2k-8.el7.x86_64                                                                           10/15 
  Installing : readline-devel-6.2-10.el7.x86_64                                                                              11/15 
  Installing : expat-devel-2.1.0-10.el7_3.x86_64                                                                             12/15 
  Installing : sqlite-devel-3.7.17-8.el7.x86_64                                                                              13/15 
  Installing : gdbm-devel-1.10-8.el7.x86_64                                                                                  14/15 
  Installing : bzip2-devel-1.0.6-13.el7.x86_64                                                                               15/15 
  Verifying  : bzip2-devel-1.0.6-13.el7.x86_64                                                                                1/15 
  Verifying  : zlib-devel-1.2.7-17.el7.x86_64                                                                                 2/15 
  Verifying  : gdbm-devel-1.10-8.el7.x86_64                                                                                   3/15 
  Verifying  : keyutils-libs-devel-1.5.8-3.el7.x86_64                                                                         4/15 
  Verifying  : libverto-devel-0.2.5-4.el7.x86_64                                                                              5/15 
  Verifying  : libselinux-devel-2.5-11.el7.x86_64                                                                             6/15 
  Verifying  : krb5-devel-1.15.1-8.el7.x86_64                                                                                 7/15 
  Verifying  : libcom_err-devel-1.42.9-10.el7.x86_64                                                                          8/15 
  Verifying  : pcre-devel-8.32-17.el7.x86_64                                                                                  9/15 
  Verifying  : ncurses-devel-5.9-14.20130511.el7_4.x86_64                                                                    10/15 
  Verifying  : sqlite-devel-3.7.17-8.el7.x86_64                                                                              11/15 
  Verifying  : libsepol-devel-2.5-6.el7.x86_64                                                                               12/15 
  Verifying  : readline-devel-6.2-10.el7.x86_64                                                                              13/15 
  Verifying  : 1:openssl-devel-1.0.2k-8.el7.x86_64                                                                           14/15 
  Verifying  : expat-devel-2.1.0-10.el7_3.x86_64                                                                             15/15 

Installed:
  bzip2-devel.x86_64 0:1.0.6-13.el7          expat-devel.x86_64 0:2.1.0-10.el7_3        gdbm-devel.x86_64 0:1.10-8.el7           
  openssl-devel.x86_64 1:1.0.2k-8.el7        readline-devel.x86_64 0:6.2-10.el7         sqlite-devel.x86_64 0:3.7.17-8.el7       

Dependency Installed:
  keyutils-libs-devel.x86_64 0:1.5.8-3.el7        krb5-devel.x86_64 0:1.15.1-8.el7     libcom_err-devel.x86_64 0:1.42.9-10.el7   
  libselinux-devel.x86_64 0:2.5-11.el7            libsepol-devel.x86_64 0:2.5-6.el7    libverto-devel.x86_64 0:0.2.5-4.el7       
  ncurses-devel.x86_64 0:5.9-14.20130511.el7_4    pcre-devel.x86_64 0:8.32-17.el7      zlib-devel.x86_64 0:1.2.7-17.el7          

Complete!
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131

下载python

切换到download目录,下载最新的3.7版本python

[root@localhost ~]# cd /root/Downloads/
[root@localhost Downloads]# wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0a1.tgz
--2017-10-10 21:06:41--  https://www.python.org/ftp/python/3.7.0/Python-3.7.0a1.tgz
Resolving www.python.org (www.python.org)... 151.101.24.223, 2a04:4e42:6::223
Connecting to www.python.org (www.python.org)|151.101.24.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 21614110 (21M) [application/octet-stream]
Saving to: ‘Python-3.7.0a1.tgz100%[=========================================================================================>] 21,614,110  41.5KB/s   in 5m 24s 

2017-10-10 21:12:06 (65.2 KB/s) - ‘Python-3.7.0a1.tgz’ saved [21614110/21614110]

[root@localhost Downloads]# ls
Python-3.7.0a1.tgz
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

解压安装包并迁移至/usr/local

[root@localhost Downloads]# tar -zxvf Python-3.7.0a1.tgz
[root@localhost Downloads]# mv Python-3.7.0a1 /usr/local
  • 1
  • 2

安装python

进入解压好的Python安装包目录

[root@localhost Downloads]# cd /usr/local/Python-3.7.0a1/
[root@localhost Python-3.7.0a1]# ls
aclocal.m4    configure     Grammar     Lib      Makefile.pre.in  Objects  PCbuild        Python      Tools
config.guess  configure.ac  Include     LICENSE  Misc             Parser   Programs       README.rst
config.sub    Doc           install-sh  Mac      Modules          PC       pyconfig.h.in  setup.py
  • 1
  • 2
  • 3
  • 4
  • 5

执行自动配置文件

[root@localhost Python-3.7.0a1]# ./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
configure:

  By default, distutils will build C++ extension modules with "g++".
  If this is not intended, then set CXX on the configure command line.

checking for the platform triplet based on compiler characteristics... x86_64-linux-gnu
checking for -Wl,--no-as-needed... yes
checking for egrep... /usr/bin/grep -E
checking for ANSI C header files... yes
···
···
creating Modules/Setup
creating Modules/Setup.local
creating Makefile


If you want a release build with all stable optimizations active (PGO, etc),
please run ./configure --enable-optimizations
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

确认无误后,执行编译

[root@localhost Python-3.7.0a1]# make
  • 1

确认无误后,执行编译安装

[root@localhost Python-3.7.0a1]# make install
  • 1

此时我遭遇到问题,报错:

ModuleNotFoundError: No module named '_ctypes'
  • 1

经查阅资料后发现是由于3.7版本需要一个新的包libffi-devel,安装此包后再次进行编译安装即可

[root@localhost Python-3.7.0a1]# yum install libffi-devel -y
[root@localhost Python-3.7.0a1]# make install
  • 1
  • 2

安装完成后为了方便操作在/usr/bin路径下生成python3.7的软链接

[root@localhost Python-3.7.0a1]# ln -s /usr/local/Python-3.7.0a1/python /usr/bin/python3.7
  • 1

验证

直接使用root用户执行python3.7,可查看python状态

[root@localhost Python-3.7.0a1]# python3.7 -V
Python 3.7.0a1
  • 1
  • 2

注意事项

使用centos环境安装python3需要非常小心,不要轻易替换操作系统中自带的python2的任何设置信息,否则将导致部分重要功能(如yum)无法正常使用。
所以我在本实验中将软链接命名为python3.7,避免和原有的python相冲突。

以前遇到的坑

以前在centos下安装python3.5版本的时候还遇到过2个坑,也记录在此

错误重现

在安装和配置好python3后,想使用yum命令安装其他文件的时候报错

Error: GPG key retrieval failed: [Errno 14] Could not open/read file:///etc/pki/rpm-gpg/RPM-GPG-KEY-oracle
  • 1

解决方式

直接从oracle官网上下载报错的那个包

wget http://public-yum.oracle.com/RPM-GPG-KEY-oracle-ol6 -O /etc/pki/rpm-gpg/RPM-GPG-KEY-oracle
  • 1

以前遇到的另一个坑

错误重现

安装和配置好python3后,尝试启动python

[root@localhost bin]# python3.5 
python3.5: error while loading shared libraries: libpython3.5m.so.1.0: cannot open shared object file: No such file or directory
  • 1
  • 2

提示无法打开libpython3.5m.so.1.0,查看python的依赖关系:

[root@localhost python3.5]# ldd /usr/local/python3.5/bin/python3.5
    linux-vdso.so.1 =>  (0x00007fffca1d5000)
    libpython3.5m.so.1.0 => not found
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f44bac61000)
    libdl.so.2 => /lib64/libdl.so.2 (0x00007f44baa5d000)
    libutil.so.1 => /lib64/libutil.so.1 (0x00007f44ba85a000)
    libm.so.6 => /lib64/libm.so.6 (0x00007f44ba557000)
    libc.so.6 => /lib64/libc.so.6 (0x00007f44ba194000)
    /lib64/ld-linux-x86-64.so.2 (0x000055de0e9b5000)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

果然找不到这个包,手动在下载的python安装包中找到这个包并导入到/lib64路径,此问题解决

[root@localhost python3.5]# cp /usr/local/python3.5/lib/libpython3.5m.so.1.0 /lib64/
[root@localhost python3.5]# ldd /usr/local/python3.5/bin/python3.5
    linux-vdso.so.1 =>  (0x00007ffddaf99000)
    libpython3.5m.so.1.0 => /lib64/libpython3.5m.so.1.0 (0x00007f0071fc2000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f0071da6000)
    libdl.so.2 => /lib64/libdl.so.2 (0x00007f0071ba1000)
    libutil.so.1 => /lib64/libutil.so.1 (0x00007f007199e000)
    libm.so.6 => /lib64/libm.so.6 (0x00007f007169c000)
    libc.so.6 => /lib64/libc.so.6 (0x00007f00712d8000)
    /lib64/ld-linux-x86-64.so.2 (0x000055a35377e000)
[root@localhost python3.5]# python3.5 -V
Python 3.5.0

猜你喜欢

转载自blog.csdn.net/xdfwsl/article/details/80198762