python包管理工具pip

你能夠使用一個名爲 pip 的程序來安裝、升級和移除軟件包。默認狀況下 pip 將從 Python Package Index <https://pypi.org> 安裝軟件包。你能夠在瀏覽器中訪問 Python Package Index 或是使用 pip 受限的搜索功能:html

(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」等等。(請參閱 python pip安裝 指南以瞭解 pip 的完整文檔。)python

您能夠經過指定包的名稱來安裝最新版本的包:瀏覽器

(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

 

您還能夠經過提供包名稱後跟 == 和版本號來安裝特定版本的包:bash

(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 將軟件包升級到最新版本:ide

(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 後跟一個或多個包名稱將從虛擬環境中刪除包。ui

pip show 將顯示有關特定包的信息:spa

(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 文件中:code

(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 安裝全部必需的包:htm

(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教程

相關文章
相關標籤/搜索