使用pip更新包的方法是什么? 这些都不起作用:

pip update
pip upgrade

我知道这是一个简单的问题,但它是必要的,因为它不那么容易找到(pip文档不会弹出和堆栈溢出的其他问题是相关的,但并不完全是关于这个)


当前回答

在命令提示符中执行以下命令,

C:\Users\Owner\AppData\Local\Programs\Python\Python310>python -m pip install --upgrade pip

输出如下所示,

Requirement already satisfied: pip in c:\users\owner\appdata\local\programs\python\python310\lib\site-packages (21.2.4)
Collecting pip
  Downloading pip-22.0.3-py3-none-any.whl (2.1 MB)
     |████████████████████████████████| 2.1 MB 3.3 MB/s
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 21.2.4
    Uninstalling pip-21.2.4:
      Successfully uninstalled pip-21.2.4
Successfully installed pip-22.0.3

其他回答

在命令提示符中执行以下命令,

C:\Users\Owner\AppData\Local\Programs\Python\Python310>python -m pip install --upgrade pip

输出如下所示,

Requirement already satisfied: pip in c:\users\owner\appdata\local\programs\python\python310\lib\site-packages (21.2.4)
Collecting pip
  Downloading pip-22.0.3-py3-none-any.whl (2.1 MB)
     |████████████████████████████████| 2.1 MB 3.3 MB/s
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 21.2.4
    Uninstalling pip-21.2.4:
      Successfully uninstalled pip-21.2.4
Successfully installed pip-22.0.3

当偏离主题时,人们可能会想到这个问题,希望更新pip本身(参见这里)。

要为Python3.4+升级pip,必须按如下方式使用pip3:

sudo pip3 install pip --upgrade

这将升级位于:/usr/local/lib/python3.的pipX / dist-packages

否则,要为Python2.7升级pip,您可以如下所示使用pip:

sudo pip install pip --upgrade

这将升级位于:/usr/local/lib/python2.7/dist-packages的pip

我使用下面的行来更新我所有过期的包:

pip list --outdated --format=freeze | awk -F '==' '{print $1}' | xargs -n1 pip install -U

此外,在Jupyter笔记本中,通过在代码单元中运行下面的代码,您可以更新您的包:

%pip install <package_name> --upgrade

在终端中使用此代码:

python -m pip install --upgrade PACKAGE_NAME

例如,我想更新pip包:

python -m pip install --upgrade pip

更多的例子:

python -m pip install --upgrade selenium
python -m pip install --upgrade requests
...