终端下载Python包:
wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tar.xz # 下载
tar xJf Python-3.6.8.tar.xz # 解压
终端安装:
cd Python-3.6.8/
./configure --with-ssl # 配置openssl
make
make install
安装过程中如果出现以下错误:
Traceback (most recent call last):
File "/root/Python-3.6.8/Lib/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/root/Python-3.6.8/Lib/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/root/Python-3.6.8/Lib/ensurepip/__main__.py", line 5, in <module>
sys.exit(ensurepip._main())
File "/root/Python-3.6.8/Lib/ensurepip/__init__.py", line 204, in _main
default_pip=args.default_pip,
File "/root/Python-3.6.8/Lib/ensurepip/__init__.py", line 117, in _bootstrap
return _run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
File "/root/Python-3.6.8/Lib/ensurepip/__init__.py", line 27, in _run_pip
import pip._internal
zipimport.ZipImportError: can't decompress data; zlib not available
make: *** [install] Error 1
解决:
yum install zlib-devel
执行以上命令重新编译安装python即可!
安装成功:
Looking in links: /tmp/tmp84z1_jkh
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-18.1 setuptools-40.6.2
如果后期执行出现以下错误:
Traceback (most recent call last):
...
import ssl
File "/usr/local/lib/python3.6/ssl.py", line 101, in <module>
import _ssl # if we can't import it, let the error propagate
ModuleNotFoundError: No module named '_ssl'
解决:
yum install openssl-devel
执行以上命令重新编译安装python即可!
更多python版本安装包
查看Python:
which python3 # 查看安装地址
python3 -V # 查看安装版本
打开Python终端编程:
python3
进入 Python shell,按下 Delete/Backspace 键:
Python 3.5.7 (default, Mar 29 2017, 11:05:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import ^H^H^H^H^H^H
会出现 ^H 字符,无法正常删除,用起来很不方便。
解决方式
安装 readline 相关模块
查阅文档,发现是因为 readline 相关模块没有安装的原因。readline 是 bash shell 用的库,包含许多使用功能,例如:命令行自动补全。
yum install readline readline-devel
安装完成后,需要再重新编译安装 Python:
cd Python-3.6.8/
make
make install
1.查看当前默认的Python版本
[root@docker ~]# python
Python 3.6.8 (default, Apr 19 2020, 23:57:12)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
2.查看python安装路径:
[root@docker ~]# whereis python
python: /usr/bin/python /usr/bin/python2.7 /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/local/python3/bin/python3.6m /usr/local/python3/bin/python3.6 /usr/local/python3/bin/python3.6m-config /usr/local/python3/bin/python3.6-config /usr/share/man/man1/python.1.gz
3.查看python所有版本及软连接
[root@docker ~]# ls /usr/bin/python* -l
lrwxrwxrwx. 1 root root 30 Apr 20 00:00 /usr/bin/python -> /usr/local/python3/bin/python3
lrwxrwxrwx. 1 root root 9 Apr 12 06:50 /usr/bin/python2 -> python2.7
-rwxr-xr-x. 1 root root 7216 Aug 6 2019 /usr/bin/python2.7
lrwxrwxrwx. 1 root root 7 Apr 12 06:50 /usr/bin/python_bak -> python2
4.删除(备份)软连接
[root@docker ~]# mv /usr/bin/python /usr/bin/python.bak
5.重新创建软连接
[root@docker ~]# ln -s /usr/bin/python2.7 /usr/bin/python
6.再次查看版本
root@docker ~]# python
Python 2.7.5 (default, Aug 7 2019, 00:51:29)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>