RedHat/Centos系统中Python源码编译升级Python 2.6.6版到Python 2.7.15

1. 前言

      升级python版本,有两种方式,第一种是通过RPM包的方式升级,第二种是通过源码编译升级。两种方式各有利弊,很难说那种好,RPM难找,源码编译依赖多,总之,都是需要很多依赖库的,而一般的环境都是没有外网的。

2.  环境版本信息

       本文中的操作系统是RedHat6.9版本,属于比较老的版本,但是按照greenplum时,Python环境最低要求是Python2.7版本,系统自带的版本是Python2.6.6版本,因此只能升级Python版本来解决。

[root@localhost ~]# lsb_release -a
LSB Version:    :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
Distributor ID: RedHatEnterpriseServer
Description:    Red Hat Enterprise Linux Server release 6.9 (Santiago)
Release:        6.9
Codename:       Santiago
[root@localhost ~]# 

3. 安装步骤

3.1  Python2.7.15源码下载和配置。

      源码下载就不多说了,配置的话,可以根据需要去配置,最简单的就是大部分默认安装,切换到源码目录,直接./configure就好。

      如果有必要,可以通过命令./configure --help查看选项,如下图所示:

[root@localhost Python-2.7.15]# ./configure --help
`configure' configures python 2.7 to adapt to many kinds of systems.

Usage: ./configure [OPTION]... [VAR=VALUE]...

To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE.  See below for descriptions of some of the useful variables.

Defaults for the options are specified in brackets.

Configuration:
  -h, --help              display this help and exit
      --help=short        display options specific to this package
      --help=recursive    display the short help of all the included packages
  -V, --version           display version information and exit
  -q, --quiet, --silent   do not print `checking ...' messages
      --cache-file=FILE   cache test results in FILE [disabled]
  -C, --config-cache      alias for `--cache-file=config.cache'
  -n, --no-create         do not create output files
      --srcdir=DIR        find the sources in DIR [configure dir or `..']

Installation directories:
  --prefix=PREFIX         install architecture-independent files in PREFIX
                          [/usr/local]
  --exec-prefix=EPREFIX   install architecture-dependent files in EPREFIX
                          [PREFIX]

