【安装:Python3.8】
1 | dnf install python38 |
查看版本:
1 | python -V |
报错:
1 | - bash : python: command not found |
建立软连接:
1 | ln -s /usr/bin/python3 .8 /usr/bin/python |
这时再验证下
输出:
1 | Python 3.8.8 |
查看位置:
1 | whereis python |
输出:
1 | python: /usr/bin/python2 .7 /usr/bin/python3 .6-config /usr/bin/python3 .8 /usr/bin/python /usr/bin/python3 .6m /usr/bin/python3 .6m-x86_64-config /usr/bin/python3 .6 /usr/bin/python3 .6m-config /usr/lib/python2 .7 /usr/lib/python3 .8 /usr/lib/python3 .6 /usr/lib64/python2 .7 /usr/lib64/python3 .8 /usr/lib64/python3 .6 /usr/local/lib/python3 .6 /usr/include/python2 .7 /usr/include/python3 .8 /usr/include/python3 .6m /usr/share/man/man1/python .1.gz |
【安装:Python3.6 + pip】
1 2 3 4 5 6 | yum install -y epel-release yum install -y https: //mirrors .aliyun.com /ius/ius-release-el7 .rpm yum install -y python36u python36u-devel python36u-pip pip3.6 install -U pip pip install -U virtualenv rm -f /usr/bin/python3 && ln -s /usr/bin/python3 .6 /usr/bin/python3 |
查看版本:
1 | python3 -V |
输出:
1 | Python 3.6.8 |
【版本】
Python版本:2.7.x与3.x。
Python 3是一个不同于Python 2的语言。二者在语义、语法上,既存在细微的区别,又有着截然不同的一面。
截至今天,Python2.6/2.7是安装数量和使用度最高的版本。许多主流的Python库、框架、工具都没有做到100%兼容Python 3。因此,最稳妥的选择就是使用2.7.x)。务必只在你需要或者完全了解情况的前提下,才选择Python 3。现在,主流第三方库和框架对Python 3的支持度已经很高。根据py3readiness网站的统计,360个最受欢迎的Python包中已经有315个支持Python 3。具体的支持情况,可以查看这个网站。一定程度上说,Python 3已经成为新的主流。
查看版本:
1 | python -V |
输出:
1 | Python 2.7.5 |
参考:https://blog.csdn.net/tpc1990519/article/details/77856399
输入以下命令查看目前系统使用的Python以及安装目录,接下来将要对该目录下的Python进行修改:
1 | which python |
输出:
1 | /usr/bin/python |
1 | ll /usr/bin/python * |
输出:
1 2 3 | lrwxrwxrwx. 1 root root 7 May 30 05:52 /usr/bin/python -> python2 lrwxrwxrwx. 1 root root 9 May 30 05:52 /usr/bin/python2 -> python2.7 -rwxr-xr-x. 1 root root 7136 Nov 6 2016 /usr/bin/python2 .7 |
【升级】
下载:https://www.python.org/ftp/python/
当前最新版本为2.7.15和3.7.1。
升级到Python 2.7.15:
1 | wget https: //www .python.org /ftp/python/2 .7.15 /Python-2 .7.15.tgz && tar vxzf Python-2.7.15.tgz && cd Python-2.7.15 && . /configure && make && make install |
保留旧版本Python,因为先前的yum等命令依赖旧版本:
1 | mv /usr/bin/python /usr/bin/python2 .7.5 |
做好新版本连接:
1 | ln -s /usr/local/bin/python2 .7 /usr/bin/python |
更新环境变量:
1 | export PATH= "/usr/local/bin:$PATH" |
查看Python的版本:
1 | python -V |
输出:
1 | Python 2.7.15 |
【升级:Python 3.8.9】
下载:https://www.python.org/ftp/python/
查看Python的版本:
1 | python -V |
输出:
1 | Python 2.7.17 |
下载源码包进行编译:
1 | wget https: //www .python.org /ftp/python/3 .8.9 /Python-3 .8.9.tgz --no-check-certificate && tar vxzf Python-3.8.9.tgz && cd Python-3.8.9 && . /configure -- enable -optimizations && make && make install |
保留旧版本Python,因为先前的yum等命令依赖旧版本:
1 | mv /usr/bin/python /usr/bin/python2 .7.17 |
做好新版本连接:
1 | ln -sv /usr/local/bin/python3 .8 /usr/bin/python |
更新环境变量:
1 | export PATH= "/usr/local/bin:$PATH" |
查看Python的版本:
1 | python -V |
输出:
1 | Python 3.8.9 |
修改yum的Python配置:
因为yum采用python作为命令解释器,这可以从/usr/bin/yum文件中第一行#!/usr/bin/python发现。
而python版本之间兼容性不太好,使得2.X版本与3.0版本之间存在语法不一致问题。
而CentOS系统将python升级到2.6或3.0后,出现语法解释错误。
Python2.x内升级:
方案:编辑 /usr/bin/yum
将 #!/usr/bin/python 改为 #!/usr/bin/python2.7.5
参考:https://blog.csdn.net/chaojiwanwan/article/details/71439066
升级到Python3.x,还需要操作:
1、编辑 /usr/libexec/urlgrabber-ext-down
将 #!/usr/bin/python 改为 #!/usr/bin/python2.7.5
2、编辑 /usr/bin/yum-config-manager
将 #!/usr/bin/python 改为 #!/usr/bin/python2.7.5
否则报错如下:
1 2 3 4 5 6 7 8 9 10 11 | Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * elrepo: elrepo.mirror.angkasa. id * elrepo-kernel: elrepo.mirror.angkasa. id File "/usr/libexec/urlgrabber-ext-down" , line 28 except OSError, e: ^ SyntaxError: invalid syntax Exiting on user cancel |
【排错】
报错:-bash: python: command not found
安装 python :python2 python3.6 python3.8
1 | yum install python2 |
软链接:
1 | sudo ln -s /usr/bin/python2 .7 /usr/bin/python |
报错:UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-13
参考:https://www.cnblogs.com/xiao-xue-di/p/11283496.html
修改Python本环境,在Python的 Lib\site-packages 文件夹下新建一个 sitecustomize.py 文件,内容为:
1 2 3 4 | #coding=utf8 import sys reload(sys) sys.setdefaultencoding( 'utf8' ) |
重启Python解释器,发现编码已被设置为utf8。这是因为系统在Python启动的时候,自行调用该文件,设置系统的默认编码。
寻找 site-packages :
1 | python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()" |
输出:
1 | /usr/lib/python2 .7 /site-packages |
【介绍】
教程:http://www.runoob.com/python3/python3-tutorial.html
教程:https://www.liaoxuefeng.com/
教程:http://www.iplaypy.com/
参考:https://blog.csdn.net/xiaofei0859/article/details/51969187
参考:https://danieleriksson.net/2017/02/08/how-to-install-latest-python-on-centos/ 多版本
参考:https://www.cnblogs.com/lihanx/p/5947178.html
参考:https://www.jianshu.com/p/dce4a576c261
参考:https://blog.csdn.net/tpc1990519/article/details/77856399
参考:https://blog.csdn.net/redmoon729/article/details/53258803