默认情况下 pip 将会从 Python Package Index,<https://pypi.python.org/pypi>, 中安装包。你可以通过 web 浏览器浏览它们,或者你也能使用 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 有许多子命令:“搜索”,“安装”,“卸载”,“freeze”(译者注:这个词语暂时没有合适的词语来翻译),等等。(请参考 installing-index 指南获取 pip 更多完整的文档。)


你可以安装一个包最新的版本,通过指定包的名称:


-> 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

你也能安装一个指定版本的包,通过给出包名后面紧跟着 == 和版本号:


-> 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 install requests==2.6.0),pip 会注意到要求的版本已经安装,不会去做任何事情。你也可以提供一个不同的版本号来安装,或者运行 pip install --upgrade 来升级包到最新版本:


-> 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 安装所有必须的包:


-> 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 还有更多的选项。请参考 installing-index 指南获取关于 pip 完整的文档。

当你编写一个包并且在 Python Package Index 中也出现的话,请参考 distributing-index 指南。

最后修改:2022 年 12 月 05 日
如果觉得我的文章对你有用,请随意赞赏