Python模块安装

8 篇文章 0 订阅
3 篇文章 0 订阅

Python模块安装与删除

目录

Python模块安装与删除

Python与Python3

配置镜像源

升级pip模块

查找模块

 安装单个模块

删除单个模块

查看缓存

清除缓存

删除所有已安装模块(mercurial不可删除,用于版本控制)

批量安装模块

下载安装WHL文件

完整脚本


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

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值