VCS 支援

pip 支援從各種版本控制系統 (VCS) 安裝。此一支援需要可用的執行程式 (對於正在使用的版本控制系統)。它透過 URL 前置字元使用

  • Git -- git+

  • Mercurial -- hg+

  • Subversion -- svn+

  • Bazaar -- bzr+

受支援的 VCS

Git

受支援的架構為 git+filegit+httpsgit+sshgit+httpgit+gitgit。以下是部分受支援的格式

MyProject @ git+ssh://git@git.example.com/MyProject
MyProject @ git+file:///home/user/projects/MyProject
MyProject @ git+https://git.example.com/MyProject

警告

不建議使用 gitgit+gitgit+http 架構。前二者使用 Git 協定,這個協定沒有驗證,而 HTTP 因為缺乏基於 TLS 的加密而安全性不佳。

也可以指定「git 參照」,例如分支名稱、commit 哈希或標籤名稱

MyProject @ git+https://git.example.com/MyProject.git@master
MyProject @ git+https://git.example.com/MyProject.git@v1.0
MyProject @ git+https://git.example.com/MyProject.git@da39a3ee5e6b4b0d3255bfef95601890afd80709
MyProject @ git+https://git.example.com/MyProject.git@refs/pull/123/head

傳遞 commit 哈希時,最好指定完整的哈希,而不是部分哈希,因為完整的哈希可以讓 pip 更有效率地運作(例如執行較少的網路呼叫)。

Mercurial

受支援的架構為 hg+filehg+httphg+httpshg+sshhg+static-http。以下是部分受支援的格式

MyProject @ hg+http://hg.myproject.org/MyProject
MyProject @ hg+https://hg.myproject.org/MyProject
MyProject @ hg+ssh://hg.myproject.org/MyProject
MyProject @ hg+file:///home/user/projects/MyProject

也可以指定修訂號碼、修訂哈希、標籤名稱或本地分支名稱

MyProject @ hg+http://hg.example.com/MyProject@da39a3ee5e6b
MyProject @ hg+http://hg.example.com/MyProject@2019
MyProject @ hg+http://hg.example.com/MyProject@v1.0
MyProject @ hg+http://hg.example.com/MyProject@special_feature

Subversion

受支援的架構為 svnsvn+svnsvn+httpsvn+httpssvn+ssh。以下是部分受支援的格式

MyProject @ svn+https://svn.example.com/MyProject
MyProject @ svn+ssh://svn.example.com/MyProject
MyProject @ svn+ssh://user@svn.example.com/MyProject

你也可以對 SVN URL 指定特定修訂,如下

-e svn+http://svn.example.com/svn/MyProject/trunk@2019#egg=MyProject
-e svn+http://svn.example.com/svn/MyProject/trunk@{20080101}#egg=MyProject

請注意,您需要使用 可編輯的 VCS 安裝 才能使用 Subversion 的特定修訂版本。

Bazaar

支援的方案為 bzr+httpbzr+httpsbzr+sshbzr+sftpbzr+ftpbzr+lp。以下為支援的格式

MyProject @ bzr+http://bzr.example.com/MyProject/trunk
MyProject @ bzr+sftp://user@example.com/MyProject/trunk
MyProject @ bzr+ssh://user@example.com/MyProject/trunk
MyProject @ bzr+ftp://user@example.com/MyProject/trunk
MyProject @ bzr+lp:MyProject

標籤或修訂版本可以這樣安裝

MyProject @ bzr+https://bzr.example.com/MyProject/trunk@2019
MyProject @ bzr+http://bzr.example.com/MyProject/trunk@v1.0

可編輯的 VCS 安裝

VCS 專案可以安裝在 可編輯模式 中(使用 --editable 選項),也可以不安裝在可編輯模式中。

  • 預設複製品位置(針對可編輯安裝)為

    • <venv path>/src/SomeProject 在虛擬環境中

    • <cwd>/src/SomeProject 針對全域 Python 安裝

    可以使用 --src 選項來修改此位置。

  • 對於無法編輯的安裝,專案會在暫存目錄中以本機方式建置,然後正常安裝。

請注意,如果已安裝滿足條件的套件版本,VCS 來源不會在沒有 --upgrade 旗標的情況下覆寫該套件。此外,pip 會查看套件版本並查看目標修訂,以決定針對 VCS 需求採取什麼動作(而非提交本執行緒)。

URL 片段

pip 會查看 VCS URL 中的 subdirectory 片段,以指定 Python 套件的路徑,但 VCS 目錄中沒有該套件(例如:pkg_dir)。

pip 也會查看 egg 片段,以指定「專案名稱」。實際上,egg 片段僅需要在可編輯模式中協助 pip 決定 VCS 複製品位置。在所有其他情況中,egg 片段都不是必要的,而且不建議使用。

egg 片段應該專案名稱 的空白。其他任何內容都無法保證正常運作。

範例

如果您的儲存庫配置為

pkg_dir
├── setup.py  # setup.py for package "pkg"
└── some_module.py
other_dir
└── some_file
some_other_file

那麼,要從這個儲存庫安裝,語法會是

$ python -m pip install "pkg @ vcs+protocol://repo_url/#subdirectory=pkg_dir"
$ python -m pip install "pkg @ vcs+protocol://repo_url/#subdirectory=pkg_dir"
C:> py -m pip install "pkg @ vcs+protocol://repo_url/#subdirectory=pkg_dir"

$ python -m pip install -e "vcs+protocol://repo_url/#egg=pkg&subdirectory=pkg_dir"
$ python -m pip install -e "vcs+protocol://repo_url/#egg=pkg&subdirectory=pkg_dir"
C:> py -m pip install -e "vcs+protocol://repo_url/#egg=pkg&subdirectory=pkg_dir"