欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 培训 > pip笔记

pip笔记

2024/10/24 19:17:26 来源:https://blog.csdn.net/qq_27843945/article/details/141029137  浏览:    关键词:pip笔记

pip介绍

pip的全称:package installer for python,也就是Python包管理工具。

配置镜像源

镜像列表

  1. 阿里云 http://mirrors.aliyun.com/pypi/simple/
  2. 中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
  3. 豆瓣 http://pypi.douban.com/simple/
  4. 清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
  5. 中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/

临时使用

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple numpy

永久使用

  1. windows下在user目录中创建一个pip文件夹(一般都是不存在的 ,需要自己创建), 如C:\Users\jarvis\pip,在pip文件夹中新建文件pip.ini,内容如下:
[global]
timeout = 40
index-url = https://mirrors.aliyun.com/pypi/simple/
extra-index-url = https://pypi.mirrors.ustc.edu.cn/simple/http://pypi.douban.com/simple/https://pypi.tuna.tsinghua.edu.cn/simple/http://pypi.mirrors.ustc.edu.cn/simple/
[install]
trusted-host = mirrors.aliyun.compypi.mirrors.ustc.edu.cnpypi.douban.compypi.tuna.tsinghua.edu.cnpypi.mirrors.ustc.edu.cn
  1. 查看更改后的镜像源:pip config list

常用方法

查看包

# 查看版本号
pip --version 	# 查看所有已安装包
pip list  # 查看包详情
pip show -f beautifulsoup4  # 验证已经安装的包是否有兼容依赖问题
pip check beautifulsoup4  

更新pip

# 方法1
pip install --upgrade pip  # 方法2
python -m pip install --upgrade pip  

安装卸载

# 安装包,默认安装最新版本
pip install beautifulsoup4  # 临时使用指定源安装
pip install beautifulsoup4 -i https://pypi.tuna.tsinghua.edu.cn/simple # 安装指定版本包
pip install mataplotlib==3.4.1 	# 批量安装包
pip install -r d:\\requirments.txt 	# 离线安装 *.whl 包 
pip install PyQt5-5.10.1-5.10.1-cp35.cp36.cp37.cp38-none-win_amd64.whl  # 卸载包
pip uninstall beautifulsoup4  # 重新安装pip, -U表示自动安装最新版
easy_install -U pip   

升级包

# 查看需要升级的包 完整写法:pip list --outdated 
pip list -o # 升级包
pip install --upgrade beautifulsoup4  

其他

# 下载django包到本地桌面 
pip download django -d C:\Users\jarvis\Desktop  # 设置默认镜像源
pip config setglobal.index-url https://pypi.tuna.tsinghua.edu.cn/simple  

环境打包及安装

打包方法1:freeze 打包

pip freeze 会将当前PC环境下所有的安装包都进行生成,再进行安装的时候会全部安装很多没有的包.耗时耗力其实是不可取的。

# pip 自带方法 不推荐 可能会丢失依赖包的版本号
pip freeze > requirements.txt
# 或者
pip list --format=freeze> requirements.txt

打包方法2:pipreqs 打包

使用pipreqs,这个工具的好处是可以通过对项目目录的扫描,发现使用了哪些库,生成依赖清单。

# 安装 pipreqs
pip install pipreqs
# 生成
pipreqs . --encoding=utf8 --force

安装所有包

# 安装
pip install -r requirements.txt

卸载所有包

使用命令,按照导出的库的列表,卸载库文件

pip uninstall -r requirements.txt -y

至此,所有的第三方库就已经卸载干净,运行pip list会得到如下结果:

Package    Version
---------- -------
pip        24.0
setuptools 40.8.0

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com