27 | Ubuntu18.04.5 安装python3.7及卸载

目录

  • 1 前期准备
    • 1.1安装所需依赖(重要)
    • 1.2 配置Python版本切换
      • 1.2.1 查看候选列表中已有的Python版本
      • 1.2.2 添加 python & python3 指向选择
      • 1.2.3 查看候选列表中已有的Python版本
      • 1.2.4 创建新版python的安装目录(便于卸载)
  • 2 Python3.7(3.7.9–已测试)
    • 2.1 下载源码安装包
    • 2.2 创建安装目录
    • 2.3 解压安装包
    • 2.4配置将要安装的目录
    • 2.5 编译源码
    • 2.6 安装
    • 2.7 添加环境变量
    • 2.8 添加和选择python&python3命令指向
    • 2.9 pip安装依赖包
    • 2.10 删除安装文件
    • 2.11.卸载方法(安装时无需操作)
      • 2.11.1移除指向
      • 2.11.2 删除安装目录,对于Linux而言便是卸载
      • 2.11.3 移除环境变量
  • 3 遇到问题
    • 问题1:update-alternatives: error: no alternatives for python
    • 问题2:configure: error: no acceptable C compiler found in $PATH
    • 问题3:zipimport.ZipImportError: can't decompress data; zlib not available
    • 问题4:Package 'libreadline6' has no installation candidate(待补充)
    • 问题5:E: Unable to locate package libgdbm3(待补充)
    • 问题6:安装pip提示Can't connect to HTTPS URL because the SSL module is not available

1 前期准备

1.1安装所需依赖(重要)

注意:先安装依赖,否则后边编译会报错。

sudo apt-get install libffi-dev uuid-dev lzma-dev liblzma-dev libncurses5-dev libgdbm-dev sqlite3 libsqlite3-dev openssl tcl8.6-dev tk8.6-dev libreadline-dev zlib1g-dev build-essential bzip2 libbz2-1.0 libbz2-dev libc6-dev libdb-dev libexpat1 libexpat1-dev libgdbm3 libncursesw5-dev libpcap-dev libreadline5 libreadline6 libreadline6-dev libsqlite0 libsqlite0-dev libsqlite3-0 libssl-dev libssl1.0.0 libxml2-dev libxslt1-dev sqlite tcl tk tk-dev xz-utils zlib1g zlib1g-dev make  

1.2 配置Python版本切换

Ubuntu18.04.5系统自带有python2.7 和python3.6。

1.2.1 查看候选列表中已有的Python版本

(没有配置过的话,是不存在的)
在这里插入图片描述

1.2.2 添加 python & python3 指向选择

最后的序号表示优先级,数字越大,优先级越高,会自动选取优先级高的作为默认指向,可手动更改默认指向,这里将系统默认的指向的优先级设置为100

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 100
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 99
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 100

1.2.3 查看候选列表中已有的Python版本

sudo update-alternatives --list python
sudo update-alternatives --list python3

在这里插入图片描述
在这里插入图片描述

1.2.4 创建新版python的安装目录(便于卸载)

cd /usr/local
sudo mkdir -p /usr/local/python
sudo chmod -R 777 ./python

下面将进行各个版本的安装,至于版本号的选择
参考地址:https://www.python.org/ftp/python/
27 | Ubuntu18.04.5 安装python3.7及卸载_第1张图片

2 Python3.7(3.7.9–已测试)

2.1 下载源码安装包

wget https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz

27 | Ubuntu18.04.5 安装python3.7及卸载_第2张图片

2.2 创建安装目录

在上述前期准备中,已经在/usr/local目录下创建了一个python目录,实际上安装时如果没有创建会自动创建。

cd /usr/local/python
mkdir ./python3.7

2.3 解压安装包

cd ~
tar -xf ~/Python-3.7.9.tgz

2.4配置将要安装的目录

cd ~/Python-3.7.9
./configure prefix=/usr/local/python/python3.7 --enable-optimizations

27 | Ubuntu18.04.5 安装python3.7及卸载_第3张图片

2.5 编译源码

3.7的编译时间相比其他版本会更长,耐心等待。。。。

cd ~/Python-3.7.9
make -j 2

27 | Ubuntu18.04.5 安装python3.7及卸载_第4张图片27 | Ubuntu18.04.5 安装python3.7及卸载_第5张图片

2.6 安装

一定要使用altinstall,看过的教程有说明过使用install会覆盖安装,可能会导致Ubuntu崩溃的情况。

cd ~/Python-3.7.9
make altinstall  >&1|tee make.log

27 | Ubuntu18.04.5 安装python3.7及卸载_第6张图片

2.7 添加环境变量

安装gedit

apt install gedit

27 | Ubuntu18.04.5 安装python3.7及卸载_第7张图片

gedit ~/.bashrc或者 vim  ~/.bashrc

添加以下内容,其中第一行注释属于个人喜好,可不添加

#[Python3.7]
export PATH=$PATH:/usr/local/python/python3.7/bin

27 | Ubuntu18.04.5 安装python3.7及卸载_第8张图片

让环境变量生效

source ~/.bashrc

2.8 添加和选择python&python3命令指向

添加命令指向【数值比前面设置的默认指向的优先级100小即可】

sudo update-alternatives --install /usr/bin/python python /usr/local/python/python3.7/bin/python3.7 37 
sudo update-alternatives --install /usr/bin/python3 python3 /usr/local/python/python3.7/bin/python3.7 37

在这里插入图片描述
选择python命令的默认版本【这里只是查看,按回车即可】
*号指向应当是Ubuntu系统默认的指向(python2.7)

sudo update-alternatives --config python

27 | Ubuntu18.04.5 安装python3.7及卸载_第9张图片

选择python3命令的默认版本【这里只是查看,按回车即可】
*号指向应当是Ubuntu系统默认的指向(python3.7)

sudo update-alternatives --config python3

27 | Ubuntu18.04.5 安装python3.7及卸载_第10张图片

python3.7的启动命令

python3.7 ==> python3.7
这样之后,下面是各命令的启动版本
python2 ==> python2.7
python ==> python2.7
python3 ==> python3.5
python3.6 ==> python3.6
python3.7 ==> python3.7

2.9 pip安装依赖包

对于python3.7,安装后使用pip请以python3.7 -m pip install xxx的形式使用,同时,对于python3.7的使用,建议明确指明版本来使用,而不建议更改python3的指向,因为Ubuntu的图形界面一定程度上是依赖自带的两个版本的python,更改指向可能会带来意想不到的问题。

可更新python3.7自带的pip的版本,也可不更新,一般原版的更配,只是新版的进度条更好看,也没有版本更新警告。

python3.7 -m pip install --upgrade pip  

如果想切换回原版的

python3.7 -m pip install --upgrade pip==20.1.1
python3.7 -m pip install flask-socketio
python3.7 -m pip install numpy -i https://pypi.douban.com/simple/
python3.7 -m pip install pandas -i https://pypi.douban.com/simple/
python3.7 -m pip install kafka
python3.7 -m pip install kafka-python

2.10 删除安装文件

cd ~
sudo rm -r ./Python-3.7.9
rm -r ./Python-3.7.9.tgz

最好进行一次系统重启

2.11.卸载方法(安装时无需操作)

这里给出卸载的命令,如果不需要了,可以进行卸载。

2.11.1移除指向

sudo update-alternatives --remove python /usr/local/python/python3.7/bin/python3.7
sudo update-alternatives --remove python3 /usr/local/python/python3.7/bin/python3.7

2.11.2 删除安装目录,对于Linux而言便是卸载

rm -r /usr/local/python/python3.7

2.11.3 移除环境变量

gedit ~/.bashrc

把上面添加的环境变量内容删除即可

source ~/.bashrc

3 遇到问题

问题1:update-alternatives: error: no alternatives for python

问题2:configure: error: no acceptable C compiler found in $PATH

27 | Ubuntu18.04.5 安装python3.7及卸载_第11张图片
解决方法:

apt  install gcc

问题3:zipimport.ZipImportError: can’t decompress data; zlib not available

现象:

Creating directory /usr/local/python/python3.7/share/man/man1
/usr/bin/install -c -m 644 ./Misc/python.man \
	/usr/local/python/python3.7/share/man/man1/python3.7.1
if test "xupgrade" != "xno"  ; then \
	case upgrade in \
		upgrade) ensurepip="--altinstall --upgrade" ;; \
		install|*) ensurepip="--altinstall" ;; \
	esac; \
	 ./python -E -m ensurepip \
		$ensurepip --root=/ ; \
fi
Traceback (most recent call last):
  File "/root/Python-3.7.9/Lib/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/root/Python-3.7.9/Lib/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/root/Python-3.7.9/Lib/ensurepip/__main__.py", line 5, in <module>
    sys.exit(ensurepip._main())
  File "/root/Python-3.7.9/Lib/ensurepip/__init__.py", line 214, in _main
    default_pip=args.default_pip,
  File "/root/Python-3.7.9/Lib/ensurepip/__init__.py", line 127, in _bootstrap
    return _run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
  File "/root/Python-3.7.9/Lib/ensurepip/__init__.py", line 32, in _run_pip
    runpy.run_module("pip", run_name="__main__", alter_sys=True)
  File "/root/Python-3.7.9/Lib/runpy.py", line 201, in run_module
    mod_name, mod_spec, code = _get_module_details(mod_name)
  File "/root/Python-3.7.9/Lib/runpy.py", line 142, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "/root/Python-3.7.9/Lib/runpy.py", line 109, in _get_module_details
    __import__(pkg_name)
zipimport.ZipImportError: can't decompress data; zlib not available
make: *** [altinstall] Error 1
Makefile:1151: recipe for target 'altinstall' failed

解决方法:

apt install  -y  zlib*

问题4:Package ‘libreadline6’ has no installation candidate(待补充)

现象:

root@prd-devel-00:~/Python-3.7.9# sudo apt-get install libffi-dev uuid-dev lzma-dev liblzma-dev libncurses5-dev libgdbm-dev sqlite3 libsqlite3-dev openssl tcl8.6-dev tk8.6-dev libreadline-dev zlib1g-dev build-essential bzip2 libbz2-1.0 libbz2-dev libc6-dev libdb-dev libexpat1 libexpat1-dev libgdbm3 libncursesw5-dev libpcap-dev libreadline5 libreadline6 libreadline6-dev libsqlite0 libsqlite0-dev libsqlite3-0 libssl-dev libssl1.0.0 libxml2-dev libxslt1-dev sqlite tcl tk tk-dev xz-utils zlib1g zlib1g-dev make 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Note, selecting 'libreadline-dev' instead of 'libreadline6-dev'
Package libreadline6 is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Unable to locate package libgdbm3
E: Package 'libreadline6' has no installation candidate

解决方法:
待补充

问题5:E: Unable to locate package libgdbm3(待补充)

解决方法:
待补充

问题6:安装pip提示Can’t connect to HTTPS URL because the SSL module is not available

现象:

Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
Requirement already up-to-date: pip==20.1.1 in /usr/local/python/python3.7/lib/python3.7/site-packages (20.1.1)
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping

解决方法:

你可能感兴趣的:(#,Linux,linux,服务器,运维)