赞
踩
因为centos7自带python2.7版本,在大部分开发过程中,我们需要使用到python3进行测试,于是下载安装python3,近乎是一种刚需,至于是否设置默认python指向python3,则看个人习惯~
提示:以下是正文内容基于个人实践~
基于VMware的虚拟机,Centos7.4
提示:实机效果跟虚拟机基本一致~
[root@localhost ~]# python -V
-----------------
| python 2.7.5 |
-----------------
[root@localhost ~]# python
-----------------------------------------------------------------------
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() #退出python
-----------------------------------------------------------------------
提示:python3安装方法很多种,wget、rpm、yum等等,本文通过常用的wget与yum进行示例~
[root@localhost ~]# yum install -y wget #wget用来从指定的URL中下载文件
[root@localhost ~]# wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz
#本文下载的是官网的3.6.1,截止发布URL仍是正确的,如需其他版本替换成相对应版本极了
-----------------------------------------------------------------------
--2022-04-30 09:03:22-- https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz
Resolving www.python.org (www.python.org)... 199.232.44.223, 2a04:4e42:48::223
Connecting to www.python.org (www.python.org)|199.232.44.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 22540566 (21M) [application/octet-stream]
Saving to: ‘Python-3.6.1.tgz’
4% [=> ]
-----------------------------------------------------------------------
提示:wget下载这个得看脸,要是有其他稳定来源咱也...
[root@localhost ~]# yum -y install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel zlib-devel gcc ----------------------------------------------------------------------- Installed: bzip2-devel.x86_64 0:1.0.6-13.el7 expat-devel.x86_64 0:2.1.0-14.el7_9 gcc.x86_64 0:4.8.5-44.el7 gdbm-devel.x86_64 0:1.10-8.el7 openssl-devel.x86_64 1:1.0.2k-25.el7_9 readline-devel.x86_64 0:6.2-11.el7 zlib-devel.x86_64 0:1.2.7-19.el7_9 Dependency Installed: cpp.x86_64 0:4.8.5-44.el7 glibc-devel.x86_64 0:2.17-325.el7_9 glibc-headers.x86_64 0:2.17-325.el7_9 kernel-headers.x86_64 0:3.10.0-1160.62.1.el7 keyutils-libs-devel.x86_64 0:1.5.8-3.el7 krb5-devel.x86_64 0:1.15.1-51.el7_9 libcom_err-devel.x86_64 0:1.42.9-19.el7 libkadm5.x86_64 0:1.15.1-51.el7_9 libmpc.x86_64 0:1.0.1-3.el7 libselinux-devel.x86_64 0:2.5-15.el7 libsepol-devel.x86_64 0:2.5-10.el7 libverto-devel.x86_64 0:0.2.5-4.el7 mpfr.x86_64 0:3.1.1-4.el7 ncurses-devel.x86_64 0:5.9-14.20130511.el7_4 pcre-devel.x86_64 0:8.32-17.el7 Dependency Updated: e2fsprogs.x86_64 0:1.42.9-19.el7 e2fsprogs-libs.x86_64 0:1.42.9-19.el7 expat.x86_64 0:2.1.0-14.el7_9 glibc.x86_64 0:2.17-325.el7_9 glibc-common.x86_64 0:2.17-325.el7_9 krb5-libs.x86_64 0:1.15.1-51.el7_9 libcom_err.x86_64 0:1.42.9-19.el7 libgcc.x86_64 0:4.8.5-44.el7 libgomp.x86_64 0:4.8.5-44.el7 libselinux.x86_64 0:2.5-15.el7 libselinux-python.x86_64 0:2.5-15.el7 libselinux-utils.x86_64 0:2.5-15.el7 libsepol.x86_64 0:2.5-10.el7 libss.x86_64 0:1.42.9-19.el7 ncurses.x86_64 0:5.9-14.20130511.el7_4 ncurses-base.noarch 0:5.9-14.20130511.el7_4 ncurses-libs.x86_64 0:5.9-14.20130511.el7_4 openssl.x86_64 1:1.0.2k-25.el7_9 openssl-libs.x86_64 1:1.0.2k-25.el7_9 readline.x86_64 0:6.2-11.el7 zlib.x86_64 0:1.2.7-19.el7_9 Complete! -----------------------------------------------------------------------
[root@localhost ~]# tar -zxvf Python-3.6.1.tgz
[root@localhost ~]# cd Python-3.6.1/
[root@localhost ~]# ./configure --prefix=/usr/local/
----------------------------------------------
creating Modules/Setup
creating Modules/Setup.local
creating Makefile
---------------------------------------------
[root@localhost ~]# make
[root@localhost ~]# make altinstall
[root@localhost ~]# python3.6
------------------------------------------------------------------------
Python 3.6.1 (default, Apr 30 2022, 09:19:51)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
------------------------------------------------------------------------
提示:yum安装是基于你centos的yum源,可以通过添加诸如阿里源、163源等进行拓充~
[root@localhost ~]# yum list python3 --------------------------------------------------------------------------- Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.ustc.edu.cn * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com Available Packages python3.i686 3.6.8-18.el7 updates python3.x86_64 3.6.8-18.el7 updates --------------------------------------------------------------------------- [root@localhost ~]# yum install -y python3 --------------------------------------------------------------------------- Installed: python3.x86_64 0:3.6.8-18.el7 Dependency Installed: libtirpc.x86_64 0:0.2.4-0.16.el7 python3-libs.x86_64 0:3.6.8-18.el7 python3-pip.noarch 0:9.0.3-8.el7 python3-setuptools.noarch 0:39.2.0-10.el7 Complete! ---------------------------------------------------------------------------
[root@localhost ~]# python3 -V
---------------------------------
Python 3.6.8
---------------------------------
通过上文下载,我们进行python测试,发现…
[root@localhost ~]# python
-----------------------------------------------------------------------
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() #退出python
-----------------------------------------------------------------------
其实这是我们只是下载了python3,但是并没有指定默认使用python3
提示:下文修改python默认因人而异,习惯问题!!!
因为把默认python路径改成指向python3而不是python2,是个人习惯问题(其实就是懒!!!)
所以如果你大部分时候用的python2,就没有必要修改了,以后需要python3再使用python3指向就好~
[root@localhost ~]# cd /usr/bin [root@localhost bin]# mv python python.backup #移除原来的python指向 [root@localhost bin]# ln -s /usr/local/bin/python3.6 /usr/bin/python #将python指向成python3.6 [root@localhost bin]# python #测试python的指向 ----------------------------------------------------------------------- Python 3.6.1 (default, Apr 30 2022, 09:19:51) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> >>>exit() ----------------------------------------------------------------------- [root@localhost bin]# 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() ----------------------------------------------------------------------- [root@localhost bin]# python3 ----------------------------------------------------------------------- Python 3.6.8 (default, Nov 16 2020, 16:55:22) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> >>> exit() -----------------------------------------------------------------------
提示:为啥俺的python3是3.6.8,因为俺装了三个版本的python~
[root@localhost bin]# python2 -V
--------------------------------------
|Python 2.7.5 |
--------------------------------------
[root@localhost bin]# python3 -V
--------------------------------------
|Python 3.6.8 |
--------------------------------------
[root@localhost bin]# python3.6 -V
--------------------------------------
|Python 3.6.1 |
--------------------------------------
根据实际需求修改具体指向即可~
yum很多依赖的是python2,我猜这也是为啥centos7自带python2的原因
所以!!!重点!!!
[root@localhost bin]# cd /usr/bin [root@localhost bin]# ll yum* ------------------------------------------------- -rwxr-xr-x. 1 root root 801 Aug 5 2017 yum #相关文件都需要更改,这里不只一个!!!(因为是新装未使用且为虚拟机,这里只有一个) -------------------------------------------------- [root@localhost bin]# vi yum [root@localhost bin]# cat yum ----------------------------- #!/usr/bin/python ...... ----------------------------- #将相关文件头中的python指向修改成python2【因为你现在python默认指向python3!!!】 ----------------------------- #!/usr/bin/python2 ...... -----------------------------
[root@localhost bin]# vim /usr/libexec/urlgrabber-ext-down
#同上,将/usr/bin/python改为/usr/bin/python2
[root@localhost ~]# python test2.py
适用于一次性非后台持续运行的测试
[root@localhost ~]# chmod 755 test2.py
[root@localhost ~]# ./test2.py
优点:给.py文件设置了权限
缺点:默认指定python执行【如未修改python默认指向,则未python2运行】
[root@localhost ~]# chmod 755 test2.py
[root@localhost ~]# nohup python3.6 -u test2.py > out_test2.log 2>&1 &
最为推荐,可在后台持续运行,错误会写进日志
[root@localhost ~]# ps -ef | grep python3.6
root 3701 1949 3 11:01 pts/0 00:00:00 python3.6 -u test2.py
root 3741 1949 0 11:01 pts/0 00:00:00 grep --color=auto python3.6
------------------------------------------------------------------------------
#假设test2.py是正在运行的某监控.py,现在要暂停它
[root@localhost ~]# kill 3701
[root@localhost ~]# ps -ef | grep python3.6
root 4064 1949 0 11:03 pts/0 00:00:00 grep --color=auto python3.6
-----------------------------------------------------------------------------
#已经被kill掉,kill的时候要注意端口号
[root@localhost ~]# wget https://bootstrap.pypa.io/pip/3.6/get-pip.py #截止发布文章,该get-pip地址仍存在,如需其他python版本的pip升级,直接找https://bootstrap.pypa.io/pip/这个网址下的get-pip运行即可 ---------------------------------------------------------------------------------- --2022-04-30 21:55:01-- https://bootstrap.pypa.io/pip/3.6/get-pip.py Resolving bootstrap.pypa.io (bootstrap.pypa.io)...151.101.0.175, 151.101.64.175 ... 2022-04-30 21:55:07 (374 KB/s) - ‘get-pip.py’ saved [2159061/2159061] ----------------------------------------------------------------------------------- [root@localhost ~]# python get-pip.py -------------------------------------------------------------------- Collecting pip<22.0 Downloading pip-21.3.1-py3-none-any.whl (1.7 MB) |████████████████████████████████| 1.7 MB 510 kB/s Successfully installed pip-21.3.1 wheel-0.37.1 -------------------------------------------------------------------- #注意:!!!下载了哪个版本的get-pip,就用对应的python版本去执行 #我这里python已修改成默认指向python3.6!!! [root@localhost ~]# pip -V ------------------------------------------------------------------------- pip 21.3.1 from /usr/local/lib/python3.6/site-packages/pip (python 3.6) -------------------------------------------------------------------------
[root@localhost bin]# python -m pip install --upgrade pip -i https://pypi.douban.com/simple #这里指定了国内的源,下载会比国外源快 --------------------------------------------------------------------------------- Collecting pip Downloading https://pypi.doubanio.com/packages/a4/6d/6463d49a933f03ae820f8b/pip-21.3.1-py3-none-any.whl (1.7MB) 100% |████████████████████████████████| 1.7MB 985kB/s Installing collected packages: pip Found existing installation: pip 9.0.1 Uninstalling pip-9.0.1: Successfully uninstalled pip-9.0.1 Successfully installed pip-21.3.1 --------------------------------------------------------------------------------- [root@localhost bin]# pip -V -------------------------------------------------------------------------- pip 21.3.1 from /usr/local/lib/python3.6/site-packages/pip (python 3.6) --------------------------------------------------------------------------
提示:先用whereis pip找下对应pip在哪个版本python,再使用对应版本进行升级!!!
这个错误在日常运行可能都会遇到,原因是编码方式对中文的支持
解决方式:
在文件内容开头添加:# -*- coding: UTF-8 -*-
原因:
这是python3的print()函数自身有限制,不能完全打印所有的unicode字符。
--------------------------------------------------------------
解决方式:
改变标准输出的默认编码,添加如下代码
import io
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8')
如遇乱码(推测是utf-8的兼容性问题),则修改成:
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='gb18030')
提示:以上报错不知道linux是否会出现,这些是之前在win10做python开发时出现的问题!
pip install [包名] -i [pip源]
--------------------------------------------------
清华大学 :https://pypi.tuna.tsinghua.edu.cn/simple/
阿里云:http://mirrors.aliyun.com/pypi/simple/
中国科学技术大学 :http://pypi.mirrors.ustc.edu.cn/simple/
华中科技大学:http://pypi.hustunique.com/
豆瓣源:http://pypi.douban.com/simple/
腾讯源:http://mirrors.cloud.tencent.com/pypi/simple
华为镜像源:https://repo.huaweicloud.com/repository/pypi/simple/
天上飘来一串神奇的Base64编码:
bXpwMDExMEAxNjMuY29t
VVJMOjgxLjY5LjQuMjI5
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。