(一)PIP的使用

itmahy
itmahy
发布于 2024-01-19 / 36 阅读
0
0

(一)PIP的使用

1. pip的作用

pip 是 Python 包管理工具,该工具提供了对Python包的查找、下载、安装、卸载的功能。pip 是一个命令行程序。安装pip后,会向系统添加一个pip命令,该命令可以从命令提示符运行,而不是 python 的命令行中。

$ pip --help
​
Usage:
  pip <command> [options]
​
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.
  --keyring-provider <keyring_provider>
                              Enable the credential lookup via the keyring
                              library if user input is allowed. Specify which
                              mechanism to use [disabled, import, subprocess].
                              (default: disabled)
  --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.

2.常用命令

2.1 查看版本

$ pip --version
pip 23.1 from D:\base\python\lib\site-packages\pip (python 3.10)
​
$ pip3 --version
pip 23.1 from D:\base\python\lib\site-packages\pip (python 3.10)

2.2 升级 pip 命令

$ pip install --upgrade pip
Requirement already satisfied: pip in d:\base\python\lib\site-packages (23.1)
Collecting pip
  WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out. (read timeout=15)")': /packages/50/c2/e06851e8cc28dcad7c155f4753da8833ac06a5c704c109313b8d5a62968a/pip-23.2.1-py3-none-any.whl
  Downloading pip-23.2.1-py3-none-any.whl (2.1 MB)
     ---------------------------------------- 2.1/2.1 MB 12.3 kB/s eta 0:00:00
ERROR: To modify pip, please run the following command:
D:\base\python\python.exe -m pip install --upgrade pip
​
[notice] A new release of pip is available: 23.1 -> 23.2.1
[notice] To update, run: python.exe -m pip install --upgrade pip

2.3 搜索包

C:\Users\LENOVO>pip search ipython
ERROR: XMLRPC request failed [code: -32500]
RuntimeError: PyPI no longer supports 'pip search' (or XML-RPC search). Please use https://pypi.org/search (via a browser) instead. See https://warehouse.pypa.io/api-reference/xml-rpc.html#deprecated-methods for more information.
​
# 报错的原因是 pip search 似乎不再支持,所以使用 pip_search
C:\Users\LENOVO>pip install pip_search
Requirement already satisfied: pip_search in d:\base\python\lib\site-packages (0.0.12)
Requirement already satisfied: bs4 in d:\base\python\lib\site-packages (from pip_search) (0.0.1)
Requirement already satisfied: requests in d:\base\python\lib\site-packages (from pip_search) (2.22.0)
Requirement already satisfied: rich in d:\base\python\lib\site-packages (from pip_search) (10.2.2)
Requirement already satisfied: beautifulsoup4 in d:\base\python\lib\site-packages (from bs4->pip_search) (4.9.3)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in d:\base\python\lib\site-packages (from requests->pip_search) (3.0.4)
Requirement already satisfied: idna<2.9,>=2.5 in d:\base\python\lib\site-packages (from requests->pip_search) (2.8)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in d:\base\python\lib\site-packages (from requests->pip_search) (1.25.11)
Requirement already satisfied: certifi>=2017.4.17 in d:\base\python\lib\site-packages (from requests->pip_search) (2022.12.7)
Requirement already satisfied: colorama<0.5.0,>=0.4.0 in d:\base\python\lib\site-packages (from rich->pip_search) (0.4.6)
Requirement already satisfied: commonmark<0.10.0,>=0.9.0 in d:\base\python\lib\site-packages (from rich->pip_search) (0.9.1)
Requirement already satisfied: pygments<3.0.0,>=2.6.0 in d:\base\python\lib\site-packages (from rich->pip_search) (2.16.1)
Requirement already satisfied: soupsieve>1.2 in d:\base\python\lib\site-packages (from beautifulsoup4->bs4->pip_search) (2.5)
​
[notice] A new release of pip is available: 23.1 -> 23.2.1
[notice] To update, run: python.exe -m pip install --upgrade pip
​
# 使用 pip_search 搜索包
C:\Users\LENOVO>pip_search ipython
结果如果下图

2.4 包管理命令

  • 下载

    pip instal [包名]

    pip install -i [国内源地址] [包名]

    国内源地址

    清华大学:https://pypi.tuna.tsinghua.edu.cn/simple

    阿里云:http://mirrors.aliyun.com/pypi/simple/

    豆瓣:http://pypi.douban.com/simple/

    安装特定版本的包, 通过使用 ==, >=, <=, >, < 来指定一个版本号。

    如:

    $ pip install matplotlib==3.4.1           
    $ pip install 'Markdown<2.0'
    $ pip install 'Markdown>2.0,<2.0.3'
  • 卸载

    pip uninstall [包名]

  • 升级包

    升级包到当前最新的版本,可以使用-U 或者 --upgrade

    pip install --upgrade [包名]

    pip install -U [包名]

  • 显示包的详细信息

    pip show [包名]

  • 列出已安装的包

    pip list

  • 查看需要升级的包

    pip list -o

  • 列出包的描述信息

    pip freeze

    导出已经安装的包列表,并保存到到本地文件中:

    pip freeze > 文件.txt

    pip freeze > requirements.txt

  • 批量安装库

    pip install -r [txt文件名]

    pip install -r requirements.txt

    txt 文件的格式

    netkiller-logging==0.0.4 netkiller-firewall

  • 兼容性检查

    pip check package-name


评论