Python模块安装与删除
目录
删除所有已安装模块(mercurial不可删除,用于版本控制)
Python与Python3
Python2.x版本与Python3.x版本的区别:将以下命令中python3和pip3全部替换为python和pip。
配置镜像源
在以下三个镜像源中,选择一个。推荐使用网易镜像源,速度最快(但有些包不全)。中科大与清华大学镜像源的包很全。
pip3 config set global.index-url https://mirrors.163.com/pypi/simple
或者:
pip3 config set global.index-url https://mirrors.ustc.edu.cn/pypi/web/simple
运行结果如下:
Writing to /Users/username/.config/pip/pip.conf
文件内容如下:
[global]
index-url = https://mirrors.163.com/pypi/simple
BASH脚本可以选择多个镜像源中进行配置:
#!/bin/bash
#使用网易镜像源
MIRROR="https://mirrors.163.com/pypi/simple"
#使用中科大镜像源
#MIRROR="https://mirrors.ustc.edu.cn/pypi/web/simple"
#使用清华大学镜像源
#MIRROR="https://pypi.tuna.tsinghua.edu.cn/simple"
pip3 config set global.index-url ${MIRROR}
升级pip模块
#!/bin/bash
#使用网易镜像源
MIRROR="https://mirrors.163.com/pypi/simple"
upgrade_pip(){
if [ "$(which pip)" == "" ]
then
python -m ensurepip --user --default-pip
python3 -m ensurepip --user --default-pip
pip install -i ${MIRROR} --user --upgrade pip
pip3 install -i ${MIRROR} --user --upgrade pip
fi
}
upgrade_pip
查找模块
pip3 search module_name
pip3 search numpy
安装单个模块
pip3 install module_name
例如:安装numpy模块
命令:
pip3 install numpy
运行结果:
Collecting numpy
Downloading numpy-1.19.4-cp39-cp39-macosx_10_9_x86_64.whl (15.4 MB)
|████████████████████████████████| 15.4 MB 4.5 MB/s
Installing collected packages: numpy
Successfully installed numpy-1.19.4
删除单个模块
pip3 uninstall module_name
pip3 uninstall numpy
查看缓存
pip3 cache list
清除缓存
pip3 cache purge
删除所有已安装模块(mercurial不可删除,用于版本控制)
BASH脚本(需要用户确认)
#!/bin/bash
uninstall_all(){
PKG_ALL=$(pip3 freeze | sed 's/mercurial.*//')
if [ "$PKG_ALL" != "" ]
then
echo $PKG_ALL
pip3 uninstall $PKG_ALL
fi
}
uninstall
BASH脚本(不需要用户确认)
#!/bin/bash
uninstall_all(){
PKG_ALL=$(pip3 freeze | sed 's/mercurial.*//')
if [ "$PKG_ALL" != "" ]
then
echo $PKG_ALL
pip3 uninstall -y $PKG_ALL
fi
}
uninstall
批量安装模块
需要安装的模块在PKG变量中进行自定义。
BASH脚本:
#!/bin/bash
#安装模块
PKG="ipython jupyter matplotlib numpy openpyxl pandas pillow pygame scipy scikit-learn seaborn statsmodels xlrd xlwt xlwings"
#使用网易镜像源
MIRROR="https://mirrors.163.com/pypi/simple"
install(){
if [ "$(which pip)" == "" ]
then
python3 -m ensurepip --user --default-pip
fi
pip3 install -i ${MIRROR} pip -U
pip3 install -i $MIRROR $PKG
}
install
下载安装WHL文件
#!/bin/bash
#安装模块
PKG="ipython jupyter matplotlib numpy openpyxl pandas pillow pygame scipy scikit-learn seaborn statsmodels xlrd xlwt xlwings"
#使用网易镜像源
MIRROR="https://mirrors.163.com/pypi/simple"
download(){
if test -d wheel
then
echo 'wheel目录已存在!'
else
echo 'wheel目录不存在!创建wheel目录'
mkdir wheel
fi
cd wheel
pip3 download $PKG
cd ..
}
download
删除所有已安装模块
pip
pip uninstall -y `pip freeze | sed 's/ @.*//' | sed 's/==.*//'`
pip3
pip3 uninstall -y `pip3 freeze | sed 's/ @.*//' | sed 's/==.*//'`
完整脚本
可以自行修改,增加命令参数,如:install, uninstall, uninstall_all, download(待续...)
#!/bin/bash
#安装模块
PKG="wheel bs4 ipython jupyter matplotlib numpy openpyxl pandas pillow pygame requests scipy scikit-learn seaborn statsmodels xlrd xlwt xlwings"
#使用网易镜像源
MIRROR="https://mirrors.163.com/pypi/simple"
#使用中科大镜像源
#MIRROR="https://mirrors.ustc.edu.cn/pypi/web/simple"
#使用清华大学镜像源
#MIRROR="https://pypi.tuna.tsinghua.edu.cn/simple"
set_mirror(){
pip3 config set global.index-url ${MIRROR}
}
upgrade_pip(){
if [ "$(which pip)" == "" ]
then
python3 -m ensurepip --user --default-pip
pip3 install -i ${MIRROR} --user --upgrade pip
pip3 config set global.index-url ${MIRROR}
fi
}
download(){
if test -d wheel
then
echo 'wheel目录已存在!'
else
echo 'wheel目录不存在!创建wheel目录'
mkdir wheel
fi
echo pip3 download -d ./wheel/ $PKG
pip3 download -d ./wheel/ $PKG
cd ..
}
install(){
pip3 install -i $MIRROR $PKG
}
purge_all(){
echo pip3 cache purge
pip3 cache purge
}
uninstall(){
pip3 uninstall -y $PKG
}
uninstall_all(){
PKG_ALL=$(pip3 freeze | sed 's/mercurial.*//')
if [ "$PKG_ALL" != "" ]
then
echo $PKG_ALL
pip3 uninstall -y $PKG_ALL
fi
}
set_mirror
upgrade_pip
uninstall $PKG
purge_all
download
install