Python pip常用命令详解 VS 10个使用pip的小技巧

2022-08-2209:34:53编程语言入门到精通Comments985 views字数 2480阅读模式

安装

当然在Python 3.4版本之后以及Python 2.7.9版本之后,官网的安装包当中就已经自带了pip,用户直接在安装完Python之后就可以直接使用,要是使用由virtualenv或者pyvenv创建的虚拟环境,那么pip也是被默认安装的。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27420.html

如果是需要自己另外安装pip包的,在已经配置好Python的环境当中运行下面这个命令行。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27420.html

py -m ensurepip --upgrade

另外一种方式是从官网上(https://bootstrap.pypa.io/get-pip.py)直接下载get-pip.py脚本,然后直接运行python get-pip.py脚本即可。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27420.html

如何使用

安装后,在命令行中输入pip,然后按下回车,就会出现下图所示的使用说明:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27420.html

Python pip常用命令详解 VS 10个使用pip的小技巧

升级

要是你觉得自己的pip版本有点低,想要升级一下的话,在命令行中输入以下命令。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27420.html

pip install --upgrade pip

或者是。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27420.html

pip install -U pip

安装某个版本的包

如果打算用pip来安装第三方的包,用的是以下的命令行。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27420.html

pip install package-name

例如我们想要安装指定版本的第三方的包,例如安装3.4.1版本的matplotlib,文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27420.html

pip install matplotlib==3.4.1

卸载或者是更新包

要是你打算想要卸载某个包,该要输入的命令行是。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27420.html

pip uninstall package_name

而如果打算更新某个包,对应的命令行是。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27420.html

pip install --upgrade package_name
# 或者是
pip install -U package_name

查看某个包的信息

可以通过以下的这个命令行来查看指定包的信息,文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27420.html

pip show -f requests

output文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27420.html

Name: requests
Version: 2.24.0
Summary: Python HTTP for Humans.
Home-page: https://requests.readthedocs.io
Author: Kenneth Reitz
Author-email: me@kennethreitz.org
License: Apache 2.0
Location: c:\users\pc120\pycharmprojects\pythonproject1\venv\lib\site-packages
Requires: certifi, chardet, idna, urllib3
Required-by: etelemetry, gTTS, pandas-datareader, pandas-profiling, pyler, pywhatkit, pyxnat, streamlit, tushare, wikipedia, yfinance
Files:
  requests-2.24.0.dist-info\DESCRIPTION.rst
  requests-2.24.0.dist-info\INSTALLER
  .......

查看需要被升级的包

我们需要查看一下现有的这些包中,哪些是需要是被升级的,可以用下面这行命令行来查看,文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27420.html

pip list -o

output文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27420.html

Package    Version Latest Type
---------- ------- ------ -----
docutils   0.15.2  0.18.1 wheel
PyYAML     5.4.1   6.0    wheel
rsa        4.7.2   4.8    wheel
setuptools 56.0.0  62.1.0 wheel

查看兼容问题

在下载安装一些标准库的时候,需要考虑到兼容问题,一些标准库的安装可能需要依赖其他的标准库,会存在版本相冲突等问题,我们先用下面这条命令行来检查一下是否会有冲突的问题存在。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27420.html

pip check package_name

当然要是我们不指定是哪个标准库的话,会检查现在已经安装的所有包中的是否存在版本冲突等问题文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27420.html

pip check

output文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27420.html

yfinance 0.1.70 has requirement requests>=2.26, but you have requests 2.24.0.
selenium 4.1.0 has requirement urllib3[secure]~=1.26, but you have urllib3 1.25.11.

指定国内源来安装

我们要是感觉到安装的速度有点慢,可以指定国内的源来安装某个包,例如。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27420.html

pip install -i https://pypi.douban.com/simple/ package_name

国内源有文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27420.html

清华:https://pypi.tuna.tsinghua.edu.cn/simple
阿里云:http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
华中理工大学:http://pypi.hustunique.com/
山东理工大学:http://pypi.sdutlinux.org/ 
豆瓣:http://pypi.douban.com/simple/

下载包但是不安装

要是我们想要下载某个包到指定的路径下,命令行如下。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27420.html

pip download package_name -d "某个路径"

例如文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27420.html

pip download requests -d "."

就是在当前的目录下下载requests模块以及其他所要依赖的模块。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27420.html

批量安装软件包

我们一般在看到别人的项目时,都会包含一个requirements.txt文件,里面包含了一些Python项目当中需要用到的第三方库。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27420.html

Python pip常用命令详解 VS 10个使用pip的小技巧

要生成这种txt文件,需要这么来做。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27420.html

pip freeze > requirements.txt

而如果我们需要来批量安装第三方库,在命令行中输入以下这个命令。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27420.html

pip install -r requirements.txt
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27420.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/27420.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定