Linux安装Python3

JetXi 2021-02-22 11:13:30
linux Python 安装 python3


Linux安装Python3环境

编辑于2021-02-21

CentOS7.5默认只安装了Python2.7环境,python和python2命令均指向2.7版本

yum需要依赖Python2环境,自带Python和软链接均位于/usr/bin目录下

而现在主流开发环境早已不是Python2,因此我们需要考虑手动安装Python3,文中安装目录为/usr/local

 

  1. 安装用于编译Python3的依赖

    [root@JetXi Python-3.9.2]# yum install zlib zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
  1. 在python.org找到download-sourcecode,下载Python3的xz包,也可直接使用wget下载

    最新版本下载链接:https://www.python.org/ftp/python/3.9.2/Python-3.9.2.tar.xz
  1. 将xz包使用xftp上传到/usr/local目录,执行如下命令解压xz包

    [root@JetXi local]# xz -d Python-3.9.2.tar.xz
    [root@JetXi local]# tar xf Python-3.9.2.tar
  1. 进入解压得到的目录,依次执行如下命令编译Python,编译时间稍长,编译成功后会在/usr/local目录下生成一个名为python3的文件夹,说明Python3安装成功

    [root@JetXi local]# cd Python-3.9.2
    [root@JetXi Python-3.9.2]# ./configure prefix=/usr/local/python3
    [root@JetXi Python-3.9.2]# make && make install
  1. 查看系统内置Python版本和软链接指向情况,由结果可知,python命令指向python2命令,而python2命令指向python2.7,因此我们将python命令软链接指向刚才安装的Python3即可,这样执行python命令默认进入Python3,执行python2命令进入python2.7

    [root@JetXi Python-3.9.2]# ll /usr/bin/python*
    lrwxrwxrwx. 1 root root    7 Feb 19 01:11 /usr/bin/python -> python2
    lrwxrwxrwx. 1 root root    9 Feb 19 01:11 /usr/bin/python2 -> python2.7
    -rwxr-xr-x. 1 root root 7136 Aug  4  2017 /usr/bin/python2.7
  1. 删除指向python2的python软链接,创建指向Python3的python软链接

    [root@JetXi Python-3.9.2]# rm -f /usr/local/python
    [root@JetXi local]# ln -s /usr/local/python3/bin/python3.9 /usr/bin/python
  1. 测试结果是否符合预期

    [root@JetXi ~]# python
    Python 3.9.2 (default, Feb 21 2021, 07:59:45)
    [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> exit()
    [root@JetXi ~]# python2
    Python 2.7.5 (default, Aug  4 2017, 00:39:18)
    [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> exit()
  1. 至此Python3环境已经全部安装完成,但是正如前文所说,yum需要基于Python2运行,因此我们需要对yum的配置文件做一些改动

    [root@JetXi ~]# vi /usr/bin/yum
    # 将第一行的python改为python2
    #! /usr/bin/python2
  1. 同理,urlgrabber-ext-down也需要修改

    [root@JetXi ~]# vi /usr/libexec/urlgrabber-ext-down
    # 将第一行的python改为python2
    #! /usr/bin/python2
  1. 若不想做8 9的改动,可以考虑不覆盖python软链接,而是创建一个python3软链接指向Python3

    [root@JetXi local]# ln -s /usr/local/python3/bin/python3.9 /usr/bin/python3
  1. 这样需要输入python3命令使用Python3

    [root@JetXi ~]# python3
    Python 3.9.2 (default, Feb 21 2021, 07:59:45)
    [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> exit()
版权声明
本文为[JetXi]所创,转载请带上原文链接,感谢
https://www.cnblogs.com/charon233/p/14423997.html

  1. Detailed usage of urllib in Python 3 (header, proxy, timeout, authentication, exception handling)
  2. python 第三方库paramiko
  3. python 第三方库paramiko
  4. Python third party library paramiko
  5. Python third party library paramiko
  6. 卸载 PyCharm!这才是 Python 小白的最理想的 IDE
  7. 卸载 PyCharm!这才是 Python 小白的最理想的 IDE
  8. Uninstall pycharm! This is the ideal IDE for Python Xiaobai
  9. django学习-27.admin管理后台里:对列表展示页面的数据展示进行相关优化
  10. Uninstall pycharm! This is the ideal IDE for Python Xiaobai
  11. Django learning - 27. Admin management background: optimize the data display of the list display page
  12. python day2
  13. python day2
  14. Python 内存泄漏问题排查
  15. Troubleshooting of Python memory leak
  16. Python 与 excel的简单应用
  17. Simple application of Python and excel
  18. Python 与 excel的简单应用
  19. Simple application of Python and excel
  20. 2.7万 Star!最全面的 Python 设计模式集合
  21. 27000 stars! The most comprehensive collection of Python design patterns
  22. python day3
  23. python day3
  24. Commonly used data operation functions of Python
  25. (数据科学学习手札108)Python+Dash快速web应用开发——静态部件篇(上)
  26. (learning notes of data science 108) Python + dash rapid web application development -- static components (I)
  27. (数据科学学习手札108)Python+Dash快速web应用开发——静态部件篇(上)
  28. (learning notes of data science 108) Python + dash rapid web application development -- static components (I)
  29. [Python] Matplotlib 图表的绘制和美化技巧
  30. Drawing and beautifying skills of [Python] Matplotlib chart
  31. [Python] Matplotlib 图表的绘制和美化技巧
  32. Drawing and beautifying skills of [Python] Matplotlib chart
  33. Virtual environment of Python project
  34. 翻译:《实用的Python编程》02_01_Datatypes
  35. Translation: practical Python Programming 02_ 01_ Datatypes
  36. 翻译:《实用的Python编程》02_01_Datatypes
  37. 翻译:《实用的Python编程》02_01_Datatypes
  38. Translation: practical Python Programming 02_ 01_ Datatypes
  39. Translation: practical Python Programming 02_ 01_ Datatypes
  40. Python 3 入门,看这篇就够了
  41. Python 3 entry, see this is enough
  42. 华为大佬打造的400集Python视频学起来,学完万物皆可爬
  43. 400 episodes of Python video created by Huawei boss
  44. django之csrf_exempt解决跨域请求的问题
  45. CSRF of Django_ Exempt solves the problem of cross domain requests
  46. 1.7 万 Star!一个简单实用的 Python 进度条库
  47. 17000 stars! A simple and practical Python progress bar library
  48. Python爬虫:设置Cookie解决网站拦截并爬取蚂蚁短租
  49. Python crawler: setting cookie to solve website interception and crawling ant short rent
  50. Python-Net编程
  51. Python net programming
  52. 学习Python数学英语基础重要吗?Python教程!
  53. Is it important to learn the basics of math and English in Python!
  54. Python数据分析常用库有哪些?Python学习!
  55. What are the common libraries for Python data analysis? Learn Python!
  56. win 创建python虚拟环境
  57. Creating Python virtual environment with win
  58. In order to automatically collect B station barrage, I developed a tool in Python
  59. 用Python编程语言来实现阿姆斯特朗数的检查
  60. Using python programming language to check Armstrong number