By default, `make install' will install all the files in
`/usr/local/bin', `/usr/local/lib' etc.  You can specify
an installation prefix other than `/usr/local' using `--prefix',
for instance `--prefix=$HOME'.

For better control, use the options below.

Fine tuning of the installation directories:
  --bindir=DIR            user executables [EPREFIX/bin]
  --sbindir=DIR           system admin executables [EPREFIX/sbin]
  --libexecdir=DIR        program executables [EPREFIX/libexec]
  --sysconfdir=DIR        read-only single-machine data [PREFIX/etc]
  --sharedstatedir=DIR    modifiable architecture-independent data [PREFIX/com]
  --localstatedir=DIR     modifiable single-machine data [PREFIX/var]
  --libdir=DIR            object code libraries [EPREFIX/lib]
  --includedir=DIR        C header files [PREFIX/include]
  --oldincludedir=DIR     C header files for non-gcc [/usr/include]
  --datarootdir=DIR       read-only arch.-independent data root [PREFIX/share]
  --datadir=DIR           read-only architecture-independent data [DATAROOTDIR]
  --infodir=DIR           info documentation [DATAROOTDIR/info]
  --localedir=DIR         locale-dependent data [DATAROOTDIR/locale]
  --mandir=DIR            man documentation [DATAROOTDIR/man]
  --docdir=DIR            documentation root [DATAROOTDIR/doc/python]
  --htmldir=DIR           html documentation [DOCDIR]
  --dvidir=DIR            dvi documentation [DOCDIR]
  --pdfdir=DIR            pdf documentation [DOCDIR]
  --psdir=DIR             ps documentation [DOCDIR]

System types:
  --build=BUILD     configure for building on BUILD [guessed]
  --host=HOST       cross-compile to build programs to run on HOST [BUILD]
  --target=TARGET   configure for building compilers for TARGET [HOST]

Optional Features:
  --disable-option-checking  ignore unrecognized --enable/--with options
  --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)
  --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]
  --enable-universalsdk[=SDKDIR]
                          Build fat binary against Mac OS X SDK
  --enable-framework[=INSTALLDIR]
                          Build (MacOSX|Darwin) framework
  --enable-shared         disable/enable building shared python library
  --enable-profiling      enable C-level code profiling
  --enable-optimizations  Enable expensive optimizations (PGO, maybe LTO,
                          etc). Disabled by default.
  --enable-toolbox-glue   disable/enable MacOSX glue code for extensions
  --enable-ipv6           Enable ipv6 (with ipv4) support
  --disable-ipv6          Disable ipv6 support
  --enable-big-digits[=BITS]
                          use big digits for Python longs [[BITS=30]]
  --enable-unicode[=ucs[24]]
                          Enable Unicode strings (default is ucs2)

Optional Packages:
  --with-PACKAGE[=ARG]    use PACKAGE [ARG=yes]
  --without-PACKAGE       do not use PACKAGE (same as --with-PACKAGE=no)
  --with-universal-archs=ARCH
                          select architectures for universal build ("32-bit",
                          "64-bit", "3-way", "intel", "intel-32", "intel-64",
                          or "all")
  --with-framework-name=FRAMEWORK
                          specify an alternate name of the framework built
                          with --enable-framework
  --without-gcc           never use gcc
  --with-icc              build with icc
  --with-cxx-main=
                          compile main() and link python executable with C++
                          compiler
  --with-suffix=.exe      set executable suffix
  --with-pydebug          build with Py_DEBUG defined
  --with-lto              Enable Link Time Optimization in PGO builds.
                          Disabled by default.
  --with-libs='lib1 ...'  link against additional libs
  --with-system-expat     build pyexpat module using an installed expat
                          library
  --with-system-ffi       build _ctypes module using an installed ffi library
  --with-tcltk-includes='-I...'
                          override search for Tcl and Tk include files
  --with-tcltk-libs='-L...'
                          override search for Tcl and Tk libs
  --with-dbmliborder=db1:db2:...
                          order to check db backends for dbm. Valid value is a
                          colon separated string with the backend names
                          `ndbm', `gdbm' and `bdb'.
  --with-signal-module    disable/enable signal module
  --with-dec-threads      use DEC Alpha/OSF1 thread-safe libraries
  --with(out)-threads[=DIRECTORY]
                          disable/enable thread support
  --with(out)-thread[=DIRECTORY]
                          deprecated; use --with(out)-threads
  --with-pth              use GNU pth threading libraries
  --with(out)-doc-strings disable/enable documentation strings
  --with(out)-tsc         enable/disable timestamp counter profile
  --with(out)-pymalloc    disable/enable specialized mallocs
  --with-valgrind         Enable Valgrind support
  --with-wctype-functions use wctype.h functions
  --with-fpectl           enable SIGFPE catching
  --with-libm=STRING      math library
  --with-libc=STRING      C library
  --with(out)-computed-gotos
                          Use computed gotos in evaluation loop (enabled by
                          default on supported compilers)
  --with(out)-ensurepip=[=OPTION]
                          "install" or "upgrade" using bundled pip, default is
                          "no"

Some influential environment variables:
  CC          C compiler command
  CFLAGS      C compiler flags
  LDFLAGS     linker flags, e.g. -L if you have libraries in a
              nonstandard directory 
  LIBS        libraries to pass to the linker, e.g. -l
  CPPFLAGS    (Objective) C/C++ preprocessor flags, e.g. -I if
              you have headers in a nonstandard directory 
  CPP         C preprocessor
  PKG_CONFIG  path to pkg-config utility
  PKG_CONFIG_PATH
              directories to add to pkg-config's search path
  PKG_CONFIG_LIBDIR
              path overriding pkg-config's built-in search path

Use these variables to override the choices made by `configure' or to help
it to find libraries and programs with nonstandard names/locations.

Report bugs to .
[root@localhost Python-2.7.15]# 

3.2 源码编译依赖模块安装

        Python源码编译一般都会报错,说某些模块找不到,我已经安装了必要的模块了,缺少的模块就比较少,此处就是为说明会报缺少模块而截图的,编译安装的缺少的模块信息如下图:

安装过的包大概有如下包,下面的包需要根据操作系统的不同,选择不同的版本。

[root@localhost greeplum]# ll
total 142004
-rw-r--r-- 1 root root   126508 Sep 10 19:30 apr-1.3.9-5.el6_9.1.x86_64.rpm
-rw-r--r-- 1 root root    89340 Sep 10 19:30 apr-util-1.3.9-3.el6_0.1.x86_64.rpm
-rw-r--r-- 1 root root    49724 Sep 30 10:22 bzip2-1.0.5-7.el6_0.x86_64.rpm
-rw-r--r-- 1 root root   256020 May 17  2018 bzip2-devel-1.0.5-7.el6_0.x86_64.rpm
-rw-r--r-- 1 root root    37368 May 17  2018 bzip2-libs-1.0.5-7.el6_0.x86_64.rpm
-rw-r--r-- 1 root root    29860 Jan 18  2018 gdbm-1.8.0-39.el6.x86_64.rpm
-rw-r--r-- 1 root root    26228 Jan 18  2018 gdbm-devel-1.8.0-39.el6.x86_64.rpm
-rw-r--r-- 1 root root   197208 Sep 10 19:36 libevent2-2.0.21-2.el6.x86_64.rpm
-rw-r--r-- 1 root root    53232 Sep 10 19:33 libyaml-0.1.3-4.el6_6.x86_64.rpm
-rw-r--r-- 1 root root   273996 Sep 15 11:58 ncurses-5.7-4.20090207.el6.x86_64.rpm
-rw-r--r-- 1 root root   656736 Sep 15 11:59 ncurses-devel-5.7-4.20090207.el6.x86_64.rpm
-rw-r--r-- 1 root root   182628 Sep 15 13:53 readline-6.0-4.el6.x86_64.rpm
-rw-r--r-- 1 root root   137392 May 17  2018 readline-devel-6.0-4.el6.x86_64.rpm
-rw-r--r-- 1 root root    82684 Sep 15 14:46 sqlite-devel-3.6.20-1.el6_7.2.x86_64.rpm
-rw-r--r-- 1 root root    81468 May 16  2018 sqlite-devel-3.6.20-1.el6.x86_64.rpm
-rw-r--r-- 1 root root  2007772 Sep 15 15:34 tcl-8.5.7-6.el6.x86_64.rpm
-rw-r--r-- 1 root root   165760 Sep 15 15:31 tcl-devel-8.5.7-6.el6.x86_64.rpm
-rw-r--r-- 1 root root   258220 Sep 15 15:29 tix-8.4.3-5.el6.x86_64.rpm
-rw-r--r-- 1 root root   141828 Sep 15 15:26 tix-devel-8.4.3-5.el6.x86_64.rpm
-rw-r--r-- 1 root root  1469560 Sep 15 15:45 tk-8.5.7-5.el6.x86_64.rpm
-rw-r--r-- 1 root root   507828 Sep 15 15:43 tk-devel-8.5.7-5.el6.x86_64.rpm
-rw-r--r-- 1 root root   263756 Sep 15 13:58 tkinter-2.6.6-66.el6_8.x86_64.rpm
[root@localhost greeplum]# 

根据参考文章的,有些包已经没什么用了 ,被弃用了,下面的包就没必要安装了,没必要安装的包如下图所示:

参考文章:https://blog.csdn.net/wowocpp/article/details/80710984

这里写图片描述

3.3 去除被弃用模块

      既然有些模块已经弃用了或者针对特定操作系统的模块,因此需要去除这些模块才能胜利通过编译。这些模块检测都在setup.py中detect_modules(self)函数中,需要移除的模块如下:

_tkinter,bsddb185,dl ,imageop, sunaudiodev 

移除的方法,可以使用注释或者删除代码的方式来做到。

(1) 移除 imageop 代码(原行号:719行~723行):

        # Disabled on 64-bit platforms
        if sys.maxsize != 9223372036854775807L:
            # Operations on images
            exts.append( Extension('imageop', ['imageop.c']) )
        else:
            missing.extend(['imageop'])

(2) 移除bsddb185代码(原行号:1237行~1254行) :

        if os.path.exists(f) and not db_incs:
            data = open(f).read()
            m = re.search(r"#s*define\s+HASHVERSION\s+2\s*", data)
            if m is not None:
                # bingo - old version used hash file format version 2
                ### XXX this should be fixed to not be platform-dependent
                ### but I don't have direct access to an osf1 platform and
                ### seemed to be muffing the search somehow
                libraries = host_platform == "osf1" and ['db'] or None
                if libraries is not None:
                    exts.append(Extension('bsddb185', ['bsddbmodule.c'],
                                          libraries=libraries))
                else:
                    exts.append(Extension('bsddb185', ['bsddbmodule.c']))
            else:
                missing.append('bsddb185')
        else:
            missing.append('bsddb185')

(3)移除dl代码(原行号:1545行~1553行)   

       # Dynamic loading module
        if sys.maxint == 0x7fffffff:
            # This requires sizeof(int) == sizeof(long) == sizeof(char*)
            dl_inc = find_file('dlfcn.h', [], inc_dirs)
            if (dl_inc is not None) and (host_platform not in ['atheos']):
                exts.append( Extension('dl', ['dlmodule.c']) )
            else:
                missing.append('dl')
        else:
            missing.append('dl')

  (4)移除sunaudiodev代码(原行号:1629行~1633行)

        if host_platform == 'sunos5':
            # SunOS specific modules
            exts.append( Extension('sunaudiodev', ['sunaudiodev.c']) )
        else:
            missing.append('sunaudiodev')

 (5)移除_tkinter代码(原行号:1746行~1747行)

        if '_tkinter' not in [e.name for e in self.extensions]:
            missing.append('_tkinter')

3.4 编译安装

编译:

[root@localhost Python-2.7.15]# make 

RedHat/Centos系统中Python源码编译升级Python 2.6.6版到Python 2.7.15_第1张图片

安装:

[root@localhost Python-2.7.15]# make install

RedHat/Centos系统中Python源码编译升级Python 2.6.6版到Python 2.7.15_第2张图片

[root@localhost Python-2.7.15]# python
Python 2.7.15 (default, Sep 30 2020, 11:47:16) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

默认安装已经覆盖原来的python,所以不需要做链接,本来可能需要ln -sf xxx/xxx/python   /usr/bin/python

你可能感兴趣的:(工具)