依赖包的安装

  1. 编译环境的安装(centos6 默认已具备)
yum -y groupinstall "Development tools"
或
yum -y install gcc gcc-c++ zlib zlib-devel
    gcc-4.4.7-23
    gcc-c++-4.4.7-23
    zlib-devel-1.2.3-29
yum install -y openssl-devel
yum install -y libffi-devel

yum install gcc gcc-c++ glibc-static
  1. 编译安装 openssl
wget http://www.openssl.org/source/openssl-1.1.1.tar.gz
tar -zxvf openssl-1.1.1.tar.gz
cd openssl-1.1.1
whereis openssl
    # 系统自带版本为 1.0.1e-fips
    # [root@hhjos6 lib]# whereis openssl
    # openssl: /usr/bin/openssl /usr/lib64/openssl /usr/include/openssl /usr/local/openssl /usr/share/man/man1/openssl.1ssl.gz
mkdir /usr/local/openssl
./config --prefix=/usr/local/openssl shared zlib
make && make install
echo -e "\nexport LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/openssl/lib" >> /etc/profile
source /etc/profile
ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/lib64/libssl.so.1.1
ln -s /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1
mv /usr/bin/openssl /usr/bin/openssl-1.0.1
ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
openssl
    version
    exit
    # 下个版本的说明 https://wiki.openssl.org/index.php/OpenSSL_3.0

程序的下载与编译

  1. 下载程序
mkdir -p /home/common/python3.8  && cd /home/common/python3.8

#测试表明,Python 并没有限制 sqlite 版本;不过关闭 --enable-loadable-sqlite-extensions 或者 --enable-optimizations 均能编译通过(虽然同样的报错还在)。

wget https://www.python.org/ftp/python/3.8.7/Python-3.8.7.tgz
    官方手册 https://docs.python.org/release/3.8.7
    sqlite教程 https://docs.python.org/3.8/library/sqlite3.html
tar -xzvf Python-3.8.7.tgz
  1. 配套修改组件的编译配置(https://docs.python.org/3.8/whatsnew/changelog.html#changelog 链接中指明了 Windows、MacOS 中集成的 sqlite 版本号,但是 Linux 的 Python 3 并没有继承 sqlite,因此需要如下配置 sqlite 库)
# ModuleNotFoundError: No module named '_sqlite3'
gedit /home/common/python3.8/Python-3.8.7/setup.py
    sqlite_inc_paths
        '/usr/local/sqlite/include',
        '/usr/local/sqlite/include/sqlite3',
#undefined symbol: __gcov_indirect_call_profiler
# cd /home/common/python3.8/Python-3.8.7 && ./python -E -S -m sysconfig --generate-posix-vars 
# 报错信息同上
# 网友中有通过设置语言
  1. 编译
whereis python
mkdir /usr/local/python3
cd /home/common/python3.8/Python-3.8.7
export CPPFLAGS=" -Wno-error=coverage-mismatch"  # 可选
./configure  --prefix=/usr/local/python3 --with-openssl=/usr/local/openssl --enable-shared CFLAGS=-fPIC --enable-profiling --enable-loadable-sqlite-extensions
    #LD_RUN_PATH=/usr/local/lib ./configure  --prefix=/usr/local/python3 --with-openssl=/usr/local/openssl --enable-shared CFLAGS=-fPIC --enable-optimizations --enable-profiling --enable-loadable-sqlite-extensions   |grep -v "yes"
    LD_RUN_PATH=/usr/local/lib: 可选
    --prefix 可把所有资源文件放在指定路径下,卸载软件或移植软件只需要对应的文件夹操作。
        不指定的话,可执行文件默认放在/usr/local/bin,库文件默认放在/usr/local/lib,
        配置文件默认放在/usr/local/etc,其它的资源文件放在/usr/local/share,比较凌乱
        要卸载程序,也可以在原来的make目录下用一次make uninstall,但前提是make文件指定过uninstall,否则卸载要结合安装日志来进行逐一删除
    --enable-optimizations 加上后性能有 10% 左右的优化,但会明显增加编译时间;
        且 https://bugs.python.org/issue34112 提出 gcc 8.1 以下可能编译不过
    --enable-shared CFLAGS=-fPIC  是使用 YCM、pyinstaller、caffe、mysqlclient 等一系列功能的需要(后者确保编译时使用相对路径)
    --enable-profiling:let you profile the interpreter with gprof
    --enable-loadable-sqlite-extensions:support loadable extensions in _sqlite module
    # 查看帮助 ./configure --help 或  ./configure -h
    # 在更改 ./configure 编译参数后必须清除生成的文件 make clean && make distclean
make -j4 
    #LD_RUN_PATH=/usr/local/lib make -j4 
    # 警告:‘/home/common/python3.8/Python-3.8.7/build/temp.linux-x86_64-3.8/home/common/python3.8/Python-3.8.7/Modules/resource.gcda’的版本是‘404R’,需要的版本‘408*’ [默认启用]
    cd /home/common/python3.8/Python-3.8.7/build/lib.linux-x86_64-3.8
    ls  |grep sqlite
make install > py3-install-`date +%Y%m%d`.log

关联系统

  • 添加到系统

    # 设置环境变量,也可以直接将 /usr/local/python3/bin 加入 /etc/profile 文件的环境变量 path 中
    ln -s /usr/local/python3/bin/python3 /usr/bin/python3
        # 考虑到 yum 必须使用 python 2 因此,不能命名为 python,切记!
        ln -s /usr/local/python3/bin/python3 /usr/bin/py3
    ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
    ln -s /usr/local/python3/bin/python3-config /usr/bin/python3-config
    cp /usr/local/python3/lib/libpython3.8.so.1.0 /usr/lib64
        #/usr/local/python3/bin/python3.8: error while loading shared libraries: libpython3.8.so.1.0: cannot open shared object file: No such file or directory
        cd /usr/local/lib
        ln -s libpython3.8.so.1.0 libpython3.8.so
        cp /usr/local/python3/lib/libpython3.8.so.1.0 /usr/local/lib
    ldconfig -v
    python -V
        whereis python
        whereis python3
    
    find / -name _sqlite*.so
    python3
        import sqlite3
        Ctrl+ D
    
    pip3 -V
        # pip 21.0 将在 2021 年 1 月释出,将停止支持 Python 2.7(Python 2.x) 系列
    pip -V
        #python 2
  • 修改 Python3环境变量

    vi ~/.bash_profile  将“PATH=$PATH:$HOME/bin”修改为“PATH=$PATH:$HOME/bin:/usr/local/python3/bin”
        source ~/.bash_profile
        # 关闭报警
        export PIP_DISABLE_PIP_VERSION_CHECK=1
  • 修复Python2的关联

    vi /usr/bin/yum
    vi /usr/bin/yum-config-manager
    
    vim /usr/libexec/urlgrabber-ext-down
    
    vi /usr/libexec/ibus-ui-gtk
    vi /usr/bin/ibus-setup
    vi /usr/libexec/ibus-engine-table
    vi /usr/bin/gnome-tweak-tool