欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python包管理工具pip

程序员文章站 2022-07-07 08:13:37
你可以使用一个名为 pip 的程序来安装、升级和移除软件包。默认情况下 pip 将从 Python Package Index 安装软件包。你可以在浏览器中访问 Python Package Index 或是使用 pip 受限的搜索功能: (tutorial-e ......

你可以使用一个名为 pip 的程序来安装、升级和移除软件包。默认情况下 pip 将从 python package index <https://pypi.org> 安装软件包。你可以在浏览器中访问 python package index 或是使用 pip 受限的搜索功能:

(tutorial-env) $ pip search astronomy
skyfield               - elegant astronomy for python
gary                   - galactic astronomy and gravitational dynamics.
novas                  - the united states naval observatory novas astronomy library
astroobs               - provides astronomy ephemeris to plan telescope observations
pyastronomy            - a collection of astronomy related tools for python.
...

 

pip 有许多子命令:“search”、“install”、“uninstall”、“freeze”等等。(请参阅  指南以了解 pip 的完整文档。)

您可以通过指定包的名称来安装最新版本的包:

(tutorial-env) $ pip install novas
collecting novas
  downloading novas-3.1.1.3.tar.gz (136kb)
installing collected packages: novas
  running setup.py install for novas
successfully installed novas-3.1.1.3

 

您还可以通过提供包名称后跟 == 和版本号来安装特定版本的包:

(tutorial-env) $ pip install requests==2.6.0
collecting requests==2.6.0
  using cached requests-2.6.0-py2.py3-none-any.whl
installing collected packages: requests
successfully installed requests-2.6.0

 

如果你重新运行这个命令,pip 会注意到已经安装了所请求的版本并且什么都不做。您可以提供不同的版本号来获取该版本,或者您可以运行 pip install --upgrade 将软件包升级到最新版本:

(tutorial-env) $ pip install --upgrade requests
collecting requests
installing collected packages: requests
  found existing installation: requests 2.6.0
    uninstalling requests-2.6.0:
      successfully uninstalled requests-2.6.0
successfully installed requests-2.7.0

 

pip uninstall 后跟一个或多个包名称将从虚拟环境中删除包。

pip show 将显示有关特定包的信息:

(tutorial-env) $ pip show requests
---
metadata-version: 2.0
name: requests
version: 2.7.0
summary: python http for humans.
home-page: http://python-requests.org
author: kenneth reitz
author-email: me@kennethreitz.com
license: apache 2.0
location: /users/akuchling/envs/tutorial-env/lib/python3.4/site-packages
requires:

 

pip list 将显示虚拟环境中安装的所有软件包:

(tutorial-env) $ pip list
novas (3.1.1.3)
numpy (1.9.2)
pip (7.0.3)
requests (2.7.0)
setuptools (16.0)

 

pip freeze` 将生成一个类似的已安装包列表,但输出使用 pip install 期望的格式。一个常见的约定是将此列表放在 requirements.txt 文件中:

(tutorial-env) $ pip freeze > requirements.txt
(tutorial-env) $ cat requirements.txt
novas==3.1.1.3
numpy==1.9.2
requests==2.7.0

 

然后可以将 requirements.txt 提交给版本控制并作为应用程序的一部分提供。然后用户可以使用 install -r 安装所有必需的包:

(tutorial-env) $ pip install -r requirements.txt
collecting novas==3.1.1.3 (from -r requirements.txt (line 1))
  ...
collecting numpy==1.9.2 (from -r requirements.txt (line 2))
  ...
collecting requests==2.7.0 (from -r requirements.txt (line 3))
  ...
installing collected packages: novas, numpy, requests
  running setup.py install for novas
successfully installed novas-3.1.1.3 numpy-1.9.2 requests-2.7.0

 

pip 有更多选择。有关 pip 的完整文档, 请参阅python教程