文章来源 | 恒源云社区
原文地址 | 【小技巧-安装篇】
1、执行命令或程序报找不到包,如何安装?
参考 常用命令,使用 apt 安装系统软件或 pip 安装 Python 包。
2、如何设置pip加速?
如果Python的pip安装库或者包总是很慢,可以更换为国内地址后进行下载
国内常见的pip源下载地址如下:
阿里云: https://mirrors.aliyun.com/pypi/simple/
腾讯云:https://mirrors.cloud.tencent.com/pypi/simple
豆瓣(douban): https://pypi.douban.com/simple/
中国科技大学: https://pypi.mirrors.ustc.edu.cn/simple/
清华大学: https://pypi.tuna.tsinghua.edu.cn/simple/
临时使用
#通过 -i 参数指定所需要使用的pip源地址去安装numpy库
pip install numpy -i https://mirrors.cloud.tencent.com/pypi/simple
永久使用
#通过该命令设置永久使用该pip镜像源
pip config set global.index-url https://mirrors.cloud.tencent.com/pypi/simple
#修改完成后通过pip命令安装所需要的包
pip install numpy
3、如何升级pip包管理工具?
#升级前查看pip版本
pip -V #如下返回pip版本为21.0.1
pip 21.0.1 from /usr/local/lib/python3.8/dist-packages/pip (python 3.8)
#升级 pip 到最新的版本
pip install pip -U
#也可以临时使用指定镜像来升级 pip
pip install -i https://mirrors.cloud.tencent.com/pypi/simple --upgrade pip
#升级后查看pip版本
pip -V #如下显示22.0.4
pip 22.0.4 from /usr/local/lib/python3.8/dist-packages/pip (python 3.8)
4、如何更换Ubuntu apt源?
如果使用apt源下载东西很慢或者报错,则可以先通过apt-get update -y命令更新apt源对实例产生缓存,然后通过apt-get install 包名安装所需要的包。
国内常见的apt源下载地址如下,下面镜像源中仅限实例中的Ubuntu 18.04系统版本使用,可通过lsb_release -a | grep Release名称查询系统版本。
阿里云
#复制以下所有命令粘贴到实例后回车替换阿里云apt镜像源
cat > /etc/apt/sources.list << EOF
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
EOF
apt-get update -y
华为云
#复制以下所有命令粘贴到实例后回车替换华为云apt镜像源
cat > /etc/apt/sources.list << EOF
deb http://repo.huaweicloud.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://repo.huaweicloud.com/ubuntu/ bionic main restricted universe multiverse
deb http://repo.huaweicloud.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://repo.huaweicloud.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://repo.huaweicloud.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://repo.huaweicloud.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://repo.huaweicloud.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://repo.huaweicloud.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://repo.huaweicloud.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://repo.huaweicloud.com/ubuntu/ bionic-backports main restricted universe multiverse
EOF
apt-get update -y
清华大学
#复制以下所有命令粘贴到实例后回车替换腾讯云apt镜像源
cat > /etc/apt/sources.list << EOF
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
EOF
apt-get update -y
中国科技大学
#复制以下所有命令粘贴到实例后回车替换腾讯云apt镜像源
cat > /etc/apt/sources.list << EOF
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-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-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
EOF
apt-get update -y