Ubuntu18.04系统 部署python3.9.0 源码编译安装及pip配置全过程记录(pip3如何采用python3.9解释器,pip3更新)

1.Ubuntu系统镜像下载和基本配置

1.1 镜像下载

镜像下载:https://cn.ubuntu.com/download/desktop

1.2 配置静态IP

配置固定IP方式:
Ubuntu18之前在/etc/network/interfaces进行配置,Ubuntu18及之后版本在/etc/netplan/*.yaml进行配置,如/etc/netplan/01-network-manager-all.yaml,如果路径没有该文件,可以新建一个

netstat -rn  #查看网关
sudo vim /etc/netplan/01-network-manager-all.yaml
#举例:
network:
  version: 2
  renderer: NetworkManager
  ethernets:
    ens33:  # 网卡名称
      dhcp4: no
      dhcp6: no
      addresses: [192.168.100.101/24]  # 本机ip及掩码
      gateway4: 192.168.100.2 # 网关地址
      nameservers:
        addresses: [192.168.100.2]  # DNS跟随网关地址一致,也可以改别的,如[114.114.114.114,8.8.8.8

参考:https://www.shuzhiduo.com/A/kmzLNpKl5G/
yaml文件内容:


network:
  version: 2
  renderer: NetworkManager
  ethernets:
    ens37:
      dhcp4: no
      dhcp6: no
      addresses: [192.168.8.155/24]
      gateway4: 192.168.8.1
      nameservers:
        addresses: [8.8.8.8,114.114.114.114]

在配置完yaml文件后执行:

sudo netplan apply # 修改文件后应用该修改
ifconfig #查看

1.3 yaml文件语法:

  • 1.yaml的基本格式是 k: v (注意:冒号后面有一个空格,而且这个空格是必须的)
    yaml中的字符串默认不用加双引号或者单引号,如果加了双引号或者单引号,则单引号和双引号在yaml中有不同的用法,
        ""双引号不会转义字符串里面的特殊字符:特殊字符会作为本身想表达的意思 例:"张三\n李四"  会表示出 张三  然后是一个换行   然后才是 李四, '' 单引号会转义字符串里面的特殊字符
        例:'张三\n李四'  表示出来的就是一个字符串   张三\n李四
    1. 使用缩进表示层级关系
    1. 缩进时不允许使用Tab键,只允许使用空格。
  • 4.缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
  • 5.大小写敏感
    原文链接:https://blog.csdn.net/hejian_0534/article/details/100577740

配置软件源

换源原因:国内有相关的软件源,用国内的源进行下载时可以提高下载效率。
国内源也是比较多的,经常用到的就有:阿里源,清华源,网易源等。
在我的操作中用到了阿里的源:
阿里apt源:https://developer.aliyun.com/mirror/ubuntu?spm=a2c6h.13651102.0.0.3e221b11YWglBv
换源方式:

  1. 找到/etc/apt/sources.list进行一下备份: sudo cp /etc/apt/sources.list /etc/apt/sources_init.list
  2. vim /etc/apt/sources.list 写入阿里源

deb https://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse

# deb https://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

其他国内的源:
阿里源 https://developer.aliyun.com/mirror/
清华源 https://mirrors.tuna.tsinghua.edu.cn/
华科源 http://mirrors.ustc.edu.cn/
网易源 http://mirrors.163.com/

清华源:

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

中科大:

deb https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

网易云:

deb http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
  1. 更新源完成换源:
sudo apt-get update
sudo apt-get -f install
sudo apt-get upgrade

重要软件安装

网络相关

sudo apt-get openssh-server
sudo apt-get ssh
sudo apt-get net-tools

系统汉化

参考文章:https://blog.csdn.net/weixin_46048542/article/details/120403339

常见问题

  • dpkg lock:
    https://blog.csdn.net/ZHNEYU/article/details/124306452
  • 右键没有新建文档:
    https://www.bkqs.com.cn/content/kpqr6v03r.html

python3.9和pip安装配置

python3.6.9卸载

重要说明:

以下代码不能参照,不要这样卸载原有的python3.6,因为这会导致原有Ubuntu系统的好多功能也被卸载,比如Ubuntu桌面系统和gedit等,慎重慎重

# 1.卸载Python3.6
sudo apt-get remove python3.6
# 2.卸载Python3.6及其依赖
sudo apt-get remove --auto-remove python3.6
# 3.清除python3.6
sudo apt-get purge python3.6

可以采用的方式:

删除python3.6指向python3的软连接,后续的软连接是python3.9->python3的,把原本的pip3重命名。因为pip3是会去/usr/bin/python3去找。
不过这样的方式会引起问题:

  • 1.“ctrl+alt+t”无法打开终端,只能通过右键的方式打开终端
  • 2.gedit打开文件的功能失效
    这是因为我们更换了默认的python3的版本,这样就出问题了。
    我们应该的做法是:
    创建软连接指向python3.9,
    python3.6->python3的软连接不动

python编译安装

  • 安装重要依赖库
    在编译python3.9之前,需要先安装wget,ssl,zlib等重要库,要不然会有问题。
sudo apt install -y wget build-essential libreadline-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev zlib1g
sudo apt-get install -y wget build-essential libreadline-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev zlib1g >install.txt

说明:以上代码的意思:可以通过-y 跳过询问 y/n
apt-get 可以获取deb包到 /var/cache/apt/archives

install.txt 输出重定向到文件内

编译安装

mkdir /usr/local/python3.9.0  #作为编译后的python软件位置
cd /home/lcy/python3.9.0  #源码位置
 ./configure --prefix=/usr/local/python3.9.0
 sudo make
 sudo make install

参考: https://blog.csdn.net/LCY133/article/details/107889704,https://www.jb51.net/article/202175.htm

python和pip配置

在执行完 make install后可以查看到/usr/local/python3.9.0/下有相关的库和bin文件,我们需要建立软连接以便将python解释器链接到/usr/bin/python3上,以后执行python3就是执行的python3.9.0,另外就是pip3.9也要与pip3相关联。

ln -s /usr/local/python3.9.0/bin/python3 /usr/bin/python3.9
ln -s /usr/local/python3.9.0/bin/pip3 /usr/bin/pip3

执行python3

Ubuntu18.04系统 部署python3.9.0 源码编译安装及pip配置全过程记录(pip3如何采用python3.9解释器,pip3更新)_第1张图片

pip3配置:

执行pip --version

lcy@ubuntu:~$ pip3 --version
Traceback (most recent call last):
  File "/usr/bin/pip3", line 5, in <module>
    from pip._internal.cli.main import main
ModuleNotFoundError: No module named 'pip._internal'

可以看到出现了问题,因为这个pip使用的是python3.9的解释器,查看pip3的代码
vim /usr/bin/pip3

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import re
import sys
from pip._internal.cli.main import main
if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(main())

可以查看到这里用的python3的解释器,也就是原有系统python3.6.9的解释器,需要我们手动更换成python3.9
vim /usr/bin/pip3

#!/usr/bin/python3.9
# -*- coding: utf-8 -*-
import re
import sys
from pip._internal.cli.main import main
if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(main())

再次执行pip3 --version,发现已经正常
执行 pip3 install flask
在这里插入图片描述
pip3 选择python版本升级:
在这里插入图片描述python3 -m pip install --upgrade pip

pip 换源安装:

在~目录创建.pip目录,进入.pip目录创建pip3.conf文件,写入换源内容,即可实现永久换源
豆瓣的

[global]
index-url = http://pypi.douban.com/simple
[install]
use-mirrors =true
mirrors =http://pypi.douban.com/simple/
trusted-host =pypi.douban.com

其他源:

清华大学:https://pypi.tuna.tsinghua.edu.cn/simple
阿里云:http://mirrors.aliyun.com/pypi/simple/
豆瓣:http://pypi.douban.com/simple/
清华的:

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
use-mirrors =true
mirrors =https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host =pypi.tuna.tsinghua.edu.cn

阿里的:

[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
use-mirrors =true
mirrors =http://mirrors.aliyun.com/pypi/simple/
trusted-host =mirrors.aliyun.com

换源后对当前pip3进行升级:

python3.9 -m pip install --upgrade pip

其他换源方式:

在pycharm的Manage Repositories中添加源地址即可,
Ubuntu18.04系统 部署python3.9.0 源码编译安装及pip配置全过程记录(pip3如何采用python3.9解释器,pip3更新)_第2张图片

问题:

subprocess.CalledProcessError: Command ‘(‘lsb_release’, ‘-a’)’ returned non-zero exit status 1.
参照博客:
https://blog.csdn.net/qq_41856814/article/details/108714479
删除了文件:/usr/local/lsb_release

你可能感兴趣的:(python,pip,python)