PIP是通用的Python包管理工具,可以方便安装、列出,卸载python的模块/库/包等。

注意:在Python3.4(一说是3.6)及更新的版本中,PIP已经捆绑安装了,不需要再单独安装(应该需要更新)。

安装卸载

常见使用, 例如:
cmd下:
安装pycurl

pip install pycurl

列出已经安装的python

pip list

输出pycurl包的信息

pip show pycurl

卸载pycurl

pip uninstall pycurl

导出包名到

pip freeze > package20210627.txt

pip国内位置

-i https://mirrors.aliyun.com/pypi/simple/

批量安装和卸载

pip install -r package.txt
pip uninstall -r package.txt
pip install -r “F:\Program Project\python\packages\packages.txt”

添加环境变量

path=%path%;C:\Python27

安装pip

python -m pip install pip
上代码提示:No module named pip
py2 -m ensurepip

升级pip

python37 -m pip install --upgrade pip

查看可更新包:

pip list --outdated --format=columns

安装批量更新命令

pip install pip-review

依次更新所有包

pip -review --local --interactive
pip list --outdated #列出所有过期的库
pip install --upgrade 库名

python pip 删除所有包

导出所有包
pip freeze > requirements.txt
删除所有包
pip uninstall -r requirements.txt
or
pip uninstall -r requirements.txt -y

命令

Commands:
  install                     Install packages.
  download                    Download packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  inspect                     Inspect the python environment.
  list                        List installed packages.
  show                        Show information about installed packages.
  check                       Verify installed packages have compatible dependencies.
  config                      Manage local and global configuration.
  search                      Search PyPI for packages.
  cache                       Inspect and manage pip's wheel cache.
  index                       Inspect information available from package indexes.
  wheel                       Build wheels from your requirements.
  hash                        Compute hashes of package archives.
  completion                  A helper command used for command completion.
  debug                       Show information useful for debugging.
  help                        Show help for commands.

常用选项

General Options:
  -h, --help                  Show help.
  --debug                     Let unhandled exceptions propagate outside the
                              main subroutine, instead of logging them to
                              stderr.
  --isolated                  Run pip in an isolated mode, ignoring
                              environment variables and user configuration.
  --require-virtualenv        Allow pip to only run in a virtual environment;
                              exit with an error otherwise.
  --python <python>           Run pip with the specified Python interpreter.
  -v, --verbose               Give more output. Option is additive, and can be
                              used up to 3 times.
  -V, --version               Show version and exit.
  -q, --quiet                 Give less output. Option is additive, and can be
                              used up to 3 times (corresponding to WARNING,
                              ERROR, and CRITICAL logging levels).
  --log <path>                Path to a verbose appending log.
  --no-input                  Disable prompting for input.
  --proxy <proxy>             Specify a proxy in the form
                              scheme://[user:passwd@]proxy.server:port.
  --retries <retries>         Maximum number of retries each connection should
                              attempt (default 5 times).
  --timeout <sec>             Set the socket timeout (default 15 seconds).
  --exists-action <action>    Default action when a path already exists:
                              (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.
  --trusted-host <hostname>   Mark this host or host:port pair as trusted,
                              even though it does not have valid or any HTTPS.
  --cert <path>               Path to PEM-encoded CA certificate bundle. If
                              provided, overrides the default. See 'SSL
                              Certificate Verification' in pip documentation
                              for more information.
  --client-cert <path>        Path to SSL client certificate, a single file
                              containing the private key and the certificate
                              in PEM format.
  --cache-dir <dir>           Store the cache data in <dir>.
  --no-cache-dir              Disable the cache.
  --disable-pip-version-check
                              Don't periodically check PyPI to determine
                              whether a new version of pip is available for
                              download. Implied with --no-index.
  --no-color                  Suppress colored output.
  --no-python-version-warning
                              Silence deprecation warnings for upcoming
                              unsupported Pythons.
  --use-feature <feature>     Enable new functionality, that may be backward
                              incompatible.
  --use-deprecated <feature>  Enable deprecated functionality, that will be
                              removed in the future.

常见问题

问题一

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.

pip 的依赖项解析器当前未考虑安装的所有包。此行为是以下依赖项冲突的根源。

Installing collected packages: mpmath, sympy, pillow, numpy, networkx, MarkupSafe, idna, filelock, charset-normalizer, certifi, requests, jinja2, torch, torchvision, torchaudio
  Attempting uninstall: torchvision
    Found existing installation: torchvision 0.14.1
    Uninstalling torchvision-0.14.1:
      Successfully uninstalled torchvision-0.14.1
  Attempting uninstall: torchaudio
    Found existing installation: torchaudio 0.13.1
    Uninstalling torchaudio-0.13.1:
      Successfully uninstalled torchaudio-0.13.1
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
transformers 4.26.1 requires huggingface-hub<1.0,>=0.11.0, which is not installed.
transformers 4.26.1 requires packaging>=20.0, which is not installed.
transformers 4.26.1 requires pyyaml>=5.1, which is not installed.
transformers 4.26.1 requires regex!=2019.12.17, which is not installed.
transformers 4.26.1 requires tokenizers!=0.11.3,<0.14,>=0.11.1, which is not installed.
Successfully installed MarkupSafe-2.1.2 certifi-2022.12.7 charset-normalizer-2.1.1 filelock-3.9.0 idna-3.4 jinja2-3.1.2 mpmath-1.2.1 networkx-3.0rc1 numpy-1.24.1 pillow-9.3.0 requests-2.28.1 sympy-1.11.1 torch-2.1.0.dev20230312+cpu torchaudio-2.0.0.dev20230312+cpu torchvision-0.15.0.dev20230312+cpu
实例

重新安装这几个包

问题二

Read timed out 超时问题

一般windows系统出现这个问题,可以在命令后面加上--user参数,类似:

pip install pyinstaller 
# 上句报超时错误
pip install pyinstaller --user

使用–help去查看–user的作用

大意就是:把这个包换个地方安装

问题三

你想要安装一个第三方包,但是没有权限将它安装到系统Python库中去。 或者,你可能想要安装一个供自己使用的包,而不是系统上面所有用户。

Python有一个用户安装目录,通常类似”~/.local/lib/python3.3/site-packages”。 要强制在这个目录中安装包,可使用安装选项“–user”。例如:

python3 setup.py install --user

或者

pip install --user packagename

在sys.path中用户的“site-packages”目录位于系统的“site-packages”目录之前。 因此,你安装在里面的包就比系统已安装的包优先级高 (尽管并不总是这样,要取决于第三方包管理器,比如distribute或pip)。

讨论

通常包会被安装到系统的site-packages目录中去,路径类似“/usr/local/lib/python3.3/site-packages”。 不过,这样做需要有管理员权限并且使用sudo命令。 就算你有这样的权限去执行命令,使用sudo去安装一个新的,可能没有被验证过的包有时候也不安全。

安装包到用户目录中通常是一个有效的方案,它允许你创建一个自定义安装。

另外,你还可以创建一个虚拟环境。

问题四

Installing build dependencies error安装构建依赖错误


一般这种都是因为缺乏一些依赖包导致的,可以考虑直接使用conda安装,

conda install -c conda-forge pyinstaller

问题五

pip install 没反应怎么办

python -m pip install便可

参考

10.13 安装私有的包
python用pip install时安装失败的一系列问题及解决方法