> CentOS > CentOS教程 >

CentOS 7 记一次安装python3.7.3

1、默认python3、ipython3、pip的路径
    name@host:~$ which python
    /usr/bin/python
    name@host:~$ which python3
    /usr/bin/python3

    name@host:~$ which pip
    /usr/local/bin/pip
    name@host:~$ which pip3
    /usr/local/bin/pip3

    name@host:~$ which ipython3
    /usr/local/bin/ipython3
    name@host:~$ which ipython
    /usr/local/bin/ipyth
2、安装之后的路径
    /usr/local/python3.7/bin/pip3
    /usr/local/python3.7/bin/pip3.7
    /usr/local/python3.7/bin/python3
    /usr/local/python3.7/bin/python3.7
    

3、编译安装python3.7
    参考链接:https://ywnz.com/linuxjc/3159.html
    https://www.jianshu.com/p/7d32fba1121a

    官网下载:https://www.python.org/downloads/  file中选择 Gzipped source tarball

    比如:压缩包名字是Python-3.7.0.tgz
    解压:
        sudo tar -xzf Python-3.7.0.tgz
        可以 -C /opt/ 指定解压路径
    编译
        cd Python-3.7.0
        ./configure --prefix=/usr/local/python3.6 --enable-optimization
        --prefix 是预期安装目录,
        --enable-optimizations
        是优化选项(LTO,PGO 等)加上这个 flag 编译后,性能有 10% 左右的优化(如果没记错的话),但是这会明显的增加编译时间。不过关于 LTO 和 PGO 其实不在今天文章的讨论范围内,建议感兴趣的可以看看
        GCC 中 LTO 的具体实现https://gcc.gnu.org/onlinedocs/gccint/LTO-Overview.html
    安装
        sudo make
        sudo make install
    报错以及解决
        1.  configure: error:in ‘路径名’:
            no acceptable C compiler found in $path
            解决:
            sudo apt-get upgrade
            sudo apt-get install build-essential
        2.  Python build finished successfully!
            The necessary bits to build these optional modules were not found:
            _ctypes

        3.  zipimport.ZipImportError: can't decompress data; zlib not available
            解决: sudo apt install zlib*
            参考:https://blog.csdn.net/blueheart20/article/details/72827666
        4.  ModuleNotFoundError: No module named '_ctypes'
            Makefile:1130: recipe for target 'install' failed
            make: *** [install] Error 1
            解决:
            解决:安装模块
            sudo apt-get update
            sudo apt dist-upgrade
            sudo apt install build-essential libncursesw5-dev libgdbm-dev libc6-dev
            sudo apt install libssl-dev openssl
            sudo apt install libffi-dev
            ---------------------
            原文:https://blog.csdn.net/wang725/article/details/79905612
            参考链接:http://www.cnblogs.com/abeen/p/9355389.html

4、安装完成
 

    The directory '/home/name/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
    The directory '/home/name/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
    Looking in links: /tmp/tmp4crntzmm
    Collecting setuptools
    Collecting pip
    Installing collected packages: setuptools, pip
    Successfully installed pip-19.0.3 setuptools-40.8.0

链接处理

    建立软连接:

        ln -s /usr/local/python3.7/bin/python3  /usr/bin/python3

        这样建立之后,pip3 也会自动配置为python3.7的
    下面的问题出现后,将/usr/bin/python3有恢复成了原来的。
        建立了python3.7的软连接
            ln -s /usr/local/python3.7/bin/python3  /usr/bin/python3
        建立了pip3.7的软连接
            sudo ln -s  /usr/local/python3.7/bin/pip3    /usr/local/bin/pip3.7
    对于ipython3
        1.查看到 ipython3 命令在是 /usr/local/bin/ipython3
        2.cat /usr/local/bin/ipython3
            内容如下

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import re
import sys

from IPython import start_ipython

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
    sys.exit(start_ipython())

           复制一份命名为 /usr/local/bin/ipython3.7
           打开,将第一行改为 #!/usr/bin/python3.7
           试启动:
                ipython3.7
                报错
                    Traceback (most recent call last):
                    File "/usr/local/bin/ipython3.7", line 7, in <module>
                    from IPython import start_ipython
                    ModuleNotFoundError: No module named 'IPython'
                安装 sudo pip3.7 search  ipython
                成功

ipython3.7
/usr/local/python3.7/lib/python3.7/site-packages/IPython/core/history.py:226: UserWarning: IPython History requires SQLite, your history will not be saved
warn("IPython History requires SQLite, your history will not be saved")
Python 3.7.3 (default, May 20 2019, 15:29:59)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.5.0 -- An enhanced Interactive Python. Type '?' for help.
    这样 python3.7 就是python3.7的了
    这样 pip3.7 就是python3.7的了

5、系统问题:
    1.执行apt update 时报错, 因为python版本替换了
        报错如下:
            name@host:~$ sudo apt update
            命中:1 http://archive.ubuntukylin.com:10006/ubuntukylin xenial InRelease
            命中:2 http://archive.canonical.com/ubuntu bionic InRelease
            命中:3 http://mirrors.aliyun.com/ubuntu bionic InRelease
            命中:4 http://mirrors.aliyun.com/ubuntu bionic-security InRelease
            获取:5 https://deb.opera.com/opera-stable stable InRelease [2,591 B]
            已下载 2,591 B,耗时 2秒 (1,552 B/s)
            Traceback (most recent call last):
            File "/usr/lib/cnf-update-db", line 8, in <module>
            from CommandNotFound.db.creator import DbCreator
            ModuleNotFoundError: No module named 'CommandNotFound'
            正在读取软件包列表... 完成
            E: Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/test -w /var/lib/command-not-found/ -a -e /usr/lib/cnf-update-db; then /usr/lib/cnf-update-db > /dev/null; fi'
            E: Sub-process returned an error code
      同时ubuntu默认终端打不开,还是因为python的版本替换
      解决:将/usr/bin/python3下的软连接恢复成原来的。

    2.出现问题:
        E: 无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系。
      原因:有 1 个软件包没有被完全安装或卸载。
      解决: sudo apt --fix-broken install

6、最后sudo apt autoremove清除多余的包


(责任编辑:IT)