驗證¶
基本 HTTP 驗證¶
pip 支援基於 HTTP 的基本驗證憑證。這可透過在 URL 中提供使用者名稱(以及選填密碼)來完成
https://username:password@pypi.company.com/simple
對於僅需要單部分驗證令牌的索引,請將令牌提供為「使用者名稱」,不要提供密碼
https://0123456789abcdef@pypi.company.com/simple
百分比編碼特殊字元¶
新增於版本 10.0。
某些特殊字元在 URL 的憑證部分中無效。如果您的登入憑證的使用者名稱或密碼部分包含任何這些 特殊字元,則它們必須經過百分比編碼。舉例來說,對於使用者名稱為 user
和密碼為 he//o
、存取位於 pypi.company.com/simple
的儲存庫時,包含憑證的 URL 會如下所示
https://user:he%2F%2Fo@pypi.company.com/simple
netrc 支援¶
pip 支援從使用者的 .netrc
檔案載入憑證。如果 URL 中沒有包含任何憑證,pip 會嘗試從使用者的 .netrc
檔案取得該 URL 主機名稱的驗證憑證。此行為來自於 requests 的基礎使用,而該函式庫反過來則將之委派給 Python 標準函式庫的 netrc
模組。
注意
如 netrc 的標準函式庫文件 中所述,只有 ASCII 字元可以在 .netrc
檔案中使用。密碼中不允許有空白和不可列印字元。
以下是 .netrc
的範例,其中主機為 example.com
,使用者名稱為 daniel
,使用的密碼為 qwerty
machine example.com
login daniel
password qwerty
可以在 GNU ftp
手冊頁面 中找到有關 .netrc
檔案格式的更多資訊。
鑰匙圈支援¶
pip 支援使用 keyring 函式庫載入儲存在鑰匙圈中的認證,可透過傳遞值為 --keyring-provider
的 auto
、disabled
、import
或 subprocess
的 --keyring-provider
來啟用,預設值 auto
尊重 --no-input
,並在使用選項時完全不查詢鑰匙圈;否則,它會嘗試 import
、subprocess
和 disabled
提供者(按照這個順序),並使用第一個可用的提供者。
設定 pip 的鑰匙圈使用方式¶
由於鑰匙圈設定可能是系統級的,設定其使用方式最常見的方式會是使用設定檔
另請參閱
設定檔 說明 pip 設定檔如何運作。
$ pip config set --global global.keyring-provider subprocess
# A different user on the same system which has PYTHONPATH configured and and
# wanting to use keyring installed that way could then run
$ pip config set --user global.keyring-provider import
# For a specific virtual environment you might want to use disable it again
# because you will only be using PyPI and the private repo (and mirror)
# requires 2FA with a keycard and a pincode
$ pip config set --site global.index https://pypi.org/simple
$ pip config set --site global.keyring-provider disabled
# configuring it via environment variable is also possible
$ export PIP_KEYRING_PROVIDER=disabled
使用 keyring 的 Python 模組¶
將 keyring-provider
設定為 import
,會讓 pip 透過其 Python 介面與 keyring
進行通訊。
# install keyring from PyPI
$ pip install keyring --index-url https://pypi.org/simple
$ echo "your-password" | keyring set pypi.company.com your-username
$ pip install your-package --keyring-provider import --index-url https://pypi.company.com/
將 keyring 視為一個命令列應用程式¶
將 keyring-provider
設定為 subprocess
,會讓 pip 尋找並使用 PATH
中找到的 keyring
指令。
針對這個用例,URL 中必須包含使用者名稱,因為 keyring
的命令列介面需要這個資訊。請參閱下方的範例或本頁頂端的 HTTP 基本認證區段。
# Install keyring from PyPI using pipx, which we assume is installed properly
# you can also create a venv somewhere and add it to the PATH yourself instead
$ pipx install keyring --index-url https://pypi.org/simple
# For Azure DevOps, also install its keyring backend.
$ pipx inject keyring artifacts-keyring --index-url https://pypi.org/simple
# For Google Artifact Registry, also install and initialize its keyring backend.
$ pipx inject keyring keyrings.google-artifactregistry-auth --index-url https://pypi.org/simple
$ gcloud auth login
# Note that a username is required in the index URL.
$ pip install your-package --keyring-provider subprocess --index-url https://username@pypi.example.com/
此處可能有風險¶
當使用 --no-input
時,auto
提供者是保守的,完全不會查詢金鑰環,因為金鑰環可能需要使用者互動,例如在控制台上提示使用者。第三方工具會頻繁地為你呼叫 Pip,並的確會傳遞 --no-input
,因為它們行為良好,而且沒有什麼資訊可供使用。(金鑰環的其中一個 API 可用來請求不需要使用者輸入的後端。)不過,你對系統有更多的資訊!
你可以請求 auto
(或 disabled
)以外的金鑰環提供者來強制使用金鑰環。請保留 import
和 subprocess
。你可以透過傳遞 --keyring-provider import
或下列方法之一來執行此動作
# via config file, possibly with --user, --global or --site
$ pip config set global.keyring-provider subprocess
# or via environment variable
$ export PIP_KEYRING_PROVIDER=import
警告
在執行此動作時務必小心,因為它可能導致 pipx 和 Pipenv 等工具看來會當機。在隱藏 Pip 執行過程中的輸出時,這些工具會顯示自身的進度指標。在這種情況下,你不會知道金鑰環後端是否正在等待使用者輸入。
當使用 --no-input
時,pip 是保守的,完全不會查詢金鑰環,因為金鑰環可能需要使用者互動,例如在控制台上提示使用者。你可以透過傳遞 --force-keyring
或是下列方法之一來強制使用金鑰環
# possibly with --user, --global or --site
$ pip config set global.force-keyring true
# or
$ export PIP_FORCE_KEYRING=1
警告
在執行此動作時務必小心,因為它可能導致 pipx 和 Pipenv 等工具看來會當機。在隱藏 Pip 執行過程中的輸出時,這些工具會顯示自身的進度指標。在這種情況下,你不會知道金鑰環後端是否正在等待使用者輸入。
請注意,keyring
(Python 套件)需要與 pip 分開安裝。如果你需要將儲存在金鑰環中的憑證下載並安裝 keyring,這樣可能會造成自我參照的安裝問題。
因此,預期希望使用 pip 的金鑰環支援的使用者具備一些可以用來下載並安裝 keyring 的機制